ESPHome  2022.12.8
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 
34 class LightState : public EntityBase, public Component {
35  public:
37  LightState(const std::string &name, LightOutput *output);
38 
39  LightState(LightOutput *output);
40 
42 
46  LightCall toggle();
48 
49  // ========== INTERNAL METHODS ==========
50  // (In most use cases you won't need these)
52  void setup() override;
53  void dump_config() override;
54  void loop() override;
56  float get_setup_priority() const override;
57 
69 
81 
83  void publish_state();
84 
86  LightOutput *get_output() const;
87 
89  std::string get_effect_name();
90 
97  void add_new_remote_values_callback(std::function<void()> &&send_callback);
98 
105  void add_new_target_state_reached_callback(std::function<void()> &&send_callback);
106 
108  void set_default_transition_length(uint32_t default_transition_length);
109  uint32_t get_default_transition_length() const;
110 
112  void set_flash_transition_length(uint32_t flash_transition_length);
113  uint32_t get_flash_transition_length() const;
114 
116  void set_gamma_correct(float gamma_correct);
117  float get_gamma_correct() const { return this->gamma_correct_; }
118 
120  void set_restore_mode(LightRestoreMode restore_mode);
121 
123  bool supports_effects();
124 
126  const std::vector<LightEffect *> &get_effects() const;
127 
129  void add_effects(const std::vector<LightEffect *> &effects);
130 
132  void current_values_as_binary(bool *binary);
133 
134  void current_values_as_brightness(float *brightness);
135 
136  void current_values_as_rgb(float *red, float *green, float *blue, bool color_interlock = false);
137 
138  void current_values_as_rgbw(float *red, float *green, float *blue, float *white, bool color_interlock = false);
139 
140  void current_values_as_rgbww(float *red, float *green, float *blue, float *cold_white, float *warm_white,
141  bool constant_brightness = false);
142 
143  void current_values_as_rgbct(float *red, float *green, float *blue, float *color_temperature,
144  float *white_brightness);
145 
146  void current_values_as_cwww(float *cold_white, float *warm_white, bool constant_brightness = false);
147 
148  void current_values_as_ct(float *color_temperature, float *white_brightness);
149 
150  protected:
151  friend LightOutput;
152  friend LightCall;
153  friend class AddressableLight;
154 
156  void start_effect_(uint32_t effect_index);
160  void stop_effect_();
162  void start_transition_(const LightColorValues &target, uint32_t length, bool set_remote_values);
163 
165  void start_flash_(const LightColorValues &target, uint32_t length, bool set_remote_values);
166 
168  void set_immediately_(const LightColorValues &target, bool set_remote_values);
169 
171  void save_remote_values_();
172 
178  std::unique_ptr<LightTransformer> transformer_{nullptr};
180  bool next_write_{true};
181 
184 
193 
198 
204  float gamma_correct_{};
208  std::vector<LightEffect *> effects_;
209 };
210 
211 } // namespace light
212 } // 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:204
const char * name
Definition: stm32flash.h:78
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:34
ESPPreferenceObject rtc_
Object used to store the persisted values of the light.
Definition: light_state.h:183
void set_flash_transition_length(uint32_t flash_transition_length)
Set the flash transition length.
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:178
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 default_transition_length_
Default transition length for all transitions in ms.
Definition: light_state.h:200
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:68
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.
float get_gamma_correct() const
Definition: light_state.h:117
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:206
LightOutput * get_output() const
Get the light output associated with this object.
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.
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 setup() override
Load state from preferences.
Definition: light_state.cpp:35
void dump_config() override
void current_values_as_binary(bool *binary)
The result of all the current_values_as_* methods have gamma correction applied.
bool next_write_
Whether the light value should be written in the next cycle.
Definition: light_state.h:180
void current_values_as_rgb(float *red, float *green, float *blue, bool color_interlock=false)
uint32_t flash_transition_length_
Transition length to use for flash transitions.
Definition: light_state.h:202
LightOutput * output_
Store the output to allow effects to have more access.
Definition: light_state.h:174
float gamma_correct(float value, float gamma)
Applies gamma correction of gamma to value.
Definition: helpers.cpp:314
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.
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
std::vector< LightEffect * > effects_
List of effects for this light.
Definition: light_state.h:208
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.
CallbackManager< void()> remote_values_callback_
Callback to call when new values for the frontend are available.
Definition: light_state.h:192
This class is used to represent the capabilities of a light.
Definition: light_traits.h:11
void add_new_remote_values_callback(std::function< void()> &&send_callback)
This lets front-end components subscribe to light change events.
void set_restore_mode(LightRestoreMode restore_mode)
Set the restore mode of this light.
void save_remote_values_()
Internal method to save the current remote_values to the preferences.
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:197
Definition: a4988.cpp:4
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:176
LightColorValues remote_values
The remote color values reported to the frontend.
Definition: light_state.h:80
LightState(const std::string &name, LightOutput *output)
Construct this LightState using the provided traits and name.
Definition: light_state.cpp:11
void stop_effect_()
Internal method to stop the current effect (if one is active).
void current_values_as_rgbw(float *red, float *green, float *blue, float *white, bool color_interlock=false)