ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
light_state.h
Go to the documentation of this file.
1#pragma once
2
8#include "light_call.h"
10#include "light_effect.h"
11#include "light_traits.h"
12#include "light_transformer.h"
13
14#include "esphome/core/hal.h"
17#include <strings.h>
18#include <vector>
19
20namespace esphome::light {
21
22class LightOutput;
23class LightState;
24
32 public:
34};
35
43 public:
45};
46
57
73 LightStateRTCState() = default;
74 // Group 4-byte aligned members first
75 float brightness{1.0f};
76 float color_brightness{1.0f};
77 float red{1.0f};
78 float green{1.0f};
79 float blue{1.0f};
80 float white{1.0f};
81 float color_temp{1.0f};
82 float cold_white{1.0f};
83 float warm_white{1.0f};
85 // Group smaller members at the end
87 bool state{false};
88};
89
93class LightState : public EntityBase, public Component {
94 public:
95 LightState(LightOutput *output);
96
98
104
105 // ========== INTERNAL METHODS ==========
106 // (In most use cases you won't need these)
108 void setup() override;
109 void dump_config() override;
110 void loop() override;
112 float get_setup_priority() const override { return setup_priority::HARDWARE - 1.0f; }
113
125
137
139 void publish_state();
140
142 LightOutput *get_output() const;
143
146
152
158
160 void set_default_transition_length(uint32_t default_transition_length) {
161 this->default_transition_length_ = default_transition_length;
162 }
164
166 void set_flash_transition_length(uint32_t flash_transition_length) {
167 this->flash_transition_length_ = flash_transition_length;
168 }
170
172 void set_gamma_correct(float gamma_correct) { this->gamma_correct_ = gamma_correct; }
173 float get_gamma_correct() const { return this->gamma_correct_; }
174
175#ifdef USE_LIGHT_GAMMA_LUT
177 void set_gamma_table(const uint16_t *forward) { this->gamma_table_ = forward; }
178
180 const uint16_t *get_gamma_table() const { return this->gamma_table_; }
181
183 float gamma_correct_lut(float value) const;
185 float gamma_uncorrect_lut(float value) const;
186#else
188 float gamma_correct_lut(float value) const { return value; }
189 float gamma_uncorrect_lut(float value) const { return value; }
190#endif // USE_LIGHT_GAMMA_LUT
191
193 void set_restore_mode(LightRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
194
197 void set_initial_state(void (*callback)(LightStateRTCState &)) { this->initial_state_callback_ = callback; }
198
200 bool supports_effects() const { return !this->effects_.empty(); }
201
203 const FixedVector<LightEffect *> &get_effects() const { return this->effects_; }
204
206 void add_effects(const std::initializer_list<LightEffect *> &effects);
207
209 size_t get_effect_count() const { return this->effects_.size(); }
210
213
215 uint32_t get_effect_index(const std::string &effect_name) const {
216 if (str_equals_case_insensitive(effect_name, "none")) {
217 return 0;
218 }
219 for (size_t i = 0; i < this->effects_.size(); i++) {
220 if (str_equals_case_insensitive(effect_name, this->effects_[i]->get_name())) {
221 return i + 1; // Effects are 1-indexed in active_effect_index_
222 }
223 }
224 return 0; // Effect not found
225 }
226
228 uint32_t get_effect_index(const char *name, size_t len) const {
229 if (len == 4 && ESPHOME_strncasecmp_P(name, ESPHOME_PSTR("none"), 4) == 0) {
230 return 0;
231 }
232 StringRef ref(name, len);
233 for (size_t i = 0; i < this->effects_.size(); i++) {
234 if (str_equals_case_insensitive(ref, this->effects_[i]->get_name())) {
235 return i + 1;
236 }
237 }
238 return 0;
239 }
240
243 if (index == 0 || index > this->effects_.size()) {
244 return nullptr;
245 }
246 return this->effects_[index - 1]; // Effects are 1-indexed in active_effect_index_
247 }
248
250 std::string get_effect_name_by_index(uint32_t index) const {
251 if (index == 0) {
252 return "None";
253 }
254 if (index > this->effects_.size()) {
255 return ""; // Invalid index
256 }
257 return std::string(this->effects_[index - 1]->get_name());
258 }
259
261 void current_values_as_binary(bool *binary) { this->current_values.as_binary(binary); }
262
263 void current_values_as_brightness(float *brightness);
264
265 void current_values_as_rgb(float *red, float *green, float *blue);
266
267 void current_values_as_rgbw(float *red, float *green, float *blue, float *white);
268
269 void current_values_as_rgbww(float *red, float *green, float *blue, float *cold_white, float *warm_white,
270 bool constant_brightness = false);
271
272 void current_values_as_rgbct(float *red, float *green, float *blue, float *color_temperature,
273 float *white_brightness);
274
275 void current_values_as_cwww(float *cold_white, float *warm_white, bool constant_brightness = false);
276
277 void current_values_as_ct(float *color_temperature, float *white_brightness);
278
288 bool is_transformer_active() const { return this->is_transformer_active_; }
289
290 protected:
292 friend LightCall;
293 friend class AddressableLight;
294
296 void start_effect_(uint32_t effect_index);
300 void stop_effect_();
302 void start_transition_(const LightColorValues &target, uint32_t length, bool set_remote_values);
303
305 void start_flash_(const LightColorValues &target, uint32_t length, bool set_remote_values);
306
308 void set_immediately_(const LightColorValues &target, bool set_remote_values);
309
311 void save_remote_values_();
312
315
318 this->next_write_ = true;
319 this->enable_loop();
320 }
321
325 std::unique_ptr<LightTransformer> transformer_{nullptr};
330
340 std::unique_ptr<std::vector<LightRemoteValuesListener *>> remote_values_listeners_;
341
348 std::unique_ptr<std::vector<LightTargetStateReachedListener *>> target_state_reached_listeners_;
349
353
362#ifdef USE_LIGHT_GAMMA_LUT
363 const uint16_t *gamma_table_{nullptr};
364#endif // USE_LIGHT_GAMMA_LUT
365
367 bool next_write_{true};
368 // for effects, true if a transformer (transition) is active.
372};
373
374} // namespace esphome::light
void enable_loop()
Enable this component's loop.
Definition component.h:246
const StringRef & get_name() const
Definition entity_base.h:71
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:545
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
This class represents a requested change in a light state.
Definition light_call.h:22
This class represents the color state for a light object.
void as_binary(bool *binary) const
Convert these light color values to a binary representation and write them to binary.
Interface to write LightStates to hardware.
Listener interface for light remote value changes.
Definition light_state.h:31
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.
float gamma_correct_
Gamma correction factor for the light.
FixedVector< LightEffect * > effects_
List of effects for this light.
StringRef get_effect_name()
Return the name of the current effect, or if no effect is active "None".
void start_effect_(uint32_t effect_index)
Internal method to start an effect with the given index.
void current_values_as_rgb(float *red, float *green, float *blue)
std::string get_effect_name_by_index(uint32_t index) const
Get effect name by index. Returns "None" for index 0, empty string for invalid index.
float gamma_correct_lut(float value) const
Apply gamma correction using the pre-computed forward LUT.
void stop_effect_()
Internal method to stop the current effect (if one is active).
bool supports_effects() const
Return whether the light has any effects that meet the trait requirements.
void current_values_as_rgbct(float *red, float *green, float *blue, float *color_temperature, float *white_brightness)
void disable_loop_if_idle_()
Disable loop if neither transformer nor effect is active.
std::unique_ptr< LightTransformer > transformer_
The currently active transformer for this light (transition/flash).
LightColorValues remote_values
The remote color values reported to the frontend.
uint32_t get_current_effect_index() const
Get the currently active effect index (0 = no effect, 1+ = effect index).
LightOutput * get_output() const
Get the light output associated with this object.
void set_flash_transition_length(uint32_t flash_transition_length)
Set the flash transition length.
LightState(LightOutput *output)
void current_values_as_binary(bool *binary)
The result of all the current_values_as_* methods have gamma correction applied.
void save_remote_values_()
Internal method to save the current remote_values to the preferences.
void add_target_state_reached_listener(LightTargetStateReachedListener *listener)
Add a listener for target state reached.
LightCall turn_on()
Make a light state call.
uint32_t get_default_transition_length() const
void set_immediately_(const LightColorValues &target, bool set_remote_values)
Internal method to set the color values to target immediately (with no transition).
uint32_t get_flash_transition_length() const
void set_gamma_table(const uint16_t *forward)
Set pre-computed gamma forward lookup table (256-entry uint16 PROGMEM array)
float get_gamma_correct() const
uint32_t get_effect_index(const std::string &effect_name) const
Get effect index by name. Returns 0 if effect not found.
void current_values_as_ct(float *color_temperature, float *white_brightness)
void set_initial_state(void(*callback)(LightStateRTCState &))
Set a callback to populate the initial state defaults during setup.
float gamma_uncorrect_lut(float value) const
Reverse gamma correction by binary-searching the forward LUT.
void current_values_as_cwww(float *cold_white, float *warm_white, bool constant_brightness=false)
void(* initial_state_callback_)(LightStateRTCState &)
Callback to populate initial state defaults — called once during setup, then cleared.
uint32_t flash_transition_length_
Transition length to use for flash transitions.
void publish_state()
Publish the currently active state to the frontend.
void current_values_as_rgbww(float *red, float *green, float *blue, float *cold_white, float *warm_white, bool constant_brightness=false)
void current_values_as_brightness(float *brightness)
void setup() override
Load state from preferences.
LightEffect * get_effect_by_index(uint32_t index) const
Get effect by index. Returns nullptr if index is invalid.
uint32_t active_effect_index_
Value for storing the index of the currently active effect. 0 if no effect is active.
void start_flash_(const LightColorValues &target, uint32_t length, bool set_remote_values)
Internal method to start a flash for the specified amount of time.
LightRestoreMode restore_mode_
Restore mode of the light.
bool is_transformer_active() const
Indicator if a transformer (e.g.
const FixedVector< LightEffect * > & get_effects() const
Get all effects for this light state.
void add_effects(const std::initializer_list< LightEffect * > &effects)
Add effects for this light state.
void schedule_write_()
Schedule a write to the light output and enable the loop to process it.
const uint16_t * gamma_table_
const uint16_t * get_gamma_table() const
Get the forward gamma lookup table.
bool next_write_
Whether the light value should be written in the next cycle.
void start_transition_(const LightColorValues &target, uint32_t length, bool set_remote_values)
Internal method to start a transition to the target color with the given length.
float get_setup_priority() const override
Shortly after HARDWARE.
void set_restore_mode(LightRestoreMode restore_mode)
Set the restore mode of this light.
LightOutput * output_
Store the output to allow effects to have more access.
ESPPreferenceObject rtc_
Object used to store the persisted values of the light.
void current_values_as_rgbw(float *red, float *green, float *blue, float *white)
uint32_t default_transition_length_
Default transition length for all transitions in ms.
LightColorValues current_values
The current values of the light as outputted to the light.
uint32_t get_effect_index(const char *name, size_t len) const
Get effect index by name (const char* overload, avoids std::string construction).
LightEffect * get_active_effect_()
Internal method to get the currently active effect.
size_t get_effect_count() const
Get the total number of effects available for this light.
std::unique_ptr< std::vector< LightRemoteValuesListener * > > remote_values_listeners_
Listeners for remote values changes.
std::unique_ptr< std::vector< LightTargetStateReachedListener * > > target_state_reached_listeners_
Listeners for target state reached.
void set_gamma_correct(float gamma_correct)
Set the gamma correction factor.
void set_default_transition_length(uint32_t default_transition_length)
Set the default transition length, i.e. the transition length when no transition is provided.
Listener interface for light target state reached.
Definition light_state.h:42
This class is used to represent the capabilities of a light.
Definition light_traits.h:9
@ LIGHT_RESTORE_INVERTED_DEFAULT_ON
Definition light_state.h:53
@ LIGHT_RESTORE_DEFAULT_OFF
Definition light_state.h:48
@ LIGHT_RESTORE_INVERTED_DEFAULT_OFF
Definition light_state.h:52
ColorMode
Color modes are a combination of color capabilities that can be used at the same time.
Definition color_mode.h:49
@ UNKNOWN
No color mode configured (cannot be a supported mode, only active when light is off).
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:43
const void size_t len
Definition hal.h:64
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
Definition helpers.cpp:201
LightStateRTCState(ColorMode color_mode, bool state, float brightness, float color_brightness, float red, float green, float blue, float white, float color_temp, float cold_white, float warm_white)
Definition light_state.h:59
uint16_t length
Definition tt21100.cpp:0