ESPHome 2025.12.0-dev
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 {
10namespace light {
11
13 public:
14 void start() override {
15 // When turning light on from off state, use target state and only increase brightness from zero.
16 if (!this->start_values_.is_on() && this->target_values_.is_on()) {
18 this->start_values_.set_brightness(0.0f);
19 }
20
21 // When turning light off from on state, use source state and only decrease brightness to zero. Use a second
22 // variable for transition end state, as overwriting target_values breaks LightState logic.
23 if (this->start_values_.is_on() && !this->target_values_.is_on()) {
25 this->end_values_.set_brightness(0.0f);
26 } else {
28 }
29
30 // When changing color mode, go through off state, as color modes are orthogonal and there can't be two active.
31 if (this->start_values_.get_color_mode() != this->target_values_.get_color_mode()) {
32 this->changing_color_mode_ = true;
35 }
36 }
37
39 float p = this->get_progress_();
40
41 // Halfway through, when intermediate state (off) is reached, flip it to the target, but remain off.
42 if (this->changing_color_mode_ && p > 0.5f &&
43 this->intermediate_values_.get_color_mode() != this->target_values_.get_color_mode()) {
46 }
47
50 if (this->changing_color_mode_)
51 p = p < 0.5f ? p * 2 : (p - 0.5) * 2;
52
55 }
56
57 protected:
61};
62
64 public:
66
67 void start() override {
69 if (this->transition_length_ * 2 > this->length_)
70 this->transition_length_ = this->length_ / 2;
71
72 this->begun_lightstate_restore_ = false;
73
74 // first transition to original target
76 this->transformer_->setup(this->state_.current_values, this->target_values_, this->transition_length_);
77 }
78
81
82 if (this->transformer_ == nullptr && millis() > this->start_time_ + this->length_ - this->transition_length_) {
83 // second transition back to start value
85 this->transformer_->setup(this->state_.current_values, this->get_start_values(), this->transition_length_);
86 this->begun_lightstate_restore_ = true;
87 }
88
89 if (this->transformer_ != nullptr) {
90 result = this->transformer_->apply();
91
92 if (this->transformer_->is_finished()) {
93 this->transformer_->stop();
94 this->transformer_ = nullptr;
95 }
96 }
97
98 return result;
99 }
100
101 // Restore the original values after the flash.
102 void stop() override {
103 if (this->transformer_ != nullptr) {
104 this->transformer_->stop();
105 this->transformer_ = nullptr;
106 }
107 this->state_.current_values = this->get_start_values();
108 this->state_.remote_values = this->get_start_values();
109 this->state_.publish_state();
110 }
111
113
114 protected:
116 std::unique_ptr<LightTransformer> transformer_{nullptr};
119};
120
121} // namespace light
122} // namespace esphome
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:69
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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:30
uint8_t end[39]
Definition sun_gtil2.cpp:17