ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
base_light_effects.h
Go to the documentation of this file.
1#pragma once
2
3#include <utility>
4
7#include "light_effect.h"
8
9namespace esphome::light {
10
11inline static float random_cubic_float() {
12 const float r = random_float() * 2.0f - 1.0f;
13 return r * r * r;
14}
15
18 public:
19 explicit PulseLightEffect(const char *name) : LightEffect(name) {}
20
21 void apply() override {
22 const uint32_t now = millis();
24 return;
25 }
26 auto call = this->state_->turn_on();
27 float out = this->on_ ? this->max_brightness_ : this->min_brightness_;
30 this->on_ = !this->on_;
31 // don't tell HA every change
32 call.set_publish(false);
33 call.set_save(false);
34 call.perform();
35
36 this->last_color_change_ = now;
37 }
38
39 void set_transition_on_length(uint32_t transition_length) { this->transition_on_length_ = transition_length; }
40 void set_transition_off_length(uint32_t transition_length) { this->transition_off_length_ = transition_length; }
41
42 void set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; }
43
44 void set_min_max_brightness(float min, float max) {
45 this->min_brightness_ = min;
46 this->max_brightness_ = max;
47 }
48
49 protected:
50 bool on_ = false;
55 float min_brightness_{0.0};
56 float max_brightness_{1.0};
57};
58
61 public:
62 explicit RandomLightEffect(const char *name) : LightEffect(name) {}
63
64 void apply() override {
65 const uint32_t now = millis();
67 return;
68 }
69
70 auto color_mode = this->state_->remote_values.get_color_mode();
71 auto call = this->state_->turn_on();
72 bool changed = false;
73 if (color_mode & ColorCapability::RGB) {
74 call.set_red(random_float());
75 call.set_green(random_float());
76 call.set_blue(random_float());
77 changed = true;
78 }
79 if (color_mode & ColorCapability::COLOR_TEMPERATURE) {
80 float min = this->state_->get_traits().get_min_mireds();
81 float max = this->state_->get_traits().get_max_mireds();
82 call.set_color_temperature(min + random_float() * (max - min));
83 changed = true;
84 }
85 if (color_mode & ColorCapability::COLD_WARM_WHITE) {
86 call.set_cold_white(random_float());
87 call.set_warm_white(random_float());
88 changed = true;
89 }
90 if (!changed) {
91 // only randomize brightness if there's no colored option available
92 call.set_brightness(random_float());
93 }
95 call.set_publish(true);
96 call.set_save(false);
97 call.perform();
98
99 this->last_color_change_ = now;
100 }
101
102 void set_transition_length(uint32_t transition_length) { this->transition_length_ = transition_length; }
103
104 void set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; }
105
106 protected:
110};
111
113 public:
114 LambdaLightEffect(const char *name, void (*f)(bool initial_run), uint32_t update_interval)
115 : LightEffect(name), f_(f), update_interval_(update_interval) {}
116
117 void start() override { this->initial_run_ = true; }
118 void apply() override {
119 const uint32_t now = millis();
120 if (now - this->last_run_ >= this->update_interval_ || this->initial_run_) {
121 this->last_run_ = now;
122 this->f_(this->initial_run_);
123 this->initial_run_ = false;
124 }
125 }
126
129 uint32_t get_current_index() const { return this->get_index(); }
130
131 protected:
132 void (*f_)(bool initial_run);
136};
137
139 public:
140 AutomationLightEffect(const char *name) : LightEffect(name) {}
141 void stop() override { this->trig_->stop_action(); }
142 void apply() override {
143 if (!this->trig_->is_action_running()) {
144 this->trig_->trigger();
145 }
146 }
147 Trigger<> *get_trig() const { return trig_; }
148
151 uint32_t get_current_index() const { return this->get_index(); }
152
153 protected:
155};
156
162
164 public:
165 explicit StrobeLightEffect(const char *name) : LightEffect(name) {}
166 void apply() override {
167 const uint32_t now = millis();
168 if (now - this->last_switch_ < this->colors_[this->at_color_].duration)
169 return;
170
171 // Switch to next color
172 this->at_color_ = (this->at_color_ + 1) % this->colors_.size();
173 auto color = this->colors_[this->at_color_].color;
174
175 auto call = this->state_->turn_on();
176 call.from_light_color_values(this->colors_[this->at_color_].color);
177
178 if (!color.is_on()) {
179 // Don't turn the light off, otherwise the light effect will be stopped
180 call.set_brightness(0.0f);
181 call.set_state(true);
182 }
183 call.set_publish(false);
184 call.set_save(false);
185 call.set_transition_length_if_supported(this->colors_[this->at_color_].transition_length);
186 call.perform();
187 this->last_switch_ = now;
188 }
189
190 void set_colors(const std::initializer_list<StrobeLightEffectColor> &colors) { this->colors_ = colors; }
191
192 protected:
195 size_t at_color_{0};
196};
197
199 public:
200 explicit FlickerLightEffect(const char *name) : LightEffect(name) {}
201
202 void apply() override {
203 LightColorValues remote = this->state_->remote_values;
204 LightColorValues current = this->state_->current_values;
206 const float alpha = this->alpha_;
207 const float beta = 1.0f - alpha;
208 out.set_state(true);
209 out.set_brightness(remote.get_brightness() * beta + current.get_brightness() * alpha +
210 (random_cubic_float() * this->intensity_));
211 out.set_red(remote.get_red() * beta + current.get_red() * alpha + (random_cubic_float() * this->intensity_));
212 out.set_green(remote.get_green() * beta + current.get_green() * alpha + (random_cubic_float() * this->intensity_));
213 out.set_blue(remote.get_blue() * beta + current.get_blue() * alpha + (random_cubic_float() * this->intensity_));
214 out.set_white(remote.get_white() * beta + current.get_white() * alpha + (random_cubic_float() * this->intensity_));
215 out.set_cold_white(remote.get_cold_white() * beta + current.get_cold_white() * alpha +
216 (random_cubic_float() * this->intensity_));
217 out.set_warm_white(remote.get_warm_white() * beta + current.get_warm_white() * alpha +
218 (random_cubic_float() * this->intensity_));
219
220 auto call = this->state_->make_call();
221 call.set_publish(false);
222 call.set_save(false);
224 call.from_light_color_values(out);
225 call.set_state(true);
226 call.perform();
227 }
228
229 void set_alpha(float alpha) { this->alpha_ = alpha; }
230 void set_intensity(float intensity) { this->intensity_ = intensity; }
231
232 protected:
233 float intensity_{};
234 float alpha_{};
235};
236
237} // namespace esphome::light
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:184
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:204
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:212
bool is_action_running()
Returns true if any action connected to this trigger is running.
Definition automation.h:218
uint32_t get_current_index() const
Get the current effect index for use in automations.
uint32_t get_current_index() const
Get the current effect index for use in lambda functions.
LambdaLightEffect(const char *name, void(*f)(bool initial_run), 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.
uint32_t get_index() const
Get the index of this effect in the parent light's effect list.
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.
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)
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)
FixedVector< StrobeLightEffectColor > colors_
void set_colors(const std::initializer_list< 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.
float random_float()
Return a random float between 0 and 1.
Definition helpers.cpp:158
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25