ESPHome 2025.10.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#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;
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
130 uint32_t get_current_index() const { return this->get_index(); }
131
132 protected:
133 std::function<void(bool initial_run)> f_;
137};
138
140 public:
141 AutomationLightEffect(const std::string &name) : LightEffect(name) {}
142 void stop() override { this->trig_->stop_action(); }
143 void apply() override {
144 if (!this->trig_->is_action_running()) {
145 this->trig_->trigger();
146 }
147 }
148 Trigger<> *get_trig() const { return trig_; }
149
152 uint32_t get_current_index() const { return this->get_index(); }
153
154 protected:
156};
157
163
165 public:
166 explicit StrobeLightEffect(const std::string &name) : LightEffect(name) {}
167 void apply() override {
168 const uint32_t now = millis();
169 if (now - this->last_switch_ < this->colors_[this->at_color_].duration)
170 return;
171
172 // Switch to next color
173 this->at_color_ = (this->at_color_ + 1) % this->colors_.size();
174 auto color = this->colors_[this->at_color_].color;
175
176 auto call = this->state_->turn_on();
177 call.from_light_color_values(this->colors_[this->at_color_].color);
178
179 if (!color.is_on()) {
180 // Don't turn the light off, otherwise the light effect will be stopped
181 call.set_brightness(0.0f);
182 call.set_state(true);
183 }
184 call.set_publish(false);
185 call.set_save(false);
186 call.set_transition_length_if_supported(this->colors_[this->at_color_].transition_length);
187 call.perform();
188 this->last_switch_ = now;
189 }
190
191 void set_colors(const std::vector<StrobeLightEffectColor> &colors) { this->colors_ = colors; }
192
193 protected:
194 std::vector<StrobeLightEffectColor> colors_;
196 size_t at_color_{0};
197};
198
200 public:
201 explicit FlickerLightEffect(const std::string &name) : LightEffect(name) {}
202
203 void apply() override {
204 LightColorValues remote = this->state_->remote_values;
205 LightColorValues current = this->state_->current_values;
207 const float alpha = this->alpha_;
208 const float beta = 1.0f - alpha;
209 out.set_state(true);
210 out.set_brightness(remote.get_brightness() * beta + current.get_brightness() * alpha +
211 (random_cubic_float() * this->intensity_));
212 out.set_red(remote.get_red() * beta + current.get_red() * alpha + (random_cubic_float() * this->intensity_));
213 out.set_green(remote.get_green() * beta + current.get_green() * alpha + (random_cubic_float() * this->intensity_));
214 out.set_blue(remote.get_blue() * beta + current.get_blue() * alpha + (random_cubic_float() * this->intensity_));
215 out.set_white(remote.get_white() * beta + current.get_white() * alpha + (random_cubic_float() * this->intensity_));
216 out.set_cold_white(remote.get_cold_white() * beta + current.get_cold_white() * alpha +
217 (random_cubic_float() * this->intensity_));
218 out.set_warm_white(remote.get_warm_white() * beta + current.get_warm_white() * alpha +
219 (random_cubic_float() * this->intensity_));
220
221 auto call = this->state_->make_call();
222 call.set_publish(false);
223 call.set_save(false);
225 call.from_light_color_values(out);
226 call.set_state(true);
227 call.perform();
228 }
229
230 void set_alpha(float alpha) { this->alpha_ = alpha; }
231 void set_intensity(float intensity) { this->intensity_ = intensity; }
232
233 protected:
234 float intensity_{};
235 float alpha_{};
236};
237
238} // namespace light
239} // namespace esphome
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:153
bool is_action_running()
Returns true if any action connected to this trigger is running.
Definition automation.h:159
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition automation.h:145
uint32_t get_current_index() const
Get the current effect index for use in automations.
AutomationLightEffect(const std::string &name)
FlickerLightEffect(const std::string &name)
uint32_t get_current_index() const
Get the current effect index for use in lambda functions.
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.
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.
Definition light_state.h:99
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:156
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28