ESPHome 2026.3.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::mqtt {
12
13static const char *const TAG = "mqtt.light";
14
15using namespace esphome::light;
16
18const EntityBase *MQTTJSONLightComponent::get_entity() const { return this->state_; }
19
21 this->subscribe_json(this->get_command_topic_(), [this](const std::string &topic, JsonObject root) {
22 LightCall call = this->state_->make_call();
23 LightJSONSchema::parse_json(*this->state_, call, root);
24 call.perform();
25 });
26
28}
29
31 this->defer("send", [this]() { this->publish_state_(); });
32}
33
35
37 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
38 return this->publish_json(this->get_state_topic_to_(topic_buf), [this](JsonObject root) {
39 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
41 });
42}
44
46 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
47 root[ESPHOME_F("schema")] = ESPHOME_F("json");
48 auto traits = this->state_->get_traits();
49
50 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
51 JsonArray color_modes = root[ESPHOME_F("supported_color_modes")].to<JsonArray>();
52 if (traits.supports_color_mode(ColorMode::ON_OFF))
53 color_modes.add(ESPHOME_F("onoff"));
54 if (traits.supports_color_mode(ColorMode::BRIGHTNESS))
55 color_modes.add(ESPHOME_F("brightness"));
56 if (traits.supports_color_mode(ColorMode::WHITE))
57 color_modes.add(ESPHOME_F("white"));
58 if (traits.supports_color_mode(ColorMode::COLOR_TEMPERATURE) ||
59 traits.supports_color_mode(ColorMode::COLD_WARM_WHITE))
60 color_modes.add(ESPHOME_F("color_temp"));
61 if (traits.supports_color_mode(ColorMode::RGB))
62 color_modes.add(ESPHOME_F("rgb"));
63 if (traits.supports_color_mode(ColorMode::RGB_WHITE) ||
64 // HA doesn't support RGBCT, and there's no CWWW->CT emulation in ESPHome yet, so ignore CT control for now
65 traits.supports_color_mode(ColorMode::RGB_COLOR_TEMPERATURE))
66 color_modes.add(ESPHOME_F("rgbw"));
67 if (traits.supports_color_mode(ColorMode::RGB_COLD_WARM_WHITE))
68 color_modes.add(ESPHOME_F("rgbww"));
69
70 if (traits.supports_color_mode(ColorMode::COLOR_TEMPERATURE) ||
71 traits.supports_color_mode(ColorMode::COLD_WARM_WHITE)) {
72 root[MQTT_MIN_MIREDS] = traits.get_min_mireds();
73 root[MQTT_MAX_MIREDS] = traits.get_max_mireds();
74 }
75
76 if (this->state_->supports_effects()) {
77 root[ESPHOME_F("effect")] = true;
78 JsonArray effect_list = root[MQTT_EFFECT_LIST].to<JsonArray>();
79 for (auto *effect : this->state_->get_effects()) {
80 // c_str() is safe as effect names are null-terminated strings from codegen
81 effect_list.add(effect->get_name().c_str());
82 }
83 effect_list.add(ESPHOME_F("None"));
84 }
85}
88 ESP_LOGCONFIG(TAG, "MQTT Light '%s':", this->state_->get_name().c_str());
89 LOG_MQTT_COMPONENT(true, true);
90}
91
92} // namespace esphome::mqtt
93
94#endif
95#endif // USE_MQTT
96#endif // USE_JSON
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call.
Definition component.h:493
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:73
This class represents a requested change in a light state.
Definition light_call.h:21
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:93
void add_remote_values_listener(LightRemoteValuesListener *listener)
Add a listener for remote values changes.
const FixedVector< LightEffect * > & get_effects() const
Get all effects for this light state.
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.
StringRef get_state_topic_to_(std::span< char, MQTT_DEFAULT_TOPIC_MAX_LEN > buf) const
Get the MQTT state topic into a buffer (no heap allocation for non-lambda custom topics).
std::string get_command_topic_() const
Get the MQTT topic for listening to commands (allocates std::string).
MQTTJSONLightComponent(light::LightState *state)
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
light::LightState * get_state() const
bool state
Definition fan.h:2
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
Simple Helper struct used for Home Assistant MQTT send_discovery().