ESPHome  2024.11.1
light_state.cpp
Go to the documentation of this file.
1 #include "esphome/core/log.h"
2 
3 #include "light_output.h"
4 #include "light_state.h"
5 #include "transformers.h"
6 
7 namespace esphome {
8 namespace light {
9 
10 static const char *const TAG = "light";
11 
12 LightState::LightState(LightOutput *output) : output_(output) {}
13 
15 LightCall LightState::turn_on() { return this->make_call().set_state(true); }
16 LightCall LightState::turn_off() { return this->make_call().set_state(false); }
19 
21  ESP_LOGCONFIG(TAG, "Setting up light '%s'...", this->get_name().c_str());
22 
23  this->output_->setup_state(this);
24  for (auto *effect : this->effects_) {
25  effect->init_internal(this);
26  }
27 
28  // When supported color temperature range is known, initialize color temperature setting within bounds.
29  float min_mireds = this->get_traits().get_min_mireds();
30  if (min_mireds > 0) {
31  this->remote_values.set_color_temperature(min_mireds);
32  this->current_values.set_color_temperature(min_mireds);
33  }
34 
35  auto call = this->make_call();
36  LightStateRTCState recovered{};
37  if (this->initial_state_.has_value()) {
38  recovered = *this->initial_state_;
39  }
40  switch (this->restore_mode_) {
46  // Attempt to load from preferences, else fall back to default values
47  if (!this->rtc_.load(&recovered)) {
48  recovered.state = false;
51  recovered.state = true;
52  }
55  // Inverted restore state
56  recovered.state = !recovered.state;
57  }
58  break;
62  this->rtc_.load(&recovered);
63  recovered.state = (this->restore_mode_ == LIGHT_RESTORE_AND_ON);
64  break;
65  case LIGHT_ALWAYS_OFF:
66  recovered.state = false;
67  break;
68  case LIGHT_ALWAYS_ON:
69  recovered.state = true;
70  break;
71  }
72 
73  call.set_color_mode_if_supported(recovered.color_mode);
74  call.set_state(recovered.state);
75  call.set_brightness_if_supported(recovered.brightness);
76  call.set_color_brightness_if_supported(recovered.color_brightness);
77  call.set_red_if_supported(recovered.red);
78  call.set_green_if_supported(recovered.green);
79  call.set_blue_if_supported(recovered.blue);
80  call.set_white_if_supported(recovered.white);
81  call.set_color_temperature_if_supported(recovered.color_temp);
82  call.set_cold_white_if_supported(recovered.cold_white);
83  call.set_warm_white_if_supported(recovered.warm_white);
84  if (recovered.effect != 0) {
85  call.set_effect(recovered.effect);
86  } else {
87  call.set_transition_length_if_supported(0);
88  }
89  call.perform();
90 }
92  ESP_LOGCONFIG(TAG, "Light '%s'", this->get_name().c_str());
93  if (this->get_traits().supports_color_capability(ColorCapability::BRIGHTNESS)) {
94  ESP_LOGCONFIG(TAG, " Default Transition Length: %.1fs", this->default_transition_length_ / 1e3f);
95  ESP_LOGCONFIG(TAG, " Gamma Correct: %.2f", this->gamma_correct_);
96  }
97  if (this->get_traits().supports_color_capability(ColorCapability::COLOR_TEMPERATURE)) {
98  ESP_LOGCONFIG(TAG, " Min Mireds: %.1f", this->get_traits().get_min_mireds());
99  ESP_LOGCONFIG(TAG, " Max Mireds: %.1f", this->get_traits().get_max_mireds());
100  }
101 }
103  // Apply effect (if any)
104  auto *effect = this->get_active_effect_();
105  if (effect != nullptr) {
106  effect->apply();
107  }
108 
109  // Apply transformer (if any)
110  if (this->transformer_ != nullptr) {
111  auto values = this->transformer_->apply();
112  this->is_transformer_active_ = true;
113  if (values.has_value()) {
114  this->current_values = *values;
115  this->output_->update_state(this);
116  this->next_write_ = true;
117  }
118 
119  if (this->transformer_->is_finished()) {
120  // if the transition has written directly to the output, current_values is outdated, so update it
121  this->current_values = this->transformer_->get_target_values();
122 
123  this->transformer_->stop();
124  this->is_transformer_active_ = false;
125  this->transformer_ = nullptr;
126  this->target_state_reached_callback_.call();
127  }
128  }
129 
130  // Write state to the light
131  if (this->next_write_) {
132  this->next_write_ = false;
133  this->output_->write_state(this);
134  }
135 }
136 
138 
140 
141 LightOutput *LightState::get_output() const { return this->output_; }
143  if (this->active_effect_index_ > 0) {
144  return this->effects_[this->active_effect_index_ - 1]->get_name();
145  } else {
146  return "None";
147  }
148 }
149 
150 void LightState::add_new_remote_values_callback(std::function<void()> &&send_callback) {
151  this->remote_values_callback_.add(std::move(send_callback));
152 }
153 void LightState::add_new_target_state_reached_callback(std::function<void()> &&send_callback) {
154  this->target_state_reached_callback_.add(std::move(send_callback));
155 }
156 
157 void LightState::set_default_transition_length(uint32_t default_transition_length) {
158  this->default_transition_length_ = default_transition_length;
159 }
161 void LightState::set_flash_transition_length(uint32_t flash_transition_length) {
162  this->flash_transition_length_ = flash_transition_length;
163 }
166 void LightState::set_restore_mode(LightRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
167 void LightState::set_initial_state(const LightStateRTCState &initial_state) { this->initial_state_ = initial_state; }
168 bool LightState::supports_effects() { return !this->effects_.empty(); }
169 const std::vector<LightEffect *> &LightState::get_effects() const { return this->effects_; }
170 void LightState::add_effects(const std::vector<LightEffect *> &effects) {
171  this->effects_.reserve(this->effects_.size() + effects.size());
172  for (auto *effect : effects) {
173  this->effects_.push_back(effect);
174  }
175 }
176 
177 void LightState::current_values_as_binary(bool *binary) { this->current_values.as_binary(binary); }
179  this->current_values.as_brightness(brightness, this->gamma_correct_);
180 }
181 void LightState::current_values_as_rgb(float *red, float *green, float *blue, bool color_interlock) {
182  auto traits = this->get_traits();
183  this->current_values.as_rgb(red, green, blue, this->gamma_correct_, false);
184 }
185 void LightState::current_values_as_rgbw(float *red, float *green, float *blue, float *white, bool color_interlock) {
186  auto traits = this->get_traits();
187  this->current_values.as_rgbw(red, green, blue, white, this->gamma_correct_, false);
188 }
189 void LightState::current_values_as_rgbww(float *red, float *green, float *blue, float *cold_white, float *warm_white,
190  bool constant_brightness) {
191  this->current_values.as_rgbww(red, green, blue, cold_white, warm_white, this->gamma_correct_, constant_brightness);
192 }
193 void LightState::current_values_as_rgbct(float *red, float *green, float *blue, float *color_temperature,
194  float *white_brightness) {
195  auto traits = this->get_traits();
196  this->current_values.as_rgbct(traits.get_min_mireds(), traits.get_max_mireds(), red, green, blue, color_temperature,
197  white_brightness, this->gamma_correct_);
198 }
199 void LightState::current_values_as_cwww(float *cold_white, float *warm_white, bool constant_brightness) {
200  auto traits = this->get_traits();
201  this->current_values.as_cwww(cold_white, warm_white, this->gamma_correct_, constant_brightness);
202 }
203 void LightState::current_values_as_ct(float *color_temperature, float *white_brightness) {
204  auto traits = this->get_traits();
205  this->current_values.as_ct(traits.get_min_mireds(), traits.get_max_mireds(), color_temperature, white_brightness,
206  this->gamma_correct_);
207 }
208 
210 
211 void LightState::start_effect_(uint32_t effect_index) {
212  this->stop_effect_();
213  if (effect_index == 0)
214  return;
215 
216  this->active_effect_index_ = effect_index;
217  auto *effect = this->get_active_effect_();
218  effect->start_internal();
219 }
221  if (this->active_effect_index_ == 0) {
222  return nullptr;
223  } else {
224  return this->effects_[this->active_effect_index_ - 1];
225  }
226 }
228  auto *effect = this->get_active_effect_();
229  if (effect != nullptr) {
230  effect->stop();
231  }
232  this->active_effect_index_ = 0;
233 }
234 
235 void LightState::start_transition_(const LightColorValues &target, uint32_t length, bool set_remote_values) {
237  this->transformer_->setup(this->current_values, target, length);
238 
239  if (set_remote_values) {
240  this->remote_values = target;
241  }
242 }
243 
244 void LightState::start_flash_(const LightColorValues &target, uint32_t length, bool set_remote_values) {
245  LightColorValues end_colors = this->remote_values;
246  // If starting a flash if one is already happening, set end values to end values of current flash
247  // Hacky but works
248  if (this->transformer_ != nullptr)
249  end_colors = this->transformer_->get_start_values();
250 
251  this->transformer_ = make_unique<LightFlashTransformer>(*this);
252  this->transformer_->setup(end_colors, target, length);
253 
254  if (set_remote_values) {
255  this->remote_values = target;
256  };
257 }
258 
259 void LightState::set_immediately_(const LightColorValues &target, bool set_remote_values) {
260  this->is_transformer_active_ = false;
261  this->transformer_ = nullptr;
262  this->current_values = target;
263  if (set_remote_values) {
264  this->remote_values = target;
265  }
266  this->output_->update_state(this);
267  this->next_write_ = true;
268 }
269 
271  LightStateRTCState saved;
272  saved.color_mode = this->remote_values.get_color_mode();
273  switch (this->restore_mode_) {
276  saved.state = (this->restore_mode_ == LIGHT_RESTORE_AND_ON);
277  break;
278  default:
279  saved.state = this->remote_values.is_on();
280  break;
281  }
282  saved.brightness = this->remote_values.get_brightness();
284  saved.red = this->remote_values.get_red();
285  saved.green = this->remote_values.get_green();
286  saved.blue = this->remote_values.get_blue();
287  saved.white = this->remote_values.get_white();
289  saved.cold_white = this->remote_values.get_cold_white();
290  saved.warm_white = this->remote_values.get_warm_white();
291  saved.effect = this->active_effect_index_;
292  this->rtc_.save(&saved);
293 }
294 
295 } // namespace light
296 } // namespace esphome
void current_values_as_rgbww(float *red, float *green, float *blue, float *cold_white, float *warm_white, bool constant_brightness=false)
float gamma_correct_
Gamma correction factor for the light.
Definition: light_state.h:244
ESPPreferenceObject rtc_
Object used to store the persisted values of the light.
Definition: light_state.h:223
void set_flash_transition_length(uint32_t flash_transition_length)
Set the flash transition length.
float get_warm_white() const
Get the warm white property of these light color values. In range 0.0 to 1.0.
uint32_t get_flash_transition_length() const
void publish_state()
Publish the currently active state to the frontend.
std::unique_ptr< LightTransformer > transformer_
The currently active transformer for this light (transition/flash).
Definition: light_state.h:218
void set_immediately_(const LightColorValues &target, bool set_remote_values)
Internal method to set the color values to target immediately (with no transition).
bool is_on() const
Get the binary true/false state of these light color values.
uint32_t default_transition_length_
Default transition length for all transitions in ms.
Definition: light_state.h:240
Interface to write LightStates to hardware.
Definition: light_output.h:12
LightColorValues current_values
The current values of the light as outputted to the light.
Definition: light_state.h:94
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...
std::string get_effect_name()
Return the name of the current effect, or if no effect is active "None".
LightCall turn_on()
Make a light state call.
Definition: light_state.cpp:15
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.
void as_cwww(float *cold_white, float *warm_white, float gamma=0, 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 start_effect_(uint32_t effect_index)
Internal method to start an effect with the given index.
LightRestoreMode restore_mode_
Restore mode of the light.
Definition: light_state.h:246
LightOutput * get_output() const
Get the light output associated with this object.
float get_min_mireds() const
Definition: light_traits.h:49
void as_rgbct(float color_temperature_cw, float color_temperature_ww, float *red, float *green, float *blue, float *color_temperature, float *white_brightness, float gamma=0) const
Convert these light color values to an RGB+CT+BR representation with the given parameters.
bool supports_effects()
Return whether the light has any effects that meet the trait requirements.
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.
float get_red() const
Get the red property of these light color values. In range 0.0 to 1.0.
virtual std::unique_ptr< LightTransformer > create_default_transition()
Return the default transformer used for transitions.
Definition: light_output.cpp:7
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...
Color temperature can be controlled.
void setup() override
Load state from preferences.
Definition: light_state.cpp:20
void dump_config() override
Definition: light_state.cpp:91
void current_values_as_binary(bool *binary)
The result of all the current_values_as_* methods have gamma correction applied.
bool save(const T *src)
Definition: preferences.h:21
bool next_write_
Whether the light value should be written in the next cycle.
Definition: light_state.h:220
void current_values_as_rgb(float *red, float *green, float *blue, bool color_interlock=false)
float get_blue() const
Get the blue property of these light color values. In range 0.0 to 1.0.
uint32_t flash_transition_length_
Transition length to use for flash transitions.
Definition: light_state.h:242
LightState(LightOutput *output)
Definition: light_state.cpp:12
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...
LightOutput * output_
Store the output to allow effects to have more access.
Definition: light_state.h:214
float gamma_correct(float value, float gamma)
Applies gamma correction of gamma to value.
Definition: helpers.cpp:542
This class represents the color state for a light object.
void current_values_as_rgbct(float *red, float *green, float *blue, float *color_temperature, float *white_brightness)
LightEffect * get_active_effect_()
Internal method to get the currently active effect.
float get_white() const
Get the white property of these light color values. In range 0.0 to 1.0.
ESPPreferences * global_preferences
float get_color_temperature() const
Get the color temperature property of these light color values in mired.
void add_effects(const std::vector< LightEffect *> &effects)
Add effects for this light state.
void current_values_as_brightness(float *brightness)
uint32_t get_default_transition_length() const
void set_gamma_correct(float gamma_correct)
Set the gamma correction factor.
const std::vector< LightEffect * > & get_effects() const
Get all effects for this light state.
This class represents a requested change in a light state.
Definition: light_call.h:14
ColorMode get_color_mode() const
Get the color mode of these light color values.
LightCall & set_state(optional< bool > state)
Set the binary ON/OFF state of the light.
Definition: light_call.cpp:552
std::vector< LightEffect * > effects_
List of effects for this light.
Definition: light_state.h:250
Master brightness of the light can be controlled.
void current_values_as_ct(float *color_temperature, float *white_brightness)
void current_values_as_cwww(float *cold_white, float *warm_white, bool constant_brightness=false)
float get_setup_priority() const override
Shortly after HARDWARE.
void set_initial_state(const LightStateRTCState &initial_state)
Set the initial state of this light.
CallbackManager< void()> remote_values_callback_
Callback to call when new values for the frontend are available.
Definition: light_state.h:232
virtual void update_state(LightState *state)
Called on every update of the current values of the associated LightState, can optionally be used to ...
Definition: light_output.h:24
This class is used to represent the capabilities of a light.
Definition: light_traits.h:11
void as_rgbww(float *red, float *green, float *blue, float *cold_white, float *warm_white, float gamma=0, bool constant_brightness=false) const
Convert these light color values to an RGBWW representation with the given parameters.
void add_new_remote_values_callback(std::function< void()> &&send_callback)
This lets front-end components subscribe to light change events.
void as_ct(float color_temperature_cw, float color_temperature_ww, float *color_temperature, float *white_brightness, float gamma=0) const
Convert these light color values to a CT+BR representation with the given parameters.
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
void set_restore_mode(LightRestoreMode restore_mode)
Set the restore mode of this light.
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
void set_color_temperature(float color_temperature)
Set the color temperature property of these light color values in mired.
void save_remote_values_()
Internal method to save the current remote_values to the preferences.
void as_binary(bool *binary) const
Convert these light color values to a binary representation and write them to binary.
virtual void setup_state(LightState *state)
Definition: light_output.h:20
uint16_t length
Definition: tt21100.cpp:12
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...
Definition: light_state.h:237
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
optional< LightStateRTCState > initial_state_
Initial state of the light.
Definition: light_state.h:248
uint32_t active_effect_index_
Value for storing the index of the currently active effect. 0 if no effect is active.
Definition: light_state.h:216
LightColorValues remote_values
The remote color values reported to the frontend.
Definition: light_state.h:106
virtual LightTraits get_traits()=0
Return the LightTraits of this LightOutput.
float get_color_brightness() const
Get the color brightness property of these light color values. In range 0.0 to 1.0.
void as_rgbw(float *red, float *green, float *blue, float *white, float gamma=0, bool color_interlock=false) const
Convert these light color values to an RGBW representation and write them to red, green...
bool is_transformer_active()
Indicator if a transformer (e.g.
void as_rgb(float *red, float *green, float *blue, float gamma=0, bool color_interlock=false) const
Convert these light color values to an RGB representation and write them to red, green, blue.
void as_brightness(float *brightness, float gamma=0) const
Convert these light color values to a brightness-only representation and write them to brightness...
void stop_effect_()
Internal method to stop the current effect (if one is active).
uint32_t get_object_id_hash()
Definition: entity_base.cpp:76
float get_green() const
Get the green property of these light color values. In range 0.0 to 1.0.
const StringRef & get_name() const
Definition: entity_base.cpp:10
float get_brightness() const
Get the brightness property of these light color values. In range 0.0 to 1.0.
void current_values_as_rgbw(float *red, float *green, float *blue, float *white, bool color_interlock=false)