ESPHome  2024.11.2
light_state.h
Go to the documentation of this file.
1 #pragma once
2 
7 #include "light_call.h"
8 #include "light_color_values.h"
9 #include "light_effect.h"
10 #include "light_traits.h"
11 #include "light_transformer.h"
12 
13 #include <vector>
14 
15 namespace esphome {
16 namespace light {
17 
18 class LightOutput;
19 
29 };
30 
33  float blue, float white, float color_temp, float cold_white, float warm_white)
34  : color_mode(color_mode),
35  state(state),
36  brightness(brightness),
37  color_brightness(color_brightness),
38  red(red),
39  green(green),
40  blue(blue),
41  white(white),
42  color_temp(color_temp),
43  cold_white(cold_white),
44  warm_white(warm_white) {}
45  LightStateRTCState() = default;
47  bool state{false};
48  float brightness{1.0f};
49  float color_brightness{1.0f};
50  float red{1.0f};
51  float green{1.0f};
52  float blue{1.0f};
53  float white{1.0f};
54  float color_temp{1.0f};
55  float cold_white{1.0f};
56  float warm_white{1.0f};
57  uint32_t effect{0};
58 };
59 
63 class LightState : public EntityBase, public Component {
64  public:
65  LightState(LightOutput *output);
66 
67  LightTraits get_traits();
68 
70  LightCall turn_on();
71  LightCall turn_off();
72  LightCall toggle();
73  LightCall make_call();
74 
75  // ========== INTERNAL METHODS ==========
76  // (In most use cases you won't need these)
78  void setup() override;
79  void dump_config() override;
80  void loop() override;
82  float get_setup_priority() const override;
83 
95 
107 
109  void publish_state();
110 
112  LightOutput *get_output() const;
113 
115  std::string get_effect_name();
116 
123  void add_new_remote_values_callback(std::function<void()> &&send_callback);
124 
131  void add_new_target_state_reached_callback(std::function<void()> &&send_callback);
132 
134  void set_default_transition_length(uint32_t default_transition_length);
135  uint32_t get_default_transition_length() const;
136 
138  void set_flash_transition_length(uint32_t flash_transition_length);
139  uint32_t get_flash_transition_length() const;
140 
142  void set_gamma_correct(float gamma_correct);
143  float get_gamma_correct() const { return this->gamma_correct_; }
144 
146  void set_restore_mode(LightRestoreMode restore_mode);
147 
149  void set_initial_state(const LightStateRTCState &initial_state);
150 
152  bool supports_effects();
153 
155  const std::vector<LightEffect *> &get_effects() const;
156 
158  void add_effects(const std::vector<LightEffect *> &effects);
159 
161  void current_values_as_binary(bool *binary);
162 
163  void current_values_as_brightness(float *brightness);
164 
165  void current_values_as_rgb(float *red, float *green, float *blue, bool color_interlock = false);
166 
167  void current_values_as_rgbw(float *red, float *green, float *blue, float *white, bool color_interlock = false);
168 
169  void current_values_as_rgbww(float *red, float *green, float *blue, float *cold_white, float *warm_white,
170  bool constant_brightness = false);
171 
172  void current_values_as_rgbct(float *red, float *green, float *blue, float *color_temperature,
173  float *white_brightness);
174 
175  void current_values_as_cwww(float *cold_white, float *warm_white, bool constant_brightness = false);
176 
177  void current_values_as_ct(float *color_temperature, float *white_brightness);
178 
188  bool is_transformer_active();
189 
190  protected:
191  friend LightOutput;
192  friend LightCall;
193  friend class AddressableLight;
194 
196  void start_effect_(uint32_t effect_index);
198  LightEffect *get_active_effect_();
200  void stop_effect_();
202  void start_transition_(const LightColorValues &target, uint32_t length, bool set_remote_values);
203 
205  void start_flash_(const LightColorValues &target, uint32_t length, bool set_remote_values);
206 
208  void set_immediately_(const LightColorValues &target, bool set_remote_values);
209 
211  void save_remote_values_();
212 
216  uint32_t active_effect_index_{};
218  std::unique_ptr<LightTransformer> transformer_{nullptr};
220  bool next_write_{true};
221 
224 
232  CallbackManager<void()> remote_values_callback_{};
233 
237  CallbackManager<void()> target_state_reached_callback_{};
238 
240  uint32_t default_transition_length_{};
242  uint32_t flash_transition_length_{};
244  float gamma_correct_{};
250  std::vector<LightEffect *> effects_;
251 
252  // for effects, true if a transformer (transition) is active.
253  bool is_transformer_active_ = false;
254 };
255 
256 } // namespace light
257 } // namespace esphome
void setup()
ColorMode
Color modes are a combination of color capabilities that can be used at the same time.
Definition: color_mode.h:49
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:63
void loop()
ESPPreferenceObject rtc_
Object used to store the persisted values of the light.
Definition: light_state.h:223
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
float get_gamma_correct() const
Definition: light_state.h:143
LightRestoreMode restore_mode_
Restore mode of the light.
Definition: light_state.h:246
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.
This class represents a requested change in a light state.
Definition: light_call.h:14
std::vector< LightEffect * > effects_
List of effects for this light.
Definition: light_state.h:250
No color mode configured (cannot be a supported mode, only active when light is off).
This class is used to represent the capabilities of a light.
Definition: light_traits.h:11
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
LightColorValues remote_values
The remote color values reported to the frontend.
Definition: light_state.h:106
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:32