ESPHome 2025.12.3
Loading...
Searching...
No Matches
transformers.h
Go to the documentation of this file.
1#pragma once
2
6#include "light_state.h"
7#include "light_transformer.h"
8
9namespace esphome::light {
10
12 public:
13 void start() override {
14 // When turning light on from off state, use target state and only increase brightness from zero.
15 if (!this->start_values_.is_on() && this->target_values_.is_on()) {
17 this->start_values_.set_brightness(0.0f);
18 }
19
20 // When turning light off from on state, use source state and only decrease brightness to zero. Use a second
21 // variable for transition end state, as overwriting target_values breaks LightState logic.
22 if (this->start_values_.is_on() && !this->target_values_.is_on()) {
24 this->end_values_.set_brightness(0.0f);
25 } else {
27 }
28
29 // When changing color mode, go through off state, as color modes are orthogonal and there can't be two active.
30 if (this->start_values_.get_color_mode() != this->target_values_.get_color_mode()) {
31 this->changing_color_mode_ = true;
34 }
35 }
36
38 float p = this->get_progress_();
39
40 // Halfway through, when intermediate state (off) is reached, flip it to the target, but remain off.
41 if (this->changing_color_mode_ && p > 0.5f &&
42 this->intermediate_values_.get_color_mode() != this->target_values_.get_color_mode()) {
45 }
46
49 if (this->changing_color_mode_)
50 p = p < 0.5f ? p * 2 : (p - 0.5) * 2;
51
54 }
55
56 protected:
60};
61
63 public:
65
66 void start() override {
68 if (this->transition_length_ * 2 > this->length_)
69 this->transition_length_ = this->length_ / 2;
70
71 this->begun_lightstate_restore_ = false;
72
73 // first transition to original target
75 this->transformer_->setup(this->state_.current_values, this->target_values_, this->transition_length_);
76 }
77
80
81 if (this->transformer_ == nullptr && millis() > this->start_time_ + this->length_ - this->transition_length_) {
82 // second transition back to start value
84 this->transformer_->setup(this->state_.current_values, this->get_start_values(), this->transition_length_);
85 this->begun_lightstate_restore_ = true;
86 }
87
88 if (this->transformer_ != nullptr) {
89 result = this->transformer_->apply();
90
91 if (this->transformer_->is_finished()) {
92 this->transformer_->stop();
93 this->transformer_ = nullptr;
94 }
95 }
96
97 return result;
98 }
99
100 // Restore the original values after the flash.
101 void stop() override {
102 if (this->transformer_ != nullptr) {
103 this->transformer_->stop();
104 this->transformer_ = nullptr;
105 }
106 this->state_.current_values = this->get_start_values();
107 this->state_.remote_values = this->get_start_values();
108 this->state_.publish_state();
109 }
110
112
113 protected:
115 std::unique_ptr<LightTransformer> transformer_{nullptr};
118};
119
120} // namespace esphome::light
This class represents the color state for a light object.
static LightColorValues lerp(const LightColorValues &start, const LightColorValues &end, float completion)
Linearly interpolate between the values in start to the values in end.
void set_brightness(float brightness)
Set the brightness property of these light color values. In range 0.0 to 1.0.
bool is_on() const
Get the binary true/false state of these light color values.
void set_state(float state)
Set the state of these light color values. In range from 0.0 (off) to 1.0 (on)
ColorMode get_color_mode() const
Get the color mode of these light color values.
optional< LightColorValues > apply() override
std::unique_ptr< LightTransformer > transformer_
virtual std::unique_ptr< LightTransformer > create_default_transition()
Return the default transformer used for transitions.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:91
LightColorValues remote_values
The remote color values reported to the frontend.
LightOutput * get_output() const
Get the light output associated with this object.
uint32_t get_flash_transition_length() const
void publish_state()
Publish the currently active state to the frontend.
LightColorValues current_values
The current values of the light as outputted to the light.
Base class for all light color transformers, such as transitions or flashes.
float get_progress_()
The progress of this transition, on a scale of 0 to 1.
static float smoothed_progress(float x)
virtual bool is_finished()
Indicates whether this transformation is finished.
const LightColorValues & get_start_values() const
optional< LightColorValues > apply() override
bool state
Definition fan.h:0
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:30
uint8_t end[39]
Definition sun_gtil2.cpp:17