ESPHome 2025.6.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
base_light_effects.h
Go to the documentation of this file.
1#pragma once
2
3#include <utility>
4#include <vector>
5
7#include "light_effect.h"
8
9namespace esphome {
10namespace light {
11
12inline static float random_cubic_float() {
13 const float r = random_float() * 2.0f - 1.0f;
14 return r * r * r;
15}
16
19 public:
20 explicit PulseLightEffect(const std::string &name) : LightEffect(name) {}
21
22 void apply() override {
23 const uint32_t now = millis();
25 return;
26 }
27 auto call = this->state_->turn_on();
28 float out = this->on_ ? this->max_brightness_ : this->min_brightness_;
31 this->on_ = !this->on_;
32 // don't tell HA every change
33 call.set_publish(false);
34 call.set_save(false);
35 call.perform();
36
37 this->last_color_change_ = now;
38 }
39
40 void set_transition_on_length(uint32_t transition_length) { this->transition_on_length_ = transition_length; }
41 void set_transition_off_length(uint32_t transition_length) { this->transition_off_length_ = transition_length; }
42
43 void set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; }
44
45 void set_min_max_brightness(float min, float max) {
46 this->min_brightness_ = min;
47 this->max_brightness_ = max;
48 }
49
50 protected:
51 bool on_ = false;
52 uint32_t last_color_change_{0};
55 uint32_t update_interval_{};
56 float min_brightness_{0.0};
57 float max_brightness_{1.0};
58};
59
62 public:
63 explicit RandomLightEffect(const std::string &name) : LightEffect(name) {}
64
65 void apply() override {
66 const uint32_t now = millis();
68 return;
69 }
70
71 auto color_mode = this->state_->remote_values.get_color_mode();
72 auto call = this->state_->turn_on();
73 bool changed = false;
74 if (color_mode & ColorCapability::RGB) {
75 call.set_red(random_float());
76 call.set_green(random_float());
77 call.set_blue(random_float());
78 changed = true;
79 }
80 if (color_mode & ColorCapability::COLOR_TEMPERATURE) {
81 float min = this->state_->get_traits().get_min_mireds();
82 float max = this->state_->get_traits().get_max_mireds();
83 call.set_color_temperature(min + random_float() * (max - min));
84 changed = true;
85 }
86 if (color_mode & ColorCapability::COLD_WARM_WHITE) {
87 call.set_cold_white(random_float());
88 call.set_warm_white(random_float());
89 changed = true;
90 }
91 if (!changed) {
92 // only randomize brightness if there's no colored option available
93 call.set_brightness(random_float());
94 }
96 call.set_publish(true);
97 call.set_save(false);
98 call.perform();
99
100 this->last_color_change_ = now;
101 }
102
103 void set_transition_length(uint32_t transition_length) { this->transition_length_ = transition_length; }
104
105 void set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; }
106
107 protected:
111};
112
114 public:
115 LambdaLightEffect(const std::string &name, std::function<void(bool initial_run)> f, uint32_t update_interval)
116 : LightEffect(name), f_(std::move(f)), update_interval_(update_interval) {}
117
118 void start() override { this->initial_run_ = true; }
119 void apply() override {
120 const uint32_t now = millis();
121 if (now - this->last_run_ >= this->update_interval_ || this->initial_run_) {
122 this->last_run_ = now;
123 this->f_(this->initial_run_);
124 this->initial_run_ = false;
125 }
126 }
127
128 protected:
129 std::function<void(bool initial_run)> f_;
131 uint32_t last_run_{0};
133};
134
136 public:
137 AutomationLightEffect(const std::string &name) : LightEffect(name) {}
138 void stop() override { this->trig_->stop_action(); }
139 void apply() override {
140 if (!this->trig_->is_action_running()) {
141 this->trig_->trigger();
142 }
143 }
144 Trigger<> *get_trig() const { return trig_; }
145
146 protected:
148};
149
155
157 public:
158 explicit StrobeLightEffect(const std::string &name) : LightEffect(name) {}
159 void apply() override {
160 const uint32_t now = millis();
161 if (now - this->last_switch_ < this->colors_[this->at_color_].duration)
162 return;
163
164 // Switch to next color
165 this->at_color_ = (this->at_color_ + 1) % this->colors_.size();
166 auto color = this->colors_[this->at_color_].color;
167
168 auto call = this->state_->turn_on();
169 call.from_light_color_values(this->colors_[this->at_color_].color);
170
171 if (!color.is_on()) {
172 // Don't turn the light off, otherwise the light effect will be stopped
173 call.set_brightness(0.0f);
174 call.set_state(true);
175 }
176 call.set_publish(false);
177 call.set_save(false);
178 call.set_transition_length_if_supported(this->colors_[this->at_color_].transition_length);
179 call.perform();
180 this->last_switch_ = now;
181 }
182
183 void set_colors(const std::vector<StrobeLightEffectColor> &colors) { this->colors_ = colors; }
184
185 protected:
186 std::vector<StrobeLightEffectColor> colors_;
187 uint32_t last_switch_{0};
188 size_t at_color_{0};
189};
190
192 public:
193 explicit FlickerLightEffect(const std::string &name) : LightEffect(name) {}
194
195 void apply() override {
196 LightColorValues remote = this->state_->remote_values;
197 LightColorValues current = this->state_->current_values;
199 const float alpha = this->alpha_;
200 const float beta = 1.0f - alpha;
201 out.set_state(true);
202 out.set_brightness(remote.get_brightness() * beta + current.get_brightness() * alpha +
203 (random_cubic_float() * this->intensity_));
204 out.set_red(remote.get_red() * beta + current.get_red() * alpha + (random_cubic_float() * this->intensity_));
205 out.set_green(remote.get_green() * beta + current.get_green() * alpha + (random_cubic_float() * this->intensity_));
206 out.set_blue(remote.get_blue() * beta + current.get_blue() * alpha + (random_cubic_float() * this->intensity_));
207 out.set_white(remote.get_white() * beta + current.get_white() * alpha + (random_cubic_float() * this->intensity_));
208 out.set_cold_white(remote.get_cold_white() * beta + current.get_cold_white() * alpha +
209 (random_cubic_float() * this->intensity_));
210 out.set_warm_white(remote.get_warm_white() * beta + current.get_warm_white() * alpha +
211 (random_cubic_float() * this->intensity_));
212
213 auto call = this->state_->make_call();
214 call.set_publish(false);
215 call.set_save(false);
217 call.from_light_color_values(out);
218 call.set_state(true);
219 call.perform();
220 }
221
222 void set_alpha(float alpha) { this->alpha_ = alpha; }
223 void set_intensity(float intensity) { this->intensity_ = intensity; }
224
225 protected:
226 float intensity_{};
227 float alpha_{};
228};
229
230} // namespace light
231} // namespace esphome
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:104
bool is_action_running()
Returns true if any action connected to this trigger is running.
Definition automation.h:110
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition automation.h:96
AutomationLightEffect(const std::string &name)
FlickerLightEffect(const std::string &name)
std::function< void(bool initial_run)> f_
LambdaLightEffect(const std::string &name, std::function< void(bool initial_run)> f, uint32_t update_interval)
LightCall & set_publish(bool publish)
Set whether this light call should trigger a publish state.
LightCall & set_transition_length_if_supported(uint32_t transition_length)
Set the transition length property if the light supports transitions.
LightCall & set_save(bool save)
Set whether this light call should trigger a save state to recover them at startup....
LightCall & set_brightness_if_supported(float brightness)
Set the brightness property if the light supports brightness.
LightCall & from_light_color_values(const LightColorValues &values)
LightCall & set_state(optional< bool > state)
Set the binary ON/OFF state of the light.
This class represents the color state for a light object.
float get_brightness() const
Get the brightness property of these light color values. In range 0.0 to 1.0.
float get_blue() const
Get the blue property of these light color values. In range 0.0 to 1.0.
float get_white() const
Get the white property of these light color values. In range 0.0 to 1.0.
void set_brightness(float brightness)
Set the brightness property of these light color values. In range 0.0 to 1.0.
float get_cold_white() const
Get the cold white property of these light color values. In range 0.0 to 1.0.
void set_blue(float blue)
Set the blue property of these light color values. In range 0.0 to 1.0.
void set_cold_white(float cold_white)
Set the cold white property of these light color values. In range 0.0 to 1.0.
void set_warm_white(float warm_white)
Set the warm white property of these light color values. In range 0.0 to 1.0.
float get_green() const
Get the green property of these light color values. In range 0.0 to 1.0.
void set_state(float state)
Set the state of these light color values. In range from 0.0 (off) to 1.0 (on)
float get_warm_white() const
Get the warm white property of these light color values. In range 0.0 to 1.0.
ColorMode get_color_mode() const
Get the color mode of these light color values.
void set_white(float white)
Set the white property of these light color values. In range 0.0 to 1.0.
float get_red() const
Get the red property of these light color values. In range 0.0 to 1.0.
void set_green(float green)
Set the green property of these light color values. In range 0.0 to 1.0.
void set_red(float red)
Set the red property of these light color values. In range 0.0 to 1.0.
LightColorValues remote_values
The remote color values reported to the frontend.
LightCall turn_on()
Make a light state call.
LightColorValues current_values
The current values of the light as outputted to the light.
Definition light_state.h:94
void set_min_max_brightness(float min, float max)
void set_update_interval(uint32_t update_interval)
void set_transition_off_length(uint32_t transition_length)
void set_transition_on_length(uint32_t transition_length)
PulseLightEffect(const std::string &name)
Random effect. Sets random colors every 10 seconds and slowly transitions between them.
void set_update_interval(uint32_t update_interval)
void set_transition_length(uint32_t transition_length)
RandomLightEffect(const std::string &name)
StrobeLightEffect(const std::string &name)
std::vector< StrobeLightEffectColor > colors_
void set_colors(const std::vector< StrobeLightEffectColor > &colors)
uint8_t duration
Definition msa3xx.h:0
@ RGB
Color can be controlled using RGB format (includes a brightness control for the color).
@ COLOR_TEMPERATURE
Color temperature can be controlled.
@ COLD_WARM_WHITE
Brightness of cold and warm white output can be controlled.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
float random_float()
Return a random float between 0 and 1.
Definition helpers.cpp:218
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28