ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
homeassistant_light.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4
9
10namespace esphome::homeassistant {
11
15class HomeassistantLight final : public light::LightOutput, public Component {
16 public:
17 void set_entity_id(const char *entity_id) { this->entity_id_ = entity_id; }
19 void set_report_timeout(uint8_t seconds) { this->report_timeout_s_ = seconds; }
20 void setup() override;
21 void dump_config() override;
22 float get_setup_priority() const override { return setup_priority::AFTER_CONNECTION; }
23
24 light::LightTraits get_traits() override { return this->traits_; }
25 void setup_state(light::LightState *state) override { this->state_ = state; }
26 void update_state(light::LightState *state) override;
27 void write_state(light::LightState *state) override;
28
29 protected:
33 struct Attributes {
34 uint16_t color_temp_kelvin{0};
35 uint8_t brightness{0};
36 std::array<uint8_t, 3> rgb{};
38 bool has_rgb{false};
39 optional<bool> state;
40 };
41
43 optional<uint16_t> parse_integer_(const char *name, StringRef value, uint16_t max);
45 void on_report_(bool changed);
47 template<typename T> void import_(T &field, const T &value) {
48 this->on_report_(field != value);
49 field = value;
50 }
52 void schedule_apply_();
53 void apply_imported_();
55 bool is_imported_(const Attributes &outgoing) const;
57 void fall_back_();
58
59 const char *entity_id_{nullptr};
63 bool apply_scheduled_ : 1 {false};
64 bool initial_write_ : 1 {true}; // the light core's own first write is not a local change
65 bool initial_seen_ : 1 {false}; // the light core's setup call has set the initial state
66 bool echo_pending_ : 1 {false}; // a sent change waits for Home Assistant to report it back
67 bool report_held_ : 1 {false}; // a report arrived during a transition or flash; applied when it ends
68 bool transitioned_ : 1 {false}; // a transition or flash ran since the last write sent
69 uint8_t report_timeout_s_{10};
70};
71
72} // namespace esphome::homeassistant
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
A light in Home Assistant, driven from here: its state and attributes are imported over the native AP...
void fall_back_()
Returns the light to the reported state after a change that did not reach Home Assistant.
optional< uint16_t > parse_integer_(const char *name, StringRef value, uint16_t max)
Parses a Home Assistant attribute as an integer from 1 to max, logging anything else.
void on_report_(bool changed)
A state or attribute report: answers a pending echo when it changed something, and schedules the appl...
void schedule_apply_()
Schedules apply_imported_() once for the attributes of one update.
void write_state(light::LightState *state) override
void update_state(light::LightState *state) override
void import_(T &field, const T &value)
Takes over a reported attribute, reporting whether it changed.
void setup_state(light::LightState *state) override
bool is_imported_(const Attributes &outgoing) const
True when Home Assistant reported exactly what a change would make of it.
void set_report_timeout(uint8_t seconds)
How long a sent change may take to come back as a report before the light returns to the reported sta...
Interface to write LightStates to hardware.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:68
This class is used to represent the capabilities of a light.
Definition light_traits.h:9
bool state
Definition fan.h:2
ColorMode
Color modes are a combination of color capabilities that can be used at the same time.
Definition color_mode.h:50
@ UNKNOWN
No color mode configured (cannot be a supported mode, only active when light is off).
constexpr float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.h:57
What Home Assistant reported last, in its own units.