ESPHome  2024.11.3
mqtt_light.cpp
Go to the documentation of this file.
1 #include "mqtt_light.h"
2 #include "esphome/core/log.h"
3 
4 #include "mqtt_const.h"
5 
6 #ifdef USE_JSON
7 #ifdef USE_MQTT
8 #ifdef USE_LIGHT
9 
11 namespace esphome {
12 namespace mqtt {
13 
14 static const char *const TAG = "mqtt.light";
15 
16 using namespace esphome::light;
17 
18 std::string MQTTJSONLightComponent::component_type() const { return "light"; }
19 const EntityBase *MQTTJSONLightComponent::get_entity() const { return this->state_; }
20 
22  this->subscribe_json(this->get_command_topic_(), [this](const std::string &topic, JsonObject root) {
23  LightCall call = this->state_->make_call();
24  LightJSONSchema::parse_json(*this->state_, call, root);
25  call.perform();
26  });
27 
28  auto f = std::bind(&MQTTJSONLightComponent::publish_state_, this);
29  this->state_->add_new_remote_values_callback([this, f]() { this->defer("send", f); });
30 }
31 
33 
35  return this->publish_json(this->get_state_topic_(),
36  [this](JsonObject root) { LightJSONSchema::dump_json(*this->state_, root); });
37 }
39 
41  root["schema"] = "json";
42  auto traits = this->state_->get_traits();
43 
44  root[MQTT_COLOR_MODE] = true;
45  JsonArray color_modes = root.createNestedArray("supported_color_modes");
46  if (traits.supports_color_mode(ColorMode::ON_OFF))
47  color_modes.add("onoff");
48  if (traits.supports_color_mode(ColorMode::BRIGHTNESS))
49  color_modes.add("brightness");
50  if (traits.supports_color_mode(ColorMode::WHITE))
51  color_modes.add("white");
52  if (traits.supports_color_mode(ColorMode::COLOR_TEMPERATURE) ||
53  traits.supports_color_mode(ColorMode::COLD_WARM_WHITE))
54  color_modes.add("color_temp");
55  if (traits.supports_color_mode(ColorMode::RGB))
56  color_modes.add("rgb");
57  if (traits.supports_color_mode(ColorMode::RGB_WHITE) ||
58  // HA doesn't support RGBCT, and there's no CWWW->CT emulation in ESPHome yet, so ignore CT control for now
59  traits.supports_color_mode(ColorMode::RGB_COLOR_TEMPERATURE))
60  color_modes.add("rgbw");
61  if (traits.supports_color_mode(ColorMode::RGB_COLD_WARM_WHITE))
62  color_modes.add("rgbww");
63 
64  // legacy API
65  if (traits.supports_color_capability(ColorCapability::BRIGHTNESS))
66  root["brightness"] = true;
67 
68  if (this->state_->supports_effects()) {
69  root["effect"] = true;
70  JsonArray effect_list = root.createNestedArray(MQTT_EFFECT_LIST);
71  for (auto *effect : this->state_->get_effects())
72  effect_list.add(effect->get_name());
73  effect_list.add("None");
74  }
75 }
78  ESP_LOGCONFIG(TAG, "MQTT Light '%s':", this->state_->get_name().c_str());
79  LOG_MQTT_COMPONENT(true, true)
80 }
81 
82 } // namespace mqtt
83 } // namespace esphome
84 
85 #endif
86 #endif // USE_MQTT
87 #endif // USE_JSON
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:63
light::LightState * get_state() const
Definition: mqtt_light.cpp:38
bool supports_effects()
Return whether the light has any effects that meet the trait requirements.
constexpr const char *const MQTT_COLOR_MODE
Definition: mqtt_const.h:38
bool publish_json(const std::string &topic, const json::json_build_t &f)
Construct and send a JSON MQTT message.
MQTTJSONLightComponent(light::LightState *state)
Definition: mqtt_light.cpp:32
static void parse_json(LightState &state, LightCall &call, JsonObject root)
Parse the JSON state of a light to a LightCall.
Simple Helper struct used for Home Assistant MQTT send_discovery().
const std::vector< LightEffect * > & get_effects() const
Get all effects for this light state.
This class represents a requested change in a light state.
Definition: light_call.h:14
static void dump_json(LightState &state, JsonObject root)
Dump the state of a light as JSON.
White output only (use only if the light also has another color mode such as RGB).
constexpr const char * c_str() const
Definition: string_ref.h:68
const EntityBase * get_entity() const override
Definition: mqtt_light.cpp:19
std::string component_type() const override
Definition: mqtt_light.cpp:18
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
constexpr const char *const MQTT_EFFECT_LIST
Definition: mqtt_const.h:70
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
Definition: mqtt_light.cpp:40
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
const StringRef & get_name() const
Definition: entity_base.cpp:10
bool state
Definition: fan.h:34