ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
light_state.cpp
Go to the documentation of this file.
1#include "light_state.h"
5#include "esphome/core/log.h"
6#include "light_output.h"
7#include "transformers.h"
8
9namespace esphome::light {
10
11static const char *const TAG = "light";
12
13LightState::LightState(LightOutput *output) : output_(output) {}
14
20
22 this->output_->setup_state(this);
23 for (auto *effect : this->effects_) {
24 effect->init_internal(this);
25 }
26
27 // Start with loop disabled if idle - respects any effects/transitions set up during initialization
29
30 // When supported color temperature range is known, initialize color temperature setting within bounds.
31 auto traits = this->get_traits();
32 float min_mireds = traits.get_min_mireds();
33 if (min_mireds > 0) {
34 this->remote_values.set_color_temperature(min_mireds);
35 this->current_values.set_color_temperature(min_mireds);
36 }
37
38 auto call = this->make_call();
39 LightStateRTCState recovered{};
40 if (this->initial_state_callback_) {
41 this->initial_state_callback_(recovered);
42 this->initial_state_callback_ = nullptr; // One-shot — no longer needed
43 }
44 switch (this->restore_mode_) {
50 // Attempt to load from preferences, else fall back to default values
51 if (!this->rtc_.load(&recovered)) {
52 recovered.state = (this->restore_mode_ == LIGHT_RESTORE_DEFAULT_ON ||
56 // Inverted restore state
57 recovered.state = !recovered.state;
58 }
59 break;
63 this->rtc_.load(&recovered);
64 recovered.state = (this->restore_mode_ == LIGHT_RESTORE_AND_ON);
65 break;
67 recovered.state = false;
68 break;
69 case LIGHT_ALWAYS_ON:
70 recovered.state = true;
71 break;
72 }
73
74 call.set_color_mode_if_supported(recovered.color_mode);
75 call.set_state(recovered.state);
76 call.set_brightness_if_supported(recovered.brightness);
77 call.set_color_brightness_if_supported(recovered.color_brightness);
78 call.set_red_if_supported(recovered.red);
79 call.set_green_if_supported(recovered.green);
80 call.set_blue_if_supported(recovered.blue);
81 call.set_white_if_supported(recovered.white);
82 call.set_color_temperature_if_supported(recovered.color_temp);
83 call.set_cold_white_if_supported(recovered.cold_white);
84 call.set_warm_white_if_supported(recovered.warm_white);
85 if (recovered.effect != 0) {
86 call.set_effect(recovered.effect);
87 } else {
88 call.set_transition_length_if_supported(0);
89 }
90 call.perform();
91}
93 ESP_LOGCONFIG(TAG, "Light '%s'", this->get_name().c_str());
94 auto traits = this->get_traits();
95 if (traits.supports_color_capability(ColorCapability::BRIGHTNESS)) {
96 ESP_LOGCONFIG(TAG,
97 " Default Transition Length: %.1fs\n"
98 " Gamma Correct: %.2f",
99 this->default_transition_length_ / 1e3f, this->gamma_correct_);
100 }
101 if (traits.supports_color_capability(ColorCapability::COLOR_TEMPERATURE)) {
102 ESP_LOGCONFIG(TAG,
103 " Min Mireds: %.1f\n"
104 " Max Mireds: %.1f",
105 traits.get_min_mireds(), traits.get_max_mireds());
106 }
107}
109 // Apply effect (if any)
110 auto *effect = this->get_active_effect_();
111 if (effect != nullptr) {
112 effect->apply();
113 }
114
115 // Apply transformer (if any)
116 if (this->transformer_ != nullptr) {
117 auto values = this->transformer_->apply();
118 this->is_transformer_active_ = true;
119 if (values.has_value()) {
120 this->current_values = *values;
121 this->output_->update_state(this);
122 this->next_write_ = true;
123 }
124
125 if (this->transformer_->is_finished()) {
126 // if the transition has written directly to the output, current_values is outdated, so update it
127 this->current_values = this->transformer_->get_target_values();
128
129 this->transformer_->stop();
130 this->is_transformer_active_ = false;
131 this->transformer_ = nullptr;
133 for (auto *listener : *this->target_state_reached_listeners_) {
134 listener->on_light_target_state_reached();
135 }
136 }
137
138 // Disable loop if idle (no transformer and no effect)
139 this->disable_loop_if_idle_();
140 }
141 }
142
143 // Write state to the light
144 if (this->next_write_) {
145 this->next_write_ = false;
146 this->output_->write_state(this);
147 // Disable loop if idle (no transformer and no effect)
148 this->disable_loop_if_idle_();
149 }
150}
151
153
155 if (this->remote_values_listeners_) {
156 for (auto *listener : *this->remote_values_listeners_) {
157 listener->on_light_remote_values_update();
158 }
159 }
160#if defined(USE_LIGHT) && defined(USE_CONTROLLER_REGISTRY)
161 ControllerRegistry::notify_light_update(this);
162#endif
163}
164
166
167static constexpr auto EFFECT_NONE_REF = StringRef::from_lit("None");
168
170 if (this->active_effect_index_ > 0) {
171 return this->effects_[this->active_effect_index_ - 1]->get_name();
172 }
173 return EFFECT_NONE_REF;
174}
175
177 if (!this->remote_values_listeners_) {
178 this->remote_values_listeners_ = make_unique<std::vector<LightRemoteValuesListener *>>();
179 }
180 this->remote_values_listeners_->push_back(listener);
181}
184 this->target_state_reached_listeners_ = make_unique<std::vector<LightTargetStateReachedListener *>>();
185 }
186 this->target_state_reached_listeners_->push_back(listener);
187}
188
190 this->default_transition_length_ = default_transition_length;
191}
194 this->flash_transition_length_ = flash_transition_length;
195}
198void LightState::set_restore_mode(LightRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
199void LightState::set_initial_state(void (*callback)(LightStateRTCState &)) { this->initial_state_callback_ = callback; }
200bool LightState::supports_effects() { return !this->effects_.empty(); }
202void LightState::add_effects(const std::initializer_list<LightEffect *> &effects) {
203 // Called once from Python codegen during setup with all effects from YAML config
204 this->effects_ = effects;
205}
206
209 this->current_values.as_brightness(brightness);
210 *brightness = this->gamma_correct_lut(*brightness);
211}
212void LightState::current_values_as_rgb(float *red, float *green, float *blue) {
213 this->current_values.as_rgb(red, green, blue);
214 *red = this->gamma_correct_lut(*red);
215 *green = this->gamma_correct_lut(*green);
216 *blue = this->gamma_correct_lut(*blue);
217}
218void LightState::current_values_as_rgbw(float *red, float *green, float *blue, float *white) {
219 this->current_values.as_rgbw(red, green, blue, white);
220 *red = this->gamma_correct_lut(*red);
221 *green = this->gamma_correct_lut(*green);
222 *blue = this->gamma_correct_lut(*blue);
223 *white = this->gamma_correct_lut(*white);
224}
225void LightState::current_values_as_rgbww(float *red, float *green, float *blue, float *cold_white, float *warm_white,
226 bool constant_brightness) {
227 this->current_values.as_rgb(red, green, blue);
228 *red = this->gamma_correct_lut(*red);
229 *green = this->gamma_correct_lut(*green);
230 *blue = this->gamma_correct_lut(*blue);
231 this->current_values_as_cwww(cold_white, warm_white, constant_brightness);
232}
233void LightState::current_values_as_rgbct(float *red, float *green, float *blue, float *color_temperature,
234 float *white_brightness) {
235 auto traits = this->get_traits();
236 this->current_values.as_rgbct(traits.get_min_mireds(), traits.get_max_mireds(), red, green, blue, color_temperature,
237 white_brightness);
238 *red = this->gamma_correct_lut(*red);
239 *green = this->gamma_correct_lut(*green);
240 *blue = this->gamma_correct_lut(*blue);
241 *white_brightness = this->gamma_correct_lut(*white_brightness);
242}
243void LightState::current_values_as_cwww(float *cold_white, float *warm_white, bool constant_brightness) {
244 if (!constant_brightness) {
245 // Without constant_brightness, gamma commutes with simple multiplication:
246 // gamma(white_level * cw) = gamma(white_level) * gamma(cw)
247 // (since gamma(a*b) = (a*b)^g = a^g * b^g = gamma(a) * gamma(b))
248 // so applying gamma after is mathematically equivalent and simpler.
249 this->current_values.as_cwww(cold_white, warm_white, false);
250 *cold_white = this->gamma_correct_lut(*cold_white);
251 *warm_white = this->gamma_correct_lut(*warm_white);
252 return;
253 }
254
255 // For constant_brightness mode, gamma MUST be applied to the individual
256 // channel values BEFORE the balancing formula (max/sum ratio), not after.
257 //
258 // Why: The cold_white_ and warm_white_ values stored in LightColorValues
259 // are gamma-uncorrected (see transform_parameters_() which applies
260 // gamma_uncorrect to the linear CW/WW fractions derived from color
261 // temperature). Applying gamma_correct here recovers the original linear
262 // fractions, which the constant_brightness formula then uses to distribute
263 // power evenly. The max/sum formula ensures cold+warm PWM output sums to
264 // a constant, keeping total power (and perceived brightness) the same
265 // across all color temperatures.
266 //
267 // Applying gamma AFTER the formula would be incorrect because gamma is
268 // nonlinear: gamma(a/b) != gamma(a)/gamma(b), so the carefully balanced
269 // ratio would be distorted, causing a severe brightness dip at mid-range
270 // color temperatures.
271 const auto &v = this->current_values;
272 if (!(v.get_color_mode() & ColorCapability::COLD_WARM_WHITE)) {
273 *cold_white = *warm_white = 0;
274 return;
275 }
276
277 const float cw_level = this->gamma_correct_lut(v.get_cold_white());
278 const float ww_level = this->gamma_correct_lut(v.get_warm_white());
279 const float white_level = this->gamma_correct_lut(v.get_state() * v.get_brightness());
280 const float sum = cw_level > 0 || ww_level > 0 ? cw_level + ww_level : 1; // Don't divide by zero.
281 *cold_white = white_level * std::max(cw_level, ww_level) * cw_level / sum;
282 *warm_white = white_level * std::max(cw_level, ww_level) * ww_level / sum;
283}
284void LightState::current_values_as_ct(float *color_temperature, float *white_brightness) {
285 auto traits = this->get_traits();
286 this->current_values.as_ct(traits.get_min_mireds(), traits.get_max_mireds(), color_temperature, white_brightness);
287 *white_brightness = this->gamma_correct_lut(*white_brightness);
288}
289
290#ifdef USE_LIGHT_GAMMA_LUT
291float LightState::gamma_correct_lut(float value) const {
292 if (value <= 0.0f)
293 return 0.0f;
294 if (value >= 1.0f)
295 return 1.0f;
296 if (this->gamma_table_ == nullptr)
297 return value;
298 float scaled = value * 255.0f;
299 auto idx = static_cast<uint8_t>(scaled);
300 if (idx >= 255)
301 return progmem_read_uint16(&this->gamma_table_[255]) / 65535.0f;
302 float frac = scaled - idx;
303 float a = progmem_read_uint16(&this->gamma_table_[idx]);
304 float b = progmem_read_uint16(&this->gamma_table_[idx + 1]);
305 return (a + frac * (b - a)) / 65535.0f;
306}
307float LightState::gamma_uncorrect_lut(float value) const {
308 if (value <= 0.0f)
309 return 0.0f;
310 if (value >= 1.0f)
311 return 1.0f;
312 if (this->gamma_table_ == nullptr)
313 return value;
314 uint16_t target = static_cast<uint16_t>(value * 65535.0f);
315 uint8_t lo = gamma_table_reverse_search(this->gamma_table_, target);
316 if (lo >= 255)
317 return 1.0f;
318 // Interpolate between lo and lo+1
319 uint16_t a = progmem_read_uint16(&this->gamma_table_[lo]);
320 uint16_t b = progmem_read_uint16(&this->gamma_table_[lo + 1]);
321 if (b == a)
322 return lo / 255.0f;
323 float frac = static_cast<float>(target - a) / static_cast<float>(b - a);
324 return (lo + frac) / 255.0f;
325}
326#endif // USE_LIGHT_GAMMA_LUT
327
329
331 this->stop_effect_();
332 if (effect_index == 0)
333 return;
334
335 this->active_effect_index_ = effect_index;
336 auto *effect = this->get_active_effect_();
337 effect->start_internal();
338 // Enable loop while effect is active
339 this->enable_loop();
340}
342 if (this->active_effect_index_ == 0) {
343 return nullptr;
344 } else {
345 return this->effects_[this->active_effect_index_ - 1];
346 }
347}
349 auto *effect = this->get_active_effect_();
350 if (effect != nullptr) {
351 effect->stop();
352 }
353 this->active_effect_index_ = 0;
354 // Disable loop if idle (no effect and no transformer)
355 this->disable_loop_if_idle_();
356}
357
358void LightState::start_transition_(const LightColorValues &target, uint32_t length, bool set_remote_values) {
360 this->transformer_->setup(this->current_values, target, length);
361
362 if (set_remote_values) {
363 this->remote_values = target;
364 }
365 // Enable loop while transition is active
366 this->enable_loop();
367}
368
369void LightState::start_flash_(const LightColorValues &target, uint32_t length, bool set_remote_values) {
370 LightColorValues end_colors = this->remote_values;
371 // If starting a flash if one is already happening, set end values to end values of current flash
372 // Hacky but works
373 if (this->transformer_ != nullptr)
374 end_colors = this->transformer_->get_start_values();
375
376 this->transformer_ = make_unique<LightFlashTransformer>(*this);
377 this->transformer_->setup(end_colors, target, length);
378
379 if (set_remote_values) {
380 this->remote_values = target;
381 };
382 // Enable loop while flash is active
383 this->enable_loop();
384}
385
386void LightState::set_immediately_(const LightColorValues &target, bool set_remote_values) {
387 this->is_transformer_active_ = false;
388 this->transformer_ = nullptr;
389 this->current_values = target;
390 if (set_remote_values) {
391 this->remote_values = target;
392 }
393 this->output_->update_state(this);
394 this->schedule_write_();
395}
396
398 // Only disable loop if both transformer and effect are inactive, and no pending writes
399 if (this->transformer_ == nullptr && this->get_active_effect_() == nullptr && !this->next_write_) {
400 this->disable_loop();
401 }
402}
403
405 LightStateRTCState saved;
407 switch (this->restore_mode_) {
410 saved.state = (this->restore_mode_ == LIGHT_RESTORE_AND_ON);
411 break;
412 default:
413 saved.state = this->remote_values.is_on();
414 break;
415 }
418 saved.red = this->remote_values.get_red();
419 saved.green = this->remote_values.get_green();
420 saved.blue = this->remote_values.get_blue();
421 saved.white = this->remote_values.get_white();
425 saved.effect = this->active_effect_index_;
426 this->rtc_.save(&saved);
427}
428
429} // namespace esphome::light
void enable_loop()
Enable this component's loop.
Definition component.h:258
void disable_loop()
Disable this component's loop.
const StringRef & get_name() const
Definition entity_base.h:71
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:522
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
static constexpr StringRef from_lit(const CharT(&s)[N])
Definition string_ref.h:50
This class represents a requested change in a light state.
Definition light_call.h:22
LightCall & set_state(optional< bool > state)
Set the binary ON/OFF state of the light.
This class represents the color state for a light object.
float get_brightness() const
Get the brightness property of these light color values. In range 0.0 to 1.0.
float get_blue() const
Get the blue property of these light color values. In range 0.0 to 1.0.
float get_white() const
Get the white property of these light color values. In range 0.0 to 1.0.
float get_color_temperature() const
Get the color temperature property of these light color values in mired.
void as_cwww(float *cold_white, float *warm_white, bool constant_brightness=false) const
Convert these light color values to an CWWW representation with the given parameters.
float get_cold_white() const
Get the cold white property of these light color values. In range 0.0 to 1.0.
void as_rgbw(float *red, float *green, float *blue, float *white) const
Convert these light color values to an RGBW representation and write them to red, green,...
void as_rgb(float *red, float *green, float *blue) const
Convert these light color values to an RGB representation and write them to red, green,...
bool is_on() const
Get the binary true/false state of these light color values.
float get_green() const
Get the green property of these light color values. In range 0.0 to 1.0.
void set_color_temperature(float color_temperature)
Set the color temperature property of these light color values in mired.
float get_warm_white() const
Get the warm white property of these light color values. In range 0.0 to 1.0.
void as_binary(bool *binary) const
Convert these light color values to a binary representation and write them to binary.
void as_ct(float color_temperature_cw, float color_temperature_ww, float *color_temperature, float *white_brightness) const
Convert these light color values to a CT+BR representation with the given parameters.
ColorMode get_color_mode() const
Get the color mode of these light color values.
void as_rgbct(float color_temperature_cw, float color_temperature_ww, float *red, float *green, float *blue, float *color_temperature, float *white_brightness) const
Convert these light color values to an RGB+CT+BR representation with the given parameters.
float get_red() const
Get the red property of these light color values. In range 0.0 to 1.0.
void as_brightness(float *brightness) const
Convert these light color values to a brightness-only representation and write them to brightness.
float get_color_brightness() const
Get the color brightness property of these light color values. In range 0.0 to 1.0.
Interface to write LightStates to hardware.
virtual void write_state(LightState *state)=0
Called from loop() every time the light state has changed, and should should write the new state to h...
virtual std::unique_ptr< LightTransformer > create_default_transition()
Return the default transformer used for transitions.
virtual LightTraits get_traits()=0
Return the LightTraits of this LightOutput.
virtual void update_state(LightState *state)
Called on every update of the current values of the associated LightState, can optionally be used to ...
virtual void setup_state(LightState *state)
Listener interface for light remote value changes.
Definition light_state.h:31
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)
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).
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).
void dump_config() override
LightColorValues remote_values
The remote color values reported to the frontend.
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 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.
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.
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_
bool supports_effects()
Return whether the light has any effects that meet the trait requirements.
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.
LightEffect * get_active_effect_()
Internal method to get the currently active effect.
bool is_transformer_active()
Indicator if a transformer (e.g.
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.
@ 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
uint8_t gamma_table_reverse_search(const uint16_t *table, uint16_t target)
Binary search a monotonically increasing uint16[256] PROGMEM table.
static float float b
@ BRIGHTNESS
Master brightness of the light can be controlled.
@ COLOR_TEMPERATURE
Color temperature can be controlled.
@ COLD_WARM_WHITE
Brightness of cold and warm white output can be controlled.
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:40
float gamma_correct(float value, float gamma)
Definition helpers.cpp:759
uint16_t progmem_read_uint16(const uint16_t *addr)
Definition core.cpp:40
uint16_t length
Definition tt21100.cpp:0