ESPHome 2026.5.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
193 void set_initial_state(void (*callback)(LightStateRTCState &));
194
196 bool supports_effects();
197
200
202 void add_effects(const std::initializer_list<LightEffect *> &effects);
203
205 size_t get_effect_count() const { return this->effects_.size(); }
206
209
211 uint32_t get_effect_index(const std::string &effect_name) const {
212 if (str_equals_case_insensitive(effect_name, "none")) {
213 return 0;
214 }
215 for (size_t i = 0; i < this->effects_.size(); i++) {
216 if (str_equals_case_insensitive(effect_name, this->effects_[i]->get_name())) {
217 return i + 1; // Effects are 1-indexed in active_effect_index_
218 }
219 }
220 return 0; // Effect not found
221 }
222
224 uint32_t get_effect_index(const char *name, size_t len) const {
225 if (len == 4 && ESPHOME_strncasecmp_P(name, ESPHOME_PSTR("none"), 4) == 0) {
226 return 0;
227 }
228 StringRef ref(name, len);
229 for (size_t i = 0; i < this->effects_.size(); i++) {
230 if (str_equals_case_insensitive(ref, this->effects_[i]->get_name())) {
231 return i + 1;
232 }
233 }
234 return 0;
235 }
236
239 if (index == 0 || index > this->effects_.size()) {
240 return nullptr;
241 }
242 return this->effects_[index - 1]; // Effects are 1-indexed in active_effect_index_
243 }
244
246 std::string get_effect_name_by_index(uint32_t index) const {
247 if (index == 0) {
248 return "None";
249 }
250 if (index > this->effects_.size()) {
251 return ""; // Invalid index
252 }
253 return std::string(this->effects_[index - 1]->get_name());
254 }
255
257 void current_values_as_binary(bool *binary);
258
259 void current_values_as_brightness(float *brightness);
260
261 void current_values_as_rgb(float *red, float *green, float *blue);
262
263 void current_values_as_rgbw(float *red, float *green, float *blue, float *white);
264
265 void current_values_as_rgbww(float *red, float *green, float *blue, float *cold_white, float *warm_white,
266 bool constant_brightness = false);
267
268 void current_values_as_rgbct(float *red, float *green, float *blue, float *color_temperature,
269 float *white_brightness);
270
271 void current_values_as_cwww(float *cold_white, float *warm_white, bool constant_brightness = false);
272
273 void current_values_as_ct(float *color_temperature, float *white_brightness);
274
285
286 protected:
288 friend LightCall;
289 friend class AddressableLight;
290
292 void start_effect_(uint32_t effect_index);
296 void stop_effect_();
298 void start_transition_(const LightColorValues &target, uint32_t length, bool set_remote_values);
299
301 void start_flash_(const LightColorValues &target, uint32_t length, bool set_remote_values);
302
304 void set_immediately_(const LightColorValues &target, bool set_remote_values);
305
307 void save_remote_values_();
308
311
314 this->next_write_ = true;
315 this->enable_loop();
316 }
317
321 std::unique_ptr<LightTransformer> transformer_{nullptr};
326
336 std::unique_ptr<std::vector<LightRemoteValuesListener *>> remote_values_listeners_;
337
344 std::unique_ptr<std::vector<LightTargetStateReachedListener *>> target_state_reached_listeners_;
345
349
358#ifdef USE_LIGHT_GAMMA_LUT
359 const uint16_t *gamma_table_{nullptr};
360#endif // USE_LIGHT_GAMMA_LUT
361
363 bool next_write_{true};
364 // for effects, true if a transformer (transition) is active.
368};
369
370} // namespace esphome::light
void enable_loop()
Enable this component's loop.
Definition component.h:258
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:522
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.
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).
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.
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:759
std::string size_t len
Definition helpers.h:1045
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
Definition helpers.cpp:202
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