ESPHome 2026.3.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;
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);
162
164 void set_flash_transition_length(uint32_t flash_transition_length);
166
169 float get_gamma_correct() const { return this->gamma_correct_; }
170
171#ifdef USE_LIGHT_GAMMA_LUT
173 void set_gamma_table(const uint16_t *forward) { this->gamma_table_ = forward; }
174
176 const uint16_t *get_gamma_table() const { return this->gamma_table_; }
177
179 float gamma_correct_lut(float value) const;
181 float gamma_uncorrect_lut(float value) const;
182#else
184 float gamma_correct_lut(float value) const { return value; }
185 float gamma_uncorrect_lut(float value) const { return value; }
186#endif // USE_LIGHT_GAMMA_LUT
187
189 void set_restore_mode(LightRestoreMode restore_mode);
190
192 void set_initial_state(const LightStateRTCState &initial_state);
193
195 bool supports_effects();
196
199
201 void add_effects(const std::initializer_list<LightEffect *> &effects);
202
204 size_t get_effect_count() const { return this->effects_.size(); }
205
208
210 uint32_t get_effect_index(const std::string &effect_name) const {
211 if (str_equals_case_insensitive(effect_name, "none")) {
212 return 0;
213 }
214 for (size_t i = 0; i < this->effects_.size(); i++) {
215 if (str_equals_case_insensitive(effect_name, this->effects_[i]->get_name())) {
216 return i + 1; // Effects are 1-indexed in active_effect_index_
217 }
218 }
219 return 0; // Effect not found
220 }
221
223 uint32_t get_effect_index(const char *name, size_t len) const {
224 if (len == 4 && ESPHOME_strncasecmp_P(name, ESPHOME_PSTR("none"), 4) == 0) {
225 return 0;
226 }
227 StringRef ref(name, len);
228 for (size_t i = 0; i < this->effects_.size(); i++) {
229 if (str_equals_case_insensitive(ref, this->effects_[i]->get_name())) {
230 return i + 1;
231 }
232 }
233 return 0;
234 }
235
238 if (index == 0 || index > this->effects_.size()) {
239 return nullptr;
240 }
241 return this->effects_[index - 1]; // Effects are 1-indexed in active_effect_index_
242 }
243
245 std::string get_effect_name_by_index(uint32_t index) const {
246 if (index == 0) {
247 return "None";
248 }
249 if (index > this->effects_.size()) {
250 return ""; // Invalid index
251 }
252 return std::string(this->effects_[index - 1]->get_name());
253 }
254
256 void current_values_as_binary(bool *binary);
257
258 void current_values_as_brightness(float *brightness);
259
260 void current_values_as_rgb(float *red, float *green, float *blue);
261
262 void current_values_as_rgbw(float *red, float *green, float *blue, float *white);
263
264 void current_values_as_rgbww(float *red, float *green, float *blue, float *cold_white, float *warm_white,
265 bool constant_brightness = false);
266
267 void current_values_as_rgbct(float *red, float *green, float *blue, float *color_temperature,
268 float *white_brightness);
269
270 void current_values_as_cwww(float *cold_white, float *warm_white, bool constant_brightness = false);
271
272 void current_values_as_ct(float *color_temperature, float *white_brightness);
273
284
285 protected:
287 friend LightCall;
288 friend class AddressableLight;
289
291 void start_effect_(uint32_t effect_index);
295 void stop_effect_();
297 void start_transition_(const LightColorValues &target, uint32_t length, bool set_remote_values);
298
300 void start_flash_(const LightColorValues &target, uint32_t length, bool set_remote_values);
301
303 void set_immediately_(const LightColorValues &target, bool set_remote_values);
304
306 void save_remote_values_();
307
310
313 this->next_write_ = true;
314 this->enable_loop();
315 }
316
320 std::unique_ptr<LightTransformer> transformer_{nullptr};
333#ifdef USE_LIGHT_GAMMA_LUT
334 const uint16_t *gamma_table_{nullptr};
335#endif // USE_LIGHT_GAMMA_LUT
336
338 bool next_write_{true};
339 // for effects, true if a transformer (transition) is active.
341
351 std::unique_ptr<std::vector<LightRemoteValuesListener *>> remote_values_listeners_;
352
359 std::unique_ptr<std::vector<LightTargetStateReachedListener *>> target_state_reached_listeners_;
360
363
366};
367
368} // namespace esphome::light
void enable_loop()
Enable this component's loop.
const StringRef & get_name() const
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:299
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:21
This class represents the color state for a light object.
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).
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.
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).
optional< LightStateRTCState > initial_state_
Initial state of the light.
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)
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)
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 set_initial_state(const LightStateRTCState &initial_state)
Set the initial state of this light.
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.
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.
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.
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
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).
float gamma_correct(float value, float gamma)
Definition helpers.cpp:703
std::string size_t len
Definition helpers.h:817
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
Definition helpers.cpp:163
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