ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
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
11namespace esphome {
12namespace mqtt {
13
14static const char *const TAG = "mqtt.light";
15
16using namespace esphome::light;
17
18std::string MQTTJSONLightComponent::component_type() const { return "light"; }
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_(), [this](JsonObject root) {
36 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
38 });
39}
41
43 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
44 root["schema"] = "json";
45 auto traits = this->state_->get_traits();
46
47 root[MQTT_COLOR_MODE] = true;
48 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
49 JsonArray color_modes = root["supported_color_modes"].to<JsonArray>();
50 if (traits.supports_color_mode(ColorMode::ON_OFF))
51 color_modes.add("onoff");
52 if (traits.supports_color_mode(ColorMode::BRIGHTNESS))
53 color_modes.add("brightness");
54 if (traits.supports_color_mode(ColorMode::WHITE))
55 color_modes.add("white");
56 if (traits.supports_color_mode(ColorMode::COLOR_TEMPERATURE) ||
57 traits.supports_color_mode(ColorMode::COLD_WARM_WHITE))
58 color_modes.add("color_temp");
59 if (traits.supports_color_mode(ColorMode::RGB))
60 color_modes.add("rgb");
61 if (traits.supports_color_mode(ColorMode::RGB_WHITE) ||
62 // HA doesn't support RGBCT, and there's no CWWW->CT emulation in ESPHome yet, so ignore CT control for now
63 traits.supports_color_mode(ColorMode::RGB_COLOR_TEMPERATURE))
64 color_modes.add("rgbw");
65 if (traits.supports_color_mode(ColorMode::RGB_COLD_WARM_WHITE))
66 color_modes.add("rgbww");
67
68 // legacy API
69 if (traits.supports_color_capability(ColorCapability::BRIGHTNESS))
70 root["brightness"] = true;
71
72 if (this->state_->supports_effects()) {
73 root["effect"] = true;
74 JsonArray effect_list = root[MQTT_EFFECT_LIST].to<JsonArray>();
75 for (auto *effect : this->state_->get_effects())
76 effect_list.add(effect->get_name());
77 effect_list.add("None");
78 }
79}
82 ESP_LOGCONFIG(TAG, "MQTT Light '%s':", this->state_->get_name().c_str());
83 LOG_MQTT_COMPONENT(true, true)
84}
85
86} // namespace mqtt
87} // namespace esphome
88
89#endif
90#endif // USE_MQTT
91#endif // USE_JSON
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:69
This class represents a requested change in a light state.
Definition light_call.h:18
static void parse_json(LightState &state, LightCall &call, JsonObject root)
Parse the JSON state of a light to a LightCall.
static void dump_json(LightState &state, JsonObject root)
Dump the state of a light as JSON.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:66
const std::vector< LightEffect * > & get_effects() const
Get all effects for this light state.
void add_new_remote_values_callback(std::function< void()> &&send_callback)
This lets front-end components subscribe to light change events.
bool supports_effects()
Return whether the light has any effects that meet the trait requirements.
void subscribe_json(const std::string &topic, const mqtt_json_callback_t &callback, uint8_t qos=0)
Subscribe to a MQTT topic and automatically parse JSON payload.
bool publish_json(const std::string &topic, const json::json_build_t &f)
Construct and send a JSON MQTT message.
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
std::string get_command_topic_() const
Get the MQTT topic for listening to commands.
MQTTJSONLightComponent(light::LightState *state)
const EntityBase * get_entity() const override
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
light::LightState * get_state() const
std::string component_type() const override
bool state
Definition fan.h:0
constexpr const char *const MQTT_COLOR_MODE
Definition mqtt_const.h:38
constexpr const char *const MQTT_EFFECT_LIST
Definition mqtt_const.h:72
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Simple Helper struct used for Home Assistant MQTT send_discovery().