ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
4#include "light_state.h"
5#include "addressable_light.h"
6
7namespace esphome::light {
8
9enum class LimitMode { CLAMP, DO_NOTHING };
10
11template<typename... Ts> class ToggleAction : public Action<Ts...> {
12 public:
14
15 TEMPLATABLE_VALUE(uint32_t, transition_length)
16
17 void play(const Ts &...x) override {
18 auto call = this->state_->toggle();
19 call.set_transition_length(this->transition_length_.optional_value(x...));
20 call.perform();
21 }
22
23 protected:
25};
26
27template<typename... Ts> class LightControlAction : public Action<Ts...> {
28 public:
29 explicit LightControlAction(LightState *parent) : parent_(parent) {}
30
33 TEMPLATABLE_VALUE(uint32_t, transition_length)
34 TEMPLATABLE_VALUE(uint32_t, flash_length)
35 TEMPLATABLE_VALUE(float, brightness)
36 TEMPLATABLE_VALUE(float, color_brightness)
38 TEMPLATABLE_VALUE(float, green)
39 TEMPLATABLE_VALUE(float, blue)
40 TEMPLATABLE_VALUE(float, white)
41 TEMPLATABLE_VALUE(float, color_temperature)
42 TEMPLATABLE_VALUE(float, cold_white)
43 TEMPLATABLE_VALUE(float, warm_white)
44 TEMPLATABLE_VALUE(std::string, effect)
45
46 void play(const Ts &...x) override {
47 auto call = this->parent_->make_call();
48 call.set_color_mode(this->color_mode_.optional_value(x...));
49 call.set_state(this->state_.optional_value(x...));
50 call.set_brightness(this->brightness_.optional_value(x...));
51 call.set_color_brightness(this->color_brightness_.optional_value(x...));
52 call.set_red(this->red_.optional_value(x...));
53 call.set_green(this->green_.optional_value(x...));
54 call.set_blue(this->blue_.optional_value(x...));
55 call.set_white(this->white_.optional_value(x...));
56 call.set_color_temperature(this->color_temperature_.optional_value(x...));
57 call.set_cold_white(this->cold_white_.optional_value(x...));
58 call.set_warm_white(this->warm_white_.optional_value(x...));
59 call.set_effect(this->effect_.optional_value(x...));
60 call.set_flash_length(this->flash_length_.optional_value(x...));
61 call.set_transition_length(this->transition_length_.optional_value(x...));
62 call.perform();
63 }
64
65 protected:
66 LightState *parent_;
67};
68
69template<typename... Ts> class DimRelativeAction : public Action<Ts...> {
70 public:
71 explicit DimRelativeAction(LightState *parent) : parent_(parent) {}
72
73 TEMPLATABLE_VALUE(float, relative_brightness)
74 TEMPLATABLE_VALUE(uint32_t, transition_length)
75
76 void play(const Ts &...x) override {
77 auto call = this->parent_->make_call();
78 float rel = this->relative_brightness_.value(x...);
79 float cur;
81 if ((limit_mode_ == LimitMode::DO_NOTHING) && ((cur < min_brightness_) || (cur > max_brightness_))) {
82 return;
83 }
84 float new_brightness = clamp(cur + rel, min_brightness_, max_brightness_);
85 call.set_state(new_brightness != 0.0f);
86 call.set_brightness(new_brightness);
87
88 call.set_transition_length(this->transition_length_.optional_value(x...));
89 call.perform();
90 }
91
92 void set_min_max_brightness(float min, float max) {
93 this->min_brightness_ = min;
94 this->max_brightness_ = max;
95 }
96
97 void set_limit_mode(LimitMode limit_mode) { this->limit_mode_ = limit_mode; }
98
99 protected:
101 float min_brightness_{0.0};
102 float max_brightness_{1.0};
104};
105
106template<typename... Ts> class LightIsOnCondition : public Condition<Ts...> {
107 public:
109 bool check(const Ts &...x) override { return this->state_->current_values.is_on(); }
110
111 protected:
113};
114template<typename... Ts> class LightIsOffCondition : public Condition<Ts...> {
115 public:
117 bool check(const Ts &...x) override { return !this->state_->current_values.is_on(); }
118
119 protected:
121};
122
124 public:
125 explicit LightTurnOnTrigger(LightState *a_light) : light_(a_light) {
126 a_light->add_remote_values_listener(this);
127 this->last_on_ = a_light->current_values.is_on();
128 }
129
131 // using the remote value because of transitions we need to trigger as early as possible
132 auto is_on = this->light_->remote_values.is_on();
133 // only trigger when going from off to on
134 auto should_trigger = is_on && !this->last_on_;
135 // Set new state immediately so that trigger() doesn't devolve
136 // into infinite loop
137 this->last_on_ = is_on;
138 if (should_trigger) {
139 this->trigger();
140 }
141 }
142
143 protected:
146};
147
149 public:
150 explicit LightTurnOffTrigger(LightState *a_light) : light_(a_light) {
152 }
153
155 auto is_on = this->light_->current_values.is_on();
156 // only trigger when going from on to off
157 if (!is_on) {
158 this->trigger();
159 }
160 }
161
162 protected:
164};
165
167 public:
168 explicit LightStateTrigger(LightState *a_light) { a_light->add_remote_values_listener(this); }
169
170 void on_light_remote_values_update() override { this->trigger(); }
171};
172
173// This is slightly ugly, but we can't log in headers, and can't make this a static method on AddressableSet
174// due to the template. It's just a temporary warning anyway.
175void addressableset_warn_about_scale(const char *field);
176
177template<typename... Ts> class AddressableSet : public Action<Ts...> {
178 public:
179 explicit AddressableSet(LightState *parent) : parent_(parent) {}
180
181 TEMPLATABLE_VALUE(int32_t, range_from)
182 TEMPLATABLE_VALUE(int32_t, range_to)
183 TEMPLATABLE_VALUE(float, color_brightness)
184 TEMPLATABLE_VALUE(float, red)
185 TEMPLATABLE_VALUE(float, green)
186 TEMPLATABLE_VALUE(float, blue)
187 TEMPLATABLE_VALUE(float, white)
188
189 void play(const Ts &...x) override {
190 auto *out = (AddressableLight *) this->parent_->get_output();
191 int32_t range_from = interpret_index(this->range_from_.value_or(x..., 0), out->size());
192 if (range_from < 0 || range_from >= out->size())
193 range_from = 0;
194
195 int32_t range_to = interpret_index(this->range_to_.value_or(x..., out->size() - 1) + 1, out->size());
196 if (range_to < 0 || range_to >= out->size())
197 range_to = out->size();
198
199 uint8_t color_brightness =
200 to_uint8_scale(this->color_brightness_.value_or(x..., this->parent_->remote_values.get_color_brightness()));
201 auto range = out->range(range_from, range_to);
202 if (this->red_.has_value())
203 range.set_red(esp_scale8(to_uint8_compat(this->red_.value(x...), "red"), color_brightness));
204 if (this->green_.has_value())
205 range.set_green(esp_scale8(to_uint8_compat(this->green_.value(x...), "green"), color_brightness));
206 if (this->blue_.has_value())
207 range.set_blue(esp_scale8(to_uint8_compat(this->blue_.value(x...), "blue"), color_brightness));
208 if (this->white_.has_value())
209 range.set_white(to_uint8_compat(this->white_.value(x...), "white"));
210 out->schedule_show();
211 }
212
213 protected:
215
216 // Historically, this action required uint8_t (0-255) for RGBW values from lambdas. Keep compatibility.
217 static inline uint8_t to_uint8_compat(float value, const char *field) {
218 if (value > 1.0f) {
220 return static_cast<uint8_t>(value);
221 }
222 return to_uint8_scale(value);
223 }
224};
225
226} // namespace esphome::light
virtual void play(const Ts &...x)=0
Base class for all automation conditions.
Definition automation.h:183
void trigger(const Ts &...x)
Definition automation.h:204
static uint8_t to_uint8_compat(float value, const char *field)
Definition automation.h:217
AddressableSet(LightState *parent)
Definition automation.h:179
TEMPLATABLE_VALUE(int32_t, range_from) TEMPLATABLE_VALUE(int32_t
TEMPLATABLE_VALUE(float, relative_brightness) TEMPLATABLE_VALUE(uint32_t
DimRelativeAction(LightState *parent)
Definition automation.h:71
void set_min_max_brightness(float min, float max)
Definition automation.h:92
void set_limit_mode(LimitMode limit_mode)
Definition automation.h:97
transition_length void play(const Ts &...x) override
Definition automation.h:76
LightCall & set_transition_length(optional< uint32_t > transition_length)
Set the transition length of this call in milliseconds.
bool is_on() const
Get the binary true/false state of these light color values.
void as_brightness(float *brightness, float gamma=0) const
Convert these light color values to a brightness-only representation and write them to brightness.
TEMPLATABLE_VALUE(ColorMode, color_mode) TEMPLATABLE_VALUE(bool
LightControlAction(LightState *parent)
Definition automation.h:29
bool check(const Ts &...x) override
Definition automation.h:117
LightIsOffCondition(LightState *state)
Definition automation.h:116
LightIsOnCondition(LightState *state)
Definition automation.h:108
bool check(const Ts &...x) override
Definition automation.h:109
Listener interface for light remote value changes.
Definition light_state.h:29
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:91
void add_remote_values_listener(LightRemoteValuesListener *listener)
Add a listener for remote values changes.
LightColorValues remote_values
The remote color values reported to the frontend.
LightOutput * get_output() const
Get the light output associated with this object.
void add_target_state_reached_listener(LightTargetStateReachedListener *listener)
Add a listener for target state reached.
LightColorValues current_values
The current values of the light as outputted to the light.
void on_light_remote_values_update() override
Definition automation.h:170
LightStateTrigger(LightState *a_light)
Definition automation.h:168
Listener interface for light target state reached.
Definition light_state.h:40
void on_light_target_state_reached() override
Definition automation.h:154
LightTurnOffTrigger(LightState *a_light)
Definition automation.h:150
LightTurnOnTrigger(LightState *a_light)
Definition automation.h:125
void on_light_remote_values_update() override
Definition automation.h:130
TEMPLATABLE_VALUE(uint32_t, transition_length) void play(const Ts &...x) override
Definition automation.h:15
ToggleAction(LightState *state)
Definition automation.h:13
bool state
Definition fan.h:0
Range range
Definition msa3xx.h:0
void addressableset_warn_about_scale(const char *field)
Definition automation.cpp:8
ColorMode
Color modes are a combination of color capabilities that can be used at the same time.
Definition color_mode.h:49
int32_t HOT interpret_index(int32_t index, int32_t size)
uint16_t x
Definition tt21100.cpp:5