ESPHome  1.14.3
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
esphome::light::LightState Class Reference

This class represents the communication layer between the front-end MQTT layer and the hardware output layer. More...

#include <light_state.h>

Inheritance diagram for esphome::light::LightState:
esphome::Nameable esphome::Component

Public Member Functions

 LightState (const std::string &name, LightOutput *output)
 Construct this LightState using the provided traits and name. More...
 
LightTraits get_traits ()
 
LightCall turn_on ()
 Make a light state call. More...
 
LightCall turn_off ()
 
LightCall toggle ()
 
LightCall make_call ()
 
void setup () override
 Load state from preferences. More...
 
void dump_config () override
 
void loop () override
 
float get_setup_priority () const override
 Shortly after HARDWARE. More...
 
 ESPDEPRECATED ("get_current_values() is deprecated, please use .current_values instead.") LightColorValues get_current_values()
 Deprecated method to access current_values. More...
 
 ESPDEPRECATED ("get_remote_values() is deprecated, please use .remote_values instead.") LightColorValues get_remote_values()
 Deprecated method to access remote_values. More...
 
void publish_state ()
 Publish the currently active state to the frontend. More...
 
LightOutputget_output () const
 Get the light output associated with this object. More...
 
std::string get_effect_name ()
 Return the name of the current effect, or if no effect is active "None". More...
 
void add_new_remote_values_callback (std::function< void()> &&send_callback)
 This lets front-end components subscribe to light change events. More...
 
bool supports_effects ()
 Return whether the light has any effects that meet the trait requirements. More...
 
void dump_json (JsonObject &root)
 Dump the state of this light as JSON. More...
 
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. More...
 
void set_gamma_correct (float gamma_correct)
 Set the gamma correction factor. More...
 
float get_gamma_correct () const
 
void set_restore_mode (LightRestoreMode restore_mode)
 
const std::vector< LightEffect * > & get_effects () const
 
void add_effects (std::vector< LightEffect *> effects)
 
void current_values_as_binary (bool *binary)
 
void current_values_as_brightness (float *brightness)
 
void current_values_as_rgb (float *red, float *green, float *blue)
 
void current_values_as_rgbw (float *red, float *green, float *blue, float *white)
 
void current_values_as_rgbww (float *red, float *green, float *blue, float *cold_white, float *warm_white)
 
void current_values_as_cwww (float *cold_white, float *warm_white)
 
- Public Member Functions inherited from esphome::Nameable
 Nameable ()
 
 Nameable (const std::string &name)
 
const std::string & get_name () const
 
void set_name (const std::string &name)
 
const std::string & get_object_id ()
 Get the sanitized name of this nameable as an ID. Caching it internally. More...
 
uint32_t get_object_id_hash ()
 
bool is_internal () const
 
void set_internal (bool internal)
 
- Public Member Functions inherited from esphome::Component
float get_actual_setup_priority () const
 
void set_setup_priority (float priority)
 
virtual float get_loop_priority () const
 priority of loop(). More...
 
void call ()
 
virtual void on_shutdown ()
 
virtual void on_safe_shutdown ()
 
uint32_t get_component_state () const
 
virtual void mark_failed ()
 Mark this component as failed. More...
 
bool is_failed ()
 
virtual bool can_proceed ()
 
bool status_has_warning ()
 
bool status_has_error ()
 
void status_set_warning ()
 
void status_set_error ()
 
void status_clear_warning ()
 
void status_clear_error ()
 
void status_momentary_warning (const std::string &name, uint32_t length=5000)
 
void status_momentary_error (const std::string &name, uint32_t length=5000)
 

Data Fields

LightColorValues current_values
 The current values of the light as outputted to the light. More...
 
LightColorValues remote_values
 The remote color values reported to the frontend. More...
 

Protected Member Functions

uint32_t hash_base () override
 
void start_effect_ (uint32_t effect_index)
 Internal method to start an effect with the given index. More...
 
void stop_effect_ ()
 Internal method to stop the current effect (if one is active). More...
 
void start_transition_ (const LightColorValues &target, uint32_t length)
 Internal method to start a transition to the target color with the given length. More...
 
void start_flash_ (const LightColorValues &target, uint32_t length)
 Internal method to start a flash for the specified amount of time. More...
 
void set_immediately_ (const LightColorValues &target, bool set_remote_values)
 Internal method to set the color values to target immediately (with no transition). More...
 
void set_transformer_ (std::unique_ptr< LightTransformer > transformer)
 Internal method to start a transformer. More...
 
LightEffectget_active_effect_ ()
 
- Protected Member Functions inherited from esphome::Nameable
void calc_object_id_ ()
 
- Protected Member Functions inherited from esphome::Component
virtual void call_loop ()
 
virtual void call_setup ()
 
void set_interval (const std::string &name, uint32_t interval, std::function< void()> &&f)
 Set an interval function with a unique name. More...
 
void set_interval (uint32_t interval, std::function< void()> &&f)
 
bool cancel_interval (const std::string &name)
 Cancel an interval function. More...
 
void set_timeout (uint32_t timeout, std::function< void()> &&f)
 
void set_timeout (const std::string &name, uint32_t timeout, std::function< void()> &&f)
 Set a timeout function with a unique name. More...
 
bool cancel_timeout (const std::string &name)
 Cancel a timeout function. More...
 
void defer (const std::string &name, std::function< void()> &&f)
 Defer a callback to the next loop() call. More...
 
void defer (std::function< void()> &&f)
 Defer a callback to the next loop() call. More...
 
bool cancel_defer (const std::string &name)
 Cancel a defer callback using the specified name, name must not be empty. More...
 

Protected Attributes

friend LightOutput
 
friend LightCall
 
ESPPreferenceObject rtc_
 Object used to store the persisted values of the light. More...
 
LightRestoreMode restore_mode_
 Restore mode of the light. More...
 
uint32_t default_transition_length_ {}
 Default transition length for all transitions in ms. More...
 
uint32_t active_effect_index_ {}
 Value for storing the index of the currently active effect. 0 if no effect is active. More...
 
std::unique_ptr< LightTransformertransformer_ {nullptr}
 The currently active transformer for this light (transition/flash). More...
 
CallbackManager< void()> remote_values_callback_ {}
 Callback to call when new values for the frontend are available. More...
 
LightOutputoutput_
 Store the output to allow effects to have more access. More...
 
bool next_write_ {true}
 Whether the light value should be written in the next cycle. More...
 
float gamma_correct_ {}
 Gamma correction factor for the light. More...
 
std::vector< LightEffect * > effects_
 List of effects for this light. More...
 
- Protected Attributes inherited from esphome::Nameable
std::string name_
 
std::string object_id_
 
uint32_t object_id_hash_
 
bool internal_ {false}
 
- Protected Attributes inherited from esphome::Component
uint32_t component_state_ {0x0000}
 State of this component. More...
 
float setup_priority_override_ {NAN}
 

Detailed Description

This class represents the communication layer between the front-end MQTT layer and the hardware output layer.

Definition at line 173 of file light_state.h.

Constructor & Destructor Documentation

◆ LightState()

esphome::light::LightState::LightState ( const std::string &  name,
LightOutput output 
)

Construct this LightState using the provided traits and name.

Definition at line 25 of file light_state.cpp.

Member Function Documentation

◆ add_effects()

void esphome::light::LightState::add_effects ( std::vector< LightEffect *>  effects)

Definition at line 165 of file light_state.cpp.

◆ add_new_remote_values_callback()

void esphome::light::LightState::add_new_remote_values_callback ( std::function< void()> &&  send_callback)

This lets front-end components subscribe to light change events.

This is different from add_new_current_values_callback in that it only sends events for start and end values. For example, with transitions it will only send a single callback whereas the callback passed in add_new_current_values_callback will be called every loop() cycle when a transition is active

Note the callback should get the output values through get_remote_values().

Parameters
send_callbackThe callback.

Definition at line 709 of file light_state.cpp.

◆ current_values_as_binary()

void esphome::light::LightState::current_values_as_binary ( bool *  binary)

Definition at line 675 of file light_state.cpp.

◆ current_values_as_brightness()

void esphome::light::LightState::current_values_as_brightness ( float *  brightness)

Definition at line 676 of file light_state.cpp.

◆ current_values_as_cwww()

void esphome::light::LightState::current_values_as_cwww ( float *  cold_white,
float *  warm_white 
)

Definition at line 703 of file light_state.cpp.

◆ current_values_as_rgb()

void esphome::light::LightState::current_values_as_rgb ( float *  red,
float *  green,
float *  blue 
)

Definition at line 680 of file light_state.cpp.

◆ current_values_as_rgbw()

void esphome::light::LightState::current_values_as_rgbw ( float *  red,
float *  green,
float *  blue,
float *  white 
)

Definition at line 686 of file light_state.cpp.

◆ current_values_as_rgbww()

void esphome::light::LightState::current_values_as_rgbww ( float *  red,
float *  green,
float *  blue,
float *  cold_white,
float *  warm_white 
)

Definition at line 693 of file light_state.cpp.

◆ dump_config()

void esphome::light::LightState::dump_config ( )
overridevirtual

Reimplemented from esphome::Component.

Definition at line 176 of file light_state.cpp.

◆ dump_json()

void esphome::light::LightState::dump_json ( JsonObject &  root)

Dump the state of this light as JSON.

Definition at line 78 of file light_state.cpp.

◆ ESPDEPRECATED() [1/2]

esphome::light::LightState::ESPDEPRECATED ( "get_current_values() is  deprecated,
please use .current_values instead."   
)

Deprecated method to access current_values.

◆ ESPDEPRECATED() [2/2]

esphome::light::LightState::ESPDEPRECATED ( "get_remote_values() is  deprecated,
please use .remote_values instead."   
)

Deprecated method to access remote_values.

◆ get_active_effect_()

LightEffect * esphome::light::LightState::get_active_effect_ ( )
protected

Definition at line 712 of file light_state.cpp.

◆ get_effect_name()

std::string esphome::light::LightState::get_effect_name ( )

Return the name of the current effect, or if no effect is active "None".

Definition at line 45 of file light_state.cpp.

◆ get_effects()

const std::vector< LightEffect * > & esphome::light::LightState::get_effects ( ) const

Definition at line 164 of file light_state.cpp.

◆ get_gamma_correct()

float esphome::light::LightState::get_gamma_correct ( ) const
inline

Definition at line 258 of file light_state.h.

◆ get_output()

LightOutput * esphome::light::LightState::get_output ( ) const

Get the light output associated with this object.

Definition at line 673 of file light_state.cpp.

◆ get_setup_priority()

float esphome::light::LightState::get_setup_priority ( ) const
overridevirtual

Shortly after HARDWARE.

Reimplemented from esphome::Component.

Definition at line 672 of file light_state.cpp.

◆ get_traits()

LightTraits esphome::light::LightState::get_traits ( )

Definition at line 163 of file light_state.cpp.

◆ hash_base()

uint32_t esphome::light::LightState::hash_base ( )
overrideprotectedvirtual

Implements esphome::Nameable.

Definition at line 175 of file light_state.cpp.

◆ loop()

void esphome::light::LightState::loop ( )
overridevirtual

Reimplemented from esphome::Component.

Definition at line 137 of file light_state.cpp.

◆ make_call()

LightCall esphome::light::LightState::make_call ( )

Definition at line 174 of file light_state.cpp.

◆ publish_state()

void esphome::light::LightState::publish_state ( )

Publish the currently active state to the frontend.

Definition at line 38 of file light_state.cpp.

◆ set_default_transition_length()

void esphome::light::LightState::set_default_transition_length ( uint32_t  default_transition_length)

Set the default transition length, i.e. the transition length when no transition is provided.

Definition at line 74 of file light_state.cpp.

◆ set_gamma_correct()

void esphome::light::LightState::set_gamma_correct ( float  gamma_correct)

Set the gamma correction factor.

Definition at line 674 of file light_state.cpp.

◆ set_immediately_()

void esphome::light::LightState::set_immediately_ ( const LightColorValues target,
bool  set_remote_values 
)
protected

Internal method to set the color values to target immediately (with no transition).

Definition at line 27 of file light_state.cpp.

◆ set_restore_mode()

void esphome::light::LightState::set_restore_mode ( LightRestoreMode  restore_mode)
inline

Definition at line 259 of file light_state.h.

◆ set_transformer_()

void esphome::light::LightState::set_transformer_ ( std::unique_ptr< LightTransformer transformer)
protected

Internal method to start a transformer.

Definition at line 63 of file light_state.cpp.

◆ setup()

void esphome::light::LightState::setup ( )
overridevirtual

Load state from preferences.

Reimplemented from esphome::Component.

Definition at line 96 of file light_state.cpp.

◆ start_effect_()

void esphome::light::LightState::start_effect_ ( uint32_t  effect_index)
protected

Internal method to start an effect with the given index.

Definition at line 52 of file light_state.cpp.

◆ start_flash_()

void esphome::light::LightState::start_flash_ ( const LightColorValues target,
uint32_t  length 
)
protected

Internal method to start a flash for the specified amount of time.

Definition at line 15 of file light_state.cpp.

◆ start_transition_()

void esphome::light::LightState::start_transition_ ( const LightColorValues target,
uint32_t  length 
)
protected

Internal method to start a transition to the target color with the given length.

Definition at line 10 of file light_state.cpp.

◆ stop_effect_()

void esphome::light::LightState::stop_effect_ ( )
protected

Internal method to stop the current effect (if one is active).

Definition at line 66 of file light_state.cpp.

◆ supports_effects()

bool esphome::light::LightState::supports_effects ( )

Return whether the light has any effects that meet the trait requirements.

Definition at line 62 of file light_state.cpp.

◆ toggle()

LightCall esphome::light::LightState::toggle ( )

Definition at line 173 of file light_state.cpp.

◆ turn_off()

LightCall esphome::light::LightState::turn_off ( )

Definition at line 172 of file light_state.cpp.

◆ turn_on()

LightCall esphome::light::LightState::turn_on ( )

Make a light state call.

Definition at line 171 of file light_state.cpp.

Field Documentation

◆ active_effect_index_

uint32_t esphome::light::LightState::active_effect_index_ {}
protected

Value for storing the index of the currently active effect. 0 if no effect is active.

Definition at line 309 of file light_state.h.

◆ current_values

LightColorValues esphome::light::LightState::current_values

The current values of the light as outputted to the light.

These values represent the "real" state of the light - During transitions this property will be changed continuously (in contrast to .remote_values, where they are constant during transitions).

This property is read-only for users. Any changes to it will be ignored.

Definition at line 203 of file light_state.h.

◆ default_transition_length_

uint32_t esphome::light::LightState::default_transition_length_ {}
protected

Default transition length for all transitions in ms.

Definition at line 307 of file light_state.h.

◆ effects_

std::vector<LightEffect *> esphome::light::LightState::effects_
protected

List of effects for this light.

Definition at line 326 of file light_state.h.

◆ gamma_correct_

float esphome::light::LightState::gamma_correct_ {}
protected

Gamma correction factor for the light.

Definition at line 324 of file light_state.h.

◆ LightCall

friend esphome::light::LightState::LightCall
protected

Definition at line 279 of file light_state.h.

◆ LightOutput

friend esphome::light::LightState::LightOutput
protected

Definition at line 278 of file light_state.h.

◆ next_write_

bool esphome::light::LightState::next_write_ {true}
protected

Whether the light value should be written in the next cycle.

Definition at line 322 of file light_state.h.

◆ output_

LightOutput* esphome::light::LightState::output_
protected

Store the output to allow effects to have more access.

Definition at line 320 of file light_state.h.

◆ remote_values

LightColorValues esphome::light::LightState::remote_values

The remote color values reported to the frontend.

These are different from the "current" values: For example transitions will continuously change the "current" values. But the remote values will immediately switch to the target value for a transition, reducing the number of packets sent.

This property is read-only for users. Any changes to it will be ignored.

Definition at line 221 of file light_state.h.

◆ remote_values_callback_

CallbackManager<void()> esphome::light::LightState::remote_values_callback_ {}
protected

Callback to call when new values for the frontend are available.

"Remote values" are light color values that are reported to the frontend and have a lower publish frequency than the "real" color values. For example, during transitions the current color value may change continuously, but the remote values will be reported as the target values starting with the beginning of the transition.

Definition at line 319 of file light_state.h.

◆ restore_mode_

LightRestoreMode esphome::light::LightState::restore_mode_
protected

Restore mode of the light.

Definition at line 305 of file light_state.h.

◆ rtc_

ESPPreferenceObject esphome::light::LightState::rtc_
protected

Object used to store the persisted values of the light.

Definition at line 303 of file light_state.h.

◆ transformer_

std::unique_ptr<LightTransformer> esphome::light::LightState::transformer_ {nullptr}
protected

The currently active transformer for this light (transition/flash).

Definition at line 311 of file light_state.h.


The documentation for this class was generated from the following files: