ESPHome  2024.5.5
automation.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "light_state.h"
5 #include "addressable_light.h"
6 
7 namespace esphome {
8 namespace light {
9 
10 template<typename... Ts> class ToggleAction : public Action<Ts...> {
11  public:
12  explicit ToggleAction(LightState *state) : state_(state) {}
13 
14  TEMPLATABLE_VALUE(uint32_t, transition_length)
15 
16  void play(Ts... x) override {
17  auto call = this->state_->toggle();
18  call.set_transition_length(this->transition_length_.optional_value(x...));
19  call.perform();
20  }
21 
22  protected:
24 };
25 
26 template<typename... Ts> class LightControlAction : public Action<Ts...> {
27  public:
28  explicit LightControlAction(LightState *parent) : parent_(parent) {}
29 
30  TEMPLATABLE_VALUE(ColorMode, color_mode)
32  TEMPLATABLE_VALUE(uint32_t, transition_length)
33  TEMPLATABLE_VALUE(uint32_t, flash_length)
34  TEMPLATABLE_VALUE(float, brightness)
35  TEMPLATABLE_VALUE(float, color_brightness)
36  TEMPLATABLE_VALUE(float, red)
37  TEMPLATABLE_VALUE(float, green)
38  TEMPLATABLE_VALUE(float, blue)
39  TEMPLATABLE_VALUE(float, white)
40  TEMPLATABLE_VALUE(float, color_temperature)
41  TEMPLATABLE_VALUE(float, cold_white)
42  TEMPLATABLE_VALUE(float, warm_white)
43  TEMPLATABLE_VALUE(std::string, effect)
44 
45  void play(Ts... x) override {
46  auto call = this->parent_->make_call();
47  call.set_color_mode(this->color_mode_.optional_value(x...));
48  call.set_state(this->state_.optional_value(x...));
49  call.set_brightness(this->brightness_.optional_value(x...));
50  call.set_color_brightness(this->color_brightness_.optional_value(x...));
51  call.set_red(this->red_.optional_value(x...));
52  call.set_green(this->green_.optional_value(x...));
53  call.set_blue(this->blue_.optional_value(x...));
54  call.set_white(this->white_.optional_value(x...));
55  call.set_color_temperature(this->color_temperature_.optional_value(x...));
56  call.set_cold_white(this->cold_white_.optional_value(x...));
57  call.set_warm_white(this->warm_white_.optional_value(x...));
58  call.set_effect(this->effect_.optional_value(x...));
59  call.set_flash_length(this->flash_length_.optional_value(x...));
60  call.set_transition_length(this->transition_length_.optional_value(x...));
61  call.perform();
62  }
63 
64  protected:
65  LightState *parent_;
66 };
67 
68 template<typename... Ts> class DimRelativeAction : public Action<Ts...> {
69  public:
70  explicit DimRelativeAction(LightState *parent) : parent_(parent) {}
71 
72  TEMPLATABLE_VALUE(float, relative_brightness)
73  TEMPLATABLE_VALUE(uint32_t, transition_length)
74 
75  void play(Ts... x) override {
76  auto call = this->parent_->make_call();
77  float rel = this->relative_brightness_.value(x...);
78  float cur;
79  this->parent_->remote_values.as_brightness(&cur);
80  float new_brightness = clamp(cur + rel, 0.0f, 1.0f);
81  call.set_state(new_brightness != 0.0f);
82  call.set_brightness(new_brightness);
83 
84  call.set_transition_length(this->transition_length_.optional_value(x...));
85  call.perform();
86  }
87 
88  protected:
90 };
91 
92 template<typename... Ts> class LightIsOnCondition : public Condition<Ts...> {
93  public:
94  explicit LightIsOnCondition(LightState *state) : state_(state) {}
95  bool check(Ts... x) override { return this->state_->current_values.is_on(); }
96 
97  protected:
99 };
100 template<typename... Ts> class LightIsOffCondition : public Condition<Ts...> {
101  public:
103  bool check(Ts... x) override { return !this->state_->current_values.is_on(); }
104 
105  protected:
107 };
108 
109 class LightTurnOnTrigger : public Trigger<> {
110  public:
112  a_light->add_new_remote_values_callback([this, a_light]() {
113  // using the remote value because of transitions we need to trigger as early as possible
114  auto is_on = a_light->remote_values.is_on();
115  // only trigger when going from off to on
116  auto should_trigger = is_on && !this->last_on_;
117  // Set new state immediately so that trigger() doesn't devolve
118  // into infinite loop
119  this->last_on_ = is_on;
120  if (should_trigger) {
121  this->trigger();
122  }
123  });
124  this->last_on_ = a_light->current_values.is_on();
125  }
126 
127  protected:
128  bool last_on_;
129 };
130 
131 class LightTurnOffTrigger : public Trigger<> {
132  public:
134  a_light->add_new_target_state_reached_callback([this, a_light]() {
135  auto is_on = a_light->current_values.is_on();
136  // only trigger when going from on to off
137  if (!is_on) {
138  this->trigger();
139  }
140  });
141  }
142 };
143 
144 class LightStateTrigger : public Trigger<> {
145  public:
147  a_light->add_new_remote_values_callback([this]() { this->trigger(); });
148  }
149 };
150 
151 // This is slightly ugly, but we can't log in headers, and can't make this a static method on AddressableSet
152 // due to the template. It's just a temporary warning anyway.
153 void addressableset_warn_about_scale(const char *field);
154 
155 template<typename... Ts> class AddressableSet : public Action<Ts...> {
156  public:
157  explicit AddressableSet(LightState *parent) : parent_(parent) {}
158 
159  TEMPLATABLE_VALUE(int32_t, range_from)
160  TEMPLATABLE_VALUE(int32_t, range_to)
161  TEMPLATABLE_VALUE(float, color_brightness)
162  TEMPLATABLE_VALUE(float, red)
163  TEMPLATABLE_VALUE(float, green)
164  TEMPLATABLE_VALUE(float, blue)
165  TEMPLATABLE_VALUE(float, white)
166 
167  void play(Ts... x) override {
168  auto *out = (AddressableLight *) this->parent_->get_output();
169  int32_t range_from = interpret_index(this->range_from_.value_or(x..., 0), out->size());
170  if (range_from < 0 || range_from >= out->size())
171  range_from = 0;
172 
173  int32_t range_to = interpret_index(this->range_to_.value_or(x..., out->size() - 1) + 1, out->size());
174  if (range_to < 0 || range_to >= out->size())
175  range_to = out->size();
176 
177  uint8_t color_brightness =
178  to_uint8_scale(this->color_brightness_.value_or(x..., this->parent_->remote_values.get_color_brightness()));
179  auto range = out->range(range_from, range_to);
180  if (this->red_.has_value())
181  range.set_red(esp_scale8(to_uint8_compat(this->red_.value(x...), "red"), color_brightness));
182  if (this->green_.has_value())
183  range.set_green(esp_scale8(to_uint8_compat(this->green_.value(x...), "green"), color_brightness));
184  if (this->blue_.has_value())
185  range.set_blue(esp_scale8(to_uint8_compat(this->blue_.value(x...), "blue"), color_brightness));
186  if (this->white_.has_value())
187  range.set_white(to_uint8_compat(this->white_.value(x...), "white"));
188  out->schedule_show();
189  }
190 
191  protected:
193 
194  // Historically, this action required uint8_t (0-255) for RGBW values from lambdas. Keep compatibility.
195  static inline uint8_t to_uint8_compat(float value, const char *field) {
196  if (value > 1.0f) {
198  return static_cast<uint8_t>(value);
199  }
200  return to_uint8_scale(value);
201  }
202 };
203 
204 } // namespace light
205 } // namespace esphome
ToggleAction(LightState *state)
Definition: automation.h:12
ColorMode
Color modes are a combination of color capabilities that can be used at the same time.
Definition: color_mode.h:49
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:34
bool is_on() const
Get the binary true/false state of these light color values.
LightColorValues current_values
The current values of the light as outputted to the light.
Definition: light_state.h:65
void addressableset_warn_about_scale(const char *field)
Definition: automation.cpp:9
uint16_t x
Definition: tt21100.cpp:17
void add_new_target_state_reached_callback(std::function< void()> &&send_callback)
The callback is called once the state of current_values and remote_values are equal (when the transit...
int32_t HOT interpret_index(int32_t index, int32_t size)
LightTurnOffTrigger(LightState *a_light)
Definition: automation.h:133
LightCall & set_transition_length(optional< uint32_t > transition_length)
Set the transition length of this call in milliseconds.
Definition: light_call.cpp:560
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: helpers.h:92
LightIsOffCondition(LightState *state)
Definition: automation.h:102
AddressableSet(LightState *parent)
Definition: automation.h:157
DimRelativeAction(LightState *parent)
Definition: automation.h:70
Base class for all automation conditions.
Definition: automation.h:74
TEMPLATABLE_VALUE(uint32_t, transition_length) void play(Ts... x) override
Definition: automation.h:14
bool check(Ts... x) override
Definition: automation.h:103
LightStateTrigger(LightState *a_light)
Definition: automation.h:146
void add_new_remote_values_callback(std::function< void()> &&send_callback)
This lets front-end components subscribe to light change events.
bool check(Ts... x) override
Definition: automation.h:95
LightControlAction(LightState *parent)
Definition: automation.h:28
static uint8_t to_uint8_compat(float value, const char *field)
Definition: automation.h:195
LightTurnOnTrigger(LightState *a_light)
Definition: automation.h:111
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
LightColorValues remote_values
The remote color values reported to the frontend.
Definition: light_state.h:77
LightIsOnCondition(LightState *state)
Definition: automation.h:94
virtual void play(Ts... x)=0
bool state
Definition: fan.h:34