ESPHome 2025.9.0
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 <vector>
15#include <strings.h>
16
17namespace esphome {
18namespace light {
19
20class LightOutput;
21
32
48 LightStateRTCState() = default;
49 // Group 4-byte aligned members first
50 float brightness{1.0f};
51 float color_brightness{1.0f};
52 float red{1.0f};
53 float green{1.0f};
54 float blue{1.0f};
55 float white{1.0f};
56 float color_temp{1.0f};
57 float cold_white{1.0f};
58 float warm_white{1.0f};
60 // Group smaller members at the end
62 bool state{false};
63};
64
68class LightState : public EntityBase, public Component {
69 public:
70 LightState(LightOutput *output);
71
73
79
80 // ========== INTERNAL METHODS ==========
81 // (In most use cases you won't need these)
83 void setup() override;
84 void dump_config() override;
85 void loop() override;
87 float get_setup_priority() const override;
88
100
112
114 void publish_state();
115
117 LightOutput *get_output() const;
118
120 std::string get_effect_name();
123
130 void add_new_remote_values_callback(std::function<void()> &&send_callback);
131
138 void add_new_target_state_reached_callback(std::function<void()> &&send_callback);
139
141 void set_default_transition_length(uint32_t default_transition_length);
143
145 void set_flash_transition_length(uint32_t flash_transition_length);
147
150 float get_gamma_correct() const { return this->gamma_correct_; }
151
153 void set_restore_mode(LightRestoreMode restore_mode);
154
156 void set_initial_state(const LightStateRTCState &initial_state);
157
159 bool supports_effects();
160
162 const std::vector<LightEffect *> &get_effects() const;
163
165 void add_effects(const std::vector<LightEffect *> &effects);
166
168 size_t get_effect_count() const { return this->effects_.size(); }
169
172
174 uint32_t get_effect_index(const std::string &effect_name) const {
175 if (strcasecmp(effect_name.c_str(), "none") == 0) {
176 return 0;
177 }
178 for (size_t i = 0; i < this->effects_.size(); i++) {
179 if (strcasecmp(effect_name.c_str(), this->effects_[i]->get_name().c_str()) == 0) {
180 return i + 1; // Effects are 1-indexed in active_effect_index_
181 }
182 }
183 return 0; // Effect not found
184 }
185
188 if (index == 0 || index > this->effects_.size()) {
189 return nullptr;
190 }
191 return this->effects_[index - 1]; // Effects are 1-indexed in active_effect_index_
192 }
193
195 std::string get_effect_name_by_index(uint32_t index) const {
196 if (index == 0) {
197 return "None";
198 }
199 if (index > this->effects_.size()) {
200 return ""; // Invalid index
201 }
202 return this->effects_[index - 1]->get_name();
203 }
204
206 void current_values_as_binary(bool *binary);
207
208 void current_values_as_brightness(float *brightness);
209
210 void current_values_as_rgb(float *red, float *green, float *blue, bool color_interlock = false);
211
212 void current_values_as_rgbw(float *red, float *green, float *blue, float *white, bool color_interlock = false);
213
214 void current_values_as_rgbww(float *red, float *green, float *blue, float *cold_white, float *warm_white,
215 bool constant_brightness = false);
216
217 void current_values_as_rgbct(float *red, float *green, float *blue, float *color_temperature,
218 float *white_brightness);
219
220 void current_values_as_cwww(float *cold_white, float *warm_white, bool constant_brightness = false);
221
222 void current_values_as_ct(float *color_temperature, float *white_brightness);
223
234
235 protected:
237 friend LightCall;
238 friend class AddressableLight;
239
241 void start_effect_(uint32_t effect_index);
245 void stop_effect_();
247 void start_transition_(const LightColorValues &target, uint32_t length, bool set_remote_values);
248
250 void start_flash_(const LightColorValues &target, uint32_t length, bool set_remote_values);
251
253 void set_immediately_(const LightColorValues &target, bool set_remote_values);
254
256 void save_remote_values_();
257
261 std::unique_ptr<LightTransformer> transformer_{nullptr};
263 std::vector<LightEffect *> effects_;
275 bool next_write_{true};
276 // for effects, true if a transformer (transition) is active.
278
287
292
295
298};
299
300} // namespace light
301} // namespace esphome
StringRef is a reference to a string owned by something else.
Definition string_ref.h:22
This class represents a requested change in a light state.
Definition light_call.h:18
This class represents the color state for a light object.
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
float gamma_correct_
Gamma correction factor for the light.
void start_effect_(uint32_t effect_index)
Internal method to start an effect with the given index.
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.
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)
std::unique_ptr< LightTransformer > transformer_
The currently active transformer for this light (transition/flash).
const std::vector< LightEffect * > & get_effects() const
Get all effects for this light state.
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.
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
float get_gamma_correct() const
std::string get_effect_name()
Return the name of the current effect, or if no effect is active "None".
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)
CallbackManager< void()> remote_values_callback_
Callback to call when new values for the frontend are available.
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 add_new_remote_values_callback(std::function< void()> &&send_callback)
This lets front-end components subscribe to light change events.
void current_values_as_brightness(float *brightness)
StringRef get_effect_name_ref()
Return the name of the current effect as StringRef (for API usage)
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.
void add_effects(const std::vector< LightEffect * > &effects)
Add effects for this light state.
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.
std::vector< LightEffect * > effects_
List of effects for this light.
void add_new_target_state_reached_callback(std::function< void()> &&send_callback)
The callback is called once the state of current_values and remote_values are equal (when the transit...
void current_values_as_rgb(float *red, float *green, float *blue, bool color_interlock=false)
CallbackManager< void()> target_state_reached_callback_
Callback to call when the state of current_values and remote_values are equal This should be called o...
bool supports_effects()
Return whether the light has any effects that meet the trait requirements.
void current_values_as_rgbw(float *red, float *green, float *blue, float *white, bool color_interlock=false)
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.
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.
Definition light_state.h:99
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.
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.
This class is used to represent the capabilities of a light.
@ LIGHT_RESTORE_INVERTED_DEFAULT_ON
Definition light_state.h:28
@ LIGHT_RESTORE_DEFAULT_OFF
Definition light_state.h:23
@ LIGHT_RESTORE_INVERTED_DEFAULT_OFF
Definition light_state.h:27
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).
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
float gamma_correct(float value, float gamma)
Applies gamma correction of gamma to value.
Definition helpers.cpp:494
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:34
uint16_t length
Definition tt21100.cpp:0