ESPHome 2026.10.2
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
9#include <concepts>
10#include <functional>
11#include <type_traits>
12#include <utility>
13#include <vector>
14
15namespace esphome {
16
20template<typename T, typename... X> class TemplatableFn {
21 public:
22 TemplatableFn() = default;
23 TemplatableFn(std::nullptr_t) = delete;
24
25 // Exact return type match — direct function pointer storage
26 template<typename F> TemplatableFn(F f) requires std::convertible_to<F, T (*)(X...)> : f_(f) {}
27
28 // Convertible return type (e.g., int -> uint8_t) — casting trampoline.
29 // Stateless lambdas are default-constructible in C++20, so F{} recreates the lambda inside
30 // the trampoline without capturing. This compiles to the same code as a direct call + cast.
31 // Deprecated: codegen should use the correct output type to avoid the trampoline.
32 template<typename F>
33 [[deprecated("Lambda return type does not match TemplatableFn<T> — use the correct type in "
34 "codegen")]] TemplatableFn(F) requires(!std::convertible_to<F, T (*)(X...)>) &&
35 std::invocable<F, X...> &&std::convertible_to<std::invoke_result_t<F, X...>, T> &&std::is_empty_v<F>
36 &&std::default_initializable<F> : f_([](X... x) -> T { return static_cast<T>(F{}(x...)); }) {}
37
38 // Reject any callable that didn't match the above (stateful lambdas or inconvertible return types)
39 template<typename F>
40 TemplatableFn(F) requires std::invocable<F, X...> &&
41 (!std::convertible_to<F, T (*)(X...)>) &&(!std::is_empty_v<F> ||
42 !std::convertible_to<std::invoke_result_t<F, X...>, T> ||
43 !std::default_initializable<F>) = delete;
44
45 // Reject raw (non-callable) values with a helpful diagnostic pointing at the Python-side fix.
46 // TemplatableFn stores only a function pointer (4 bytes), so constants must be wrapped in a
47 // stateless lambda by codegen. External components hitting this error should use
48 // `cg.templatable(value, args, type)` in their Python __init__.py before passing to the setter.
49 template<typename V> TemplatableFn(V) requires(!std::invocable<V, X...>) && (!std::convertible_to<V, T (*)(X...)>) {
50 static_assert(sizeof(V) == 0, "Missing cg.templatable(...) in Python codegen for this TEMPLATABLE_VALUE "
51 "field. The wrapper was always required; it worked by accident because the old "
52 "TemplatableValue implicitly converted raw constants. TemplatableFn cannot. See "
53 "https://developers.esphome.io/blog/2026/04/09/"
54 "templatablefn-4-byte-templatable-storage-for-trivially-copyable-types/");
55 }
56
57 bool has_value() const { return this->f_ != nullptr; }
58
59 T value(X... x) const { return this->f_ ? this->f_(x...) : T{}; }
60
61 optional<T> optional_value(X... x) const {
62 if (!this->f_)
63 return {};
64 return this->f_(x...);
65 }
66
67 T value_or(X... x, T default_value) const { return this->f_ ? this->f_(x...) : default_value; }
68
69 protected:
70 T (*f_)(X...){nullptr};
71};
72
75template<typename... Ts> class TemplatableBytes {
76 public:
77 void set_template(std::vector<uint8_t> (*func)(Ts...)) {
78 this->code_.func = func;
79 this->len_ = -1;
80 }
81 void set_static(const uint8_t *data, uint16_t len) {
82 this->code_.data = data;
83 this->len_ = len;
84 }
85 bool is_static() const { return this->len_ >= 0; }
87 const uint8_t *data() const { return this->code_.data; }
89 size_t size() const { return static_cast<size_t>(this->len_); }
90 std::vector<uint8_t> value(const Ts &...x) const {
91 if (this->len_ < 0)
92 return this->code_.func(x...);
93 return to_vector(this->code_.data, this->size());
94 }
97 template<size_t N = 64, typename F> void visit(F &&fn, const Ts &...x) const {
98 if (this->len_ < 0) {
99 const std::vector<uint8_t> bytes = this->code_.func(x...);
100 fn(bytes.data(), bytes.size());
101 return;
102 }
103#ifdef USE_ESP8266
104 SmallBufferWithHeapFallback<N> buf(this->size());
105 if (this->len_ != 0)
106 progmem_memcpy(buf.get(), this->code_.data, this->size());
107 fn(buf.get(), this->size());
108#else
109 fn(this->code_.data, this->size());
110#endif
111 }
112
113 protected:
114 static std::vector<uint8_t> to_vector(const uint8_t *data, size_t len) {
115 std::vector<uint8_t> out(len);
116 // An empty payload is (nullptr, 0), and memcpy from nullptr is undefined even for zero bytes.
117 if (len != 0)
118 progmem_memcpy(out.data(), data, len); // byte loads from flash fault on ESP8266
119 return out;
120 }
121
122 union {
123 std::vector<uint8_t> (*func)(Ts...);
124 const uint8_t *data;
125 } code_{};
126 int32_t len_{-1}; // -1: lambda, otherwise the length of the static table
127};
128
129#define TEMPLATABLE_BYTES(name) \
130 protected: \
131 TemplatableBytes<Ts...> name##_{}; \
132\
133 public: \
134 void set_##name##_template(std::vector<uint8_t> (*func)(Ts...)) { this->name##_.set_template(func); } \
135 void set_##name##_static(const uint8_t *data, uint16_t len) { this->name##_.set_static(data, len); }
136
137// Forward declaration for TemplatableValue (string specialization needs it)
138template<typename T, typename... X> class TemplatableValue;
139
143template<typename T, typename... X>
145 std::conditional_t<std::is_trivially_copyable_v<T>, TemplatableFn<T, X...>, TemplatableValue<T, X...>>;
146
147#define TEMPLATABLE_VALUE_(type, name) \
148 protected: \
149 TemplatableStorage<type, Ts...> name##_{}; \
150\
151 public: \
152 template<typename V> void set_##name(V name) { this->name##_ = name; }
153
154#define TEMPLATABLE_VALUE(type, name) TEMPLATABLE_VALUE_(type, name)
155
159template<typename T, typename... X> class TemplatableValue {
160 public:
161 TemplatableValue() = default;
162 TemplatableValue(std::nullptr_t) = delete;
163
164 // Accept raw constants
165 template<typename V> TemplatableValue(V value) requires(!std::invocable<V, X...>) : tag_(VALUE) {
166 new (&this->storage_.value_) T(static_cast<T>(std::move(value)));
167 }
168
169 // Accept stateless lambdas (convertible to function pointer)
170 template<typename F> TemplatableValue(F f) requires std::convertible_to<F, T (*)(X...)> : tag_(FN) {
171 this->storage_.f_ = f;
172 }
173
174 // Convertible return type (e.g., int -> uint8_t) — casting trampoline
175 template<typename F>
176 [[deprecated("Lambda return type does not match TemplatableValue<T> — use the correct type in "
177 "codegen")]] TemplatableValue(F) requires(!std::convertible_to<F, T (*)(X...)>) &&
178 std::invocable<F, X...> &&std::convertible_to<std::invoke_result_t<F, X...>, T> &&std::is_empty_v<F>
179 &&std::default_initializable<F> : tag_(FN) {
180 this->storage_.f_ = [](X... x) -> T { return static_cast<T>(F{}(x...)); };
181 }
182
183 // Reject any callable that didn't match the above
184 template<typename F>
185 TemplatableValue(F) requires std::invocable<F, X...> &&
186 (!std::convertible_to<F, T (*)(X...)>) &&(!std::is_empty_v<F> ||
187 !std::convertible_to<std::invoke_result_t<F, X...>, T> ||
188 !std::default_initializable<F>) = delete;
189
190 TemplatableValue(const TemplatableValue &other) : tag_(other.tag_) {
191 if (this->tag_ == VALUE) {
192 new (&this->storage_.value_) T(other.storage_.value_);
193 } else if (this->tag_ == FN) {
194 this->storage_.f_ = other.storage_.f_;
195 }
196 }
197
198 TemplatableValue(TemplatableValue &&other) noexcept : tag_(other.tag_) {
199 if (this->tag_ == VALUE) {
200 new (&this->storage_.value_) T(std::move(other.storage_.value_));
201 other.destroy_();
202 } else if (this->tag_ == FN) {
203 this->storage_.f_ = other.storage_.f_;
204 }
205 other.tag_ = NONE;
206 }
207
209 if (this != &other) {
210 this->destroy_();
211 this->tag_ = other.tag_;
212 if (this->tag_ == VALUE) {
213 new (&this->storage_.value_) T(other.storage_.value_);
214 } else if (this->tag_ == FN) {
215 this->storage_.f_ = other.storage_.f_;
216 }
217 }
218 return *this;
219 }
220
222 if (this != &other) {
223 this->destroy_();
224 this->tag_ = other.tag_;
225 if (this->tag_ == VALUE) {
226 new (&this->storage_.value_) T(std::move(other.storage_.value_));
227 other.destroy_();
228 } else if (this->tag_ == FN) {
229 this->storage_.f_ = other.storage_.f_;
230 }
231 other.tag_ = NONE;
232 }
233 return *this;
234 }
235
236 ~TemplatableValue() { this->destroy_(); }
237
238 bool has_value() const { return this->tag_ != NONE; }
239
240 T value(X... x) const {
241 if (this->tag_ == FN)
242 return this->storage_.f_(x...);
243 if (this->tag_ == VALUE)
244 return this->storage_.value_;
245 return T{};
246 }
247
248 optional<T> optional_value(X... x) const {
249 if (this->tag_ == NONE)
250 return {};
251 return this->value(x...);
252 }
253
254 T value_or(X... x, T default_value) const {
255 if (this->tag_ == NONE)
256 return default_value;
257 return this->value(x...);
258 }
259
260 protected:
261 void destroy_() {
262 if constexpr (!std::is_trivially_destructible_v<T>) {
263 if (this->tag_ == VALUE)
264 this->storage_.value_.~T();
265 }
266 }
267
268 enum Tag : uint8_t { NONE, VALUE, FN } tag_{NONE};
269 // Union with explicit ctor/dtor to support non-trivially-constructible/destructible T
270 // (e.g., std::vector<uint8_t>). Lifetime of value_ is managed externally via
271 // placement new and destroy_().
272 union Storage {
273 constexpr Storage() : f_(nullptr) {}
274 constexpr ~Storage() {}
276 T (*f_)(X...);
277 } storage_;
278};
279
282template<typename... X> class TemplatableValue<std::string, X...> {
283 public:
284 TemplatableValue() : type_(NONE) {}
285
286 // For const char*: store pointer directly, no heap allocation.
287 // String remains in flash and is only converted to std::string when value() is called.
288 TemplatableValue(const char *str) : type_(STATIC_STRING) { this->static_str_ = str; }
289
290#ifdef USE_ESP8266
291 // On ESP8266, __FlashStringHelper* is a distinct type from const char*.
292 // ESPHOME_F(s) expands to F(s) which returns __FlashStringHelper* pointing to PROGMEM.
293 // Store as FLASH_STRING — value()/is_empty()/ref_or_copy_to() use _P functions.
294 TemplatableValue(const __FlashStringHelper *str) : type_(FLASH_STRING) {
295 this->static_str_ = reinterpret_cast<const char *>(str);
296 }
297#endif
298
299 template<typename F> TemplatableValue(F value) requires(!std::invocable<F, X...>) : type_(VALUE) {
300 this->value_ = new std::string(std::move(value));
301 }
302
303 // For stateless lambdas (convertible to function pointer): use function pointer
304 template<typename F>
305 TemplatableValue(F f) requires std::invocable<F, X...> && std::convertible_to<F, std::string (*)(X...)>
306 : type_(STATELESS_LAMBDA) {
307 this->stateless_f_ = f; // Implicit conversion to function pointer
308 }
309
310 // For stateful lambdas (not convertible to function pointer): use std::function
311 template<typename F>
312 TemplatableValue(F f) requires std::invocable<F, X...> &&(!std::convertible_to<F, std::string (*)(X...)>)
313 : type_(LAMBDA) {
314 this->f_ = new std::function<std::string(X...)>(std::move(f));
315 }
316
317 // Copy constructor
318 TemplatableValue(const TemplatableValue &other) : type_(other.type_) {
319 if (this->type_ == VALUE) {
320 this->value_ = new std::string(*other.value_);
321 } else if (this->type_ == LAMBDA) {
322 this->f_ = new std::function<std::string(X...)>(*other.f_);
323 } else if (this->type_ == STATELESS_LAMBDA) {
324 this->stateless_f_ = other.stateless_f_;
325 } else if (this->type_ == STATIC_STRING || this->type_ == FLASH_STRING) {
326 this->static_str_ = other.static_str_;
327 }
328 }
329
330 // Move constructor
331 TemplatableValue(TemplatableValue &&other) noexcept : type_(other.type_) {
332 if (this->type_ == VALUE) {
333 this->value_ = other.value_;
334 other.value_ = nullptr;
335 } else if (this->type_ == LAMBDA) {
336 this->f_ = other.f_;
337 other.f_ = nullptr;
338 } else if (this->type_ == STATELESS_LAMBDA) {
339 this->stateless_f_ = other.stateless_f_;
340 } else if (this->type_ == STATIC_STRING || this->type_ == FLASH_STRING) {
341 this->static_str_ = other.static_str_;
342 }
343 other.type_ = NONE;
344 }
345
346 // Assignment operators
348 if (this != &other) {
349 this->~TemplatableValue();
350 new (this) TemplatableValue(other);
351 }
352 return *this;
353 }
354
356 if (this != &other) {
357 this->~TemplatableValue();
358 new (this) TemplatableValue(std::move(other));
359 }
360 return *this;
361 }
362
364 if (this->type_ == VALUE) {
365 delete this->value_;
366 } else if (this->type_ == LAMBDA) {
367 delete this->f_;
368 }
369 // STATELESS_LAMBDA/STATIC_STRING/FLASH_STRING/NONE: no cleanup needed (pointers, not heap-allocated)
370 }
371
372 bool has_value() const { return this->type_ != NONE; }
373
374 std::string value(X... x) const {
375 switch (this->type_) {
376 case STATELESS_LAMBDA:
377 return this->stateless_f_(x...); // Direct function pointer call
378 case LAMBDA:
379 return (*this->f_)(x...); // std::function call
380 case VALUE:
381 return *this->value_;
382 case STATIC_STRING:
383 return std::string(this->static_str_);
384#ifdef USE_ESP8266
385 case FLASH_STRING:
386 return progmem_string(reinterpret_cast<ProgmemStr>(this->static_str_));
387#endif
388 case NONE:
389 default:
390 return {};
391 }
392 }
393
394 optional<std::string> optional_value(X... x) const {
395 if (!this->has_value())
396 return {};
397 return this->value(x...);
398 }
399
400 std::string value_or(X... x, std::string default_value) const {
401 if (!this->has_value())
402 return default_value;
403 return this->value(x...);
404 }
405
409 bool is_static_string() const { return this->type_ == STATIC_STRING; }
410
413 const char *get_static_string() const { return this->static_str_; }
414
418 bool is_empty() const {
419 switch (this->type_) {
420 case NONE:
421 return true;
422 case STATIC_STRING:
423 return this->static_str_ == nullptr || this->static_str_[0] == '\0';
424#ifdef USE_ESP8266
425 case FLASH_STRING:
426 // PROGMEM pointer — must use progmem_read_byte on ESP8266
427 return this->static_str_ == nullptr ||
428 progmem_read_byte(reinterpret_cast<const uint8_t *>(this->static_str_)) == '\0';
429#endif
430 case VALUE:
431 return this->value_->empty();
432 default: // LAMBDA/STATELESS_LAMBDA - must call value()
433 return this->value().empty();
434 }
435 }
436
444 StringRef ref_or_copy_to(char *lambda_buf, size_t lambda_buf_size) const {
445 switch (this->type_) {
446 case NONE:
447 return StringRef();
448 case STATIC_STRING:
449 if (this->static_str_ == nullptr)
450 return StringRef();
451 return StringRef(this->static_str_, strlen(this->static_str_));
452#ifdef USE_ESP8266
453 case FLASH_STRING:
454 if (this->static_str_ == nullptr)
455 return StringRef();
456 {
457 // PROGMEM pointer — copy to buffer via _P functions
458 size_t len = strlen_P(this->static_str_);
459 size_t copy_len = std::min(len, lambda_buf_size - 1);
460 memcpy_P(lambda_buf, this->static_str_, copy_len);
461 lambda_buf[copy_len] = '\0';
462 return StringRef(lambda_buf, copy_len);
463 }
464#endif
465 case VALUE:
466 return StringRef(this->value_->data(), this->value_->size());
467 default: { // LAMBDA/STATELESS_LAMBDA - must call value() and copy
468 std::string result = this->value();
469 size_t copy_len = std::min(result.size(), lambda_buf_size - 1);
470 memcpy(lambda_buf, result.data(), copy_len);
471 lambda_buf[copy_len] = '\0';
472 return StringRef(lambda_buf, copy_len);
473 }
474 }
475 }
476
477 protected:
478 enum : uint8_t {
483 STATIC_STRING, // For const char* — avoids heap allocation
484 FLASH_STRING, // PROGMEM pointer on ESP8266; never set on other platforms
485 } type_;
486 union {
487 std::string *value_; // Heap-allocated string (VALUE)
488 std::function<std::string(X...)> *f_; // Heap-allocated std::function (LAMBDA)
489 std::string (*stateless_f_)(X...); // Function pointer (STATELESS_LAMBDA)
490 const char *static_str_; // For STATIC_STRING and FLASH_STRING types
491 };
492};
493
497template<typename T>
499 std::conditional_t<std::is_copy_constructible_v<std::remove_cvref_t<T>>, std::remove_cvref_t<T>, T>;
500
505template<typename... Ts> class Condition {
506 public:
508 virtual bool check(const Ts &...x) = 0;
509
511 bool check_tuple(const std::tuple<Ts...> &tuple) {
512 return this->check_tuple_(tuple, std::make_index_sequence<sizeof...(Ts)>{});
513 }
514
515 protected:
516 template<size_t... S> bool check_tuple_(const std::tuple<Ts...> &tuple, std::index_sequence<S...> /*unused*/) {
517 return this->check(std::get<S>(tuple)...);
518 }
519};
520
521template<typename... Ts> class Automation;
522
523template<typename... Ts> class Trigger {
524 public:
526 // Force-inline: collapses the Trigger→Automation→ActionList forwarding
527 // chain into a single frame, reducing automation call stack depth.
528 inline void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE {
529 if (this->automation_parent_ == nullptr)
530 return;
531 this->automation_parent_->trigger(x...);
532 }
533 void set_automation_parent(Automation<Ts...> *automation_parent) { this->automation_parent_ = automation_parent; }
534
536 void stop_action() {
537 if (this->automation_parent_ == nullptr)
538 return;
539 this->automation_parent_->stop();
540 }
543 if (this->automation_parent_ == nullptr)
544 return false;
545 return this->automation_parent_->is_running();
546 }
547
548 protected:
549 Automation<Ts...> *automation_parent_{nullptr};
550};
551
552template<typename... Ts> class ActionList;
553
554template<typename... Ts> class Action {
555 public:
556 virtual void play_complex(const Ts &...x) {
557 this->num_running_++;
558 this->play(x...);
559 this->play_next_(x...);
560 }
562 if (num_running_) {
563 this->stop();
564 this->num_running_ = 0;
565 }
566 this->stop_next_();
567 }
569 bool is_running() { return this->num_running_ > 0 || this->is_running_next_(); }
570
574 int total = this->num_running_;
575 if (this->next_ != nullptr)
576 total += this->next_->num_running_total();
577 return total;
578 }
579
580 protected:
581 friend ActionList<Ts...>;
582 template<typename... Us> friend class ContinuationAction;
583
584 virtual void play(const Ts &...x) = 0;
585 void play_next_(const Ts &...x) {
586 if (this->num_running_ > 0) {
587 this->num_running_--;
588 if (this->next_ != nullptr) {
589 this->next_->play_complex(x...);
590 }
591 }
592 }
593 template<size_t... S> void play_next_tuple_(const std::tuple<Ts...> &tuple, std::index_sequence<S...> /*unused*/) {
594 this->play_next_(std::get<S>(tuple)...);
595 }
596 void play_next_tuple_(const std::tuple<Ts...> &tuple) {
597 this->play_next_tuple_(tuple, std::make_index_sequence<sizeof...(Ts)>{});
598 }
599
600 virtual void stop() {}
601 void stop_next_() {
602 if (this->next_ != nullptr) {
603 this->next_->stop_complex();
604 }
605 }
606
608 if (this->next_ == nullptr)
609 return false;
610 return this->next_->is_running();
611 }
612
613 Action<Ts...> *next_{nullptr};
614
617 int num_running_{0};
618};
619
620template<typename... Ts> class ActionList {
621 public:
622 void add_action(Action<Ts...> *action) {
623 // Walk to end of chain - action lists are short and only built during setup()
624 Action<Ts...> **tail = &this->actions_;
625 while (*tail != nullptr)
626 tail = &(*tail)->next_;
627 *tail = action;
628 }
629 void add_actions(const std::initializer_list<Action<Ts...> *> &actions) {
630 // Find tail once, then append all actions in a single pass
631 Action<Ts...> **tail = &this->actions_;
632 while (*tail != nullptr)
633 tail = &(*tail)->next_;
634 for (auto *action : actions) {
635 *tail = action;
636 tail = &action->next_;
637 }
638 }
639 // Force-inline: part of the Trigger→Automation→ActionList forwarding
640 // chain collapsed to reduce automation call stack depth.
641 inline void play(const Ts &...x) ESPHOME_ALWAYS_INLINE {
642 if (this->actions_ != nullptr)
643 this->actions_->play_complex(x...);
644 }
645 void play_tuple(const std::tuple<Ts...> &tuple) {
646 this->play_tuple_(tuple, std::make_index_sequence<sizeof...(Ts)>{});
647 }
648 void stop() {
649 if (this->actions_ != nullptr)
650 this->actions_->stop_complex();
651 }
652 bool empty() const { return this->actions_ == nullptr; }
653
655 bool is_running() {
656 if (this->actions_ == nullptr)
657 return false;
658 return this->actions_->is_running();
659 }
662 if (this->actions_ == nullptr)
663 return 0;
664 return this->actions_->num_running_total();
665 }
666
667 protected:
668 template<size_t... S> void play_tuple_(const std::tuple<Ts...> &tuple, std::index_sequence<S...> /*unused*/) {
669 this->play(std::get<S>(tuple)...);
670 }
671
672 Action<Ts...> *actions_{nullptr};
673};
674
675template<typename... Ts> class Automation {
676 public:
678 // User provided, not "= default": `new(p) Automation()` would zero-fill .bss that is already zero.
679 // constexpr and noexcept keep the rest of the implicit constructor's contract.
680 constexpr Automation() noexcept {}
681 explicit Automation(Trigger<Ts...> *trigger) { trigger->set_automation_parent(this); }
682
683 void add_action(Action<Ts...> *action) { this->actions_.add_action(action); }
684 void add_actions(const std::initializer_list<Action<Ts...> *> &actions) { this->actions_.add_actions(actions); }
685
686 void stop() { this->actions_.stop(); }
687
688 // Force-inline: part of the Trigger→Automation→ActionList forwarding
689 // chain collapsed to reduce automation call stack depth.
690 inline void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE { this->actions_.play(x...); }
691
692 bool is_running() { return this->actions_.is_running(); }
693
695 int num_running() { return this->actions_.num_running(); }
696
697 protected:
699};
700
704template<typename... Ts> struct TriggerForwarder {
706 void operator()(const Ts &...args) const { this->automation->trigger(args...); }
707};
708
713 void operator()(bool state) const {
714 if (state)
715 this->automation->trigger();
716 }
717};
718
723 void operator()(bool state) const {
724 if (!state)
725 this->automation->trigger();
726 }
727};
728
729// Ensure forwarders fit in Callback::ctx_ (pointer-sized inline storage).
730// If these fail, the forwarder would heap-allocate in Callback::create().
731static_assert(sizeof(TriggerForwarder<>) <= sizeof(void *));
732static_assert(sizeof(TriggerOnTrueForwarder) <= sizeof(void *));
733static_assert(sizeof(TriggerOnFalseForwarder) <= sizeof(void *));
734static_assert(std::is_trivially_copyable_v<TriggerForwarder<>>);
735static_assert(std::is_trivially_copyable_v<TriggerOnTrueForwarder>);
736static_assert(std::is_trivially_copyable_v<TriggerOnFalseForwarder>);
737
738} // namespace esphome
bool is_running()
Check if this or any of the following actions are currently running.
Definition automation.h:569
void stop_complex()
Definition automation.h:561
Action< Ts... > * next_
Definition automation.h:613
void play_next_(const Ts &...x)
Definition automation.h:585
virtual void play(const Ts &...x)=0
void play_next_tuple_(const std::tuple< Ts... > &tuple)
Definition automation.h:596
virtual void stop()
Definition automation.h:600
bool is_running_next_()
Definition automation.h:607
int num_running_total()
The total number of actions that are currently running in this plus any of the following actions in t...
Definition automation.h:573
void play_next_tuple_(const std::tuple< Ts... > &tuple, std::index_sequence< S... >)
Definition automation.h:593
virtual void play_complex(const Ts &...x)
Definition automation.h:556
void add_action(Action< Ts... > *action)
Definition automation.h:622
void play_tuple(const std::tuple< Ts... > &tuple)
Definition automation.h:645
bool is_running()
Check if any action in this action list is currently running.
Definition automation.h:655
bool empty() const
Definition automation.h:652
void add_actions(const std::initializer_list< Action< Ts... > * > &actions)
Definition automation.h:629
void play_tuple_(const std::tuple< Ts... > &tuple, std::index_sequence< S... >)
Definition automation.h:668
void play(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:641
int num_running()
Return the number of actions in this action list that are currently running.
Definition automation.h:661
void add_action(Action< Ts... > *action)
Definition automation.h:683
int num_running()
Return the number of actions in the action part of this automation that are currently running.
Definition automation.h:695
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:690
Automation(Trigger< Ts... > *trigger)
Definition automation.h:681
void add_actions(const std::initializer_list< Action< Ts... > * > &actions)
Definition automation.h:684
ActionList< Ts... > actions_
Definition automation.h:698
constexpr Automation() noexcept
Default constructor for use with TriggerForwarder (no Trigger object needed).
Definition automation.h:680
Base class for all automation conditions.
Definition automation.h:505
bool check_tuple_(const std::tuple< Ts... > &tuple, std::index_sequence< S... >)
Definition automation.h:516
bool check_tuple(const std::tuple< Ts... > &tuple)
Call check with a tuple of values as parameter.
Definition automation.h:511
virtual bool check(const Ts &...x)=0
Check whether this condition passes. This condition check must be instant, and not cause any delays.
Simple continuation action that calls play_next_ on a parent action.
Helper class for efficient buffer allocation - uses stack for small sizes, heap for large This is use...
Definition helpers.h:824
StringRef is a reference to a string owned by something else.
Definition string_ref.h:30
Byte payload that is either a stateless lambda or a static table, which may be in PROGMEM.
Definition automation.h:75
static std::vector< uint8_t > to_vector(const uint8_t *data, size_t len)
Definition automation.h:114
void visit(F &&fn, const Ts &...x) const
Calls fn(const uint8_t *data, size_t len) with the payload readable from RAM: a lambda's vector,...
Definition automation.h:97
size_t size() const
Only valid when is_static().
Definition automation.h:89
void set_static(const uint8_t *data, uint16_t len)
Definition automation.h:81
void set_template(std::vector< uint8_t >(*func)(Ts...))
Definition automation.h:77
std::vector< uint8_t > value(const Ts &...x) const
Definition automation.h:90
const uint8_t * data() const
Only valid when is_static(); may point to PROGMEM, so read it with progmem_memcpy or progmem_read_byt...
Definition automation.h:87
Function-pointer-only templatable storage (4 bytes on 32-bit).
Definition automation.h:20
T value_or(X... x, T default_value) const
Definition automation.h:67
bool has_value() const
Definition automation.h:57
TemplatableFn(std::nullptr_t)=delete
optional< T > optional_value(X... x) const
Definition automation.h:61
T value(X... x) const
Definition automation.h:59
TemplatableValue(const __FlashStringHelper *str)
Definition automation.h:294
TemplatableValue(TemplatableValue &&other) noexcept
Definition automation.h:331
TemplatableValue & operator=(const TemplatableValue &other)
Definition automation.h:347
bool is_empty() const
Check if the string value is empty without allocating.
Definition automation.h:418
bool is_static_string() const
Check if this holds a static string (const char* stored without allocation) The pointer is always dir...
Definition automation.h:409
TemplatableValue(const TemplatableValue &other)
Definition automation.h:318
TemplatableValue & operator=(TemplatableValue &&other) noexcept
Definition automation.h:355
StringRef ref_or_copy_to(char *lambda_buf, size_t lambda_buf_size) const
Get a StringRef to the string value without heap allocation when possible.
Definition automation.h:444
std::string value_or(X... x, std::string default_value) const
Definition automation.h:400
const char * get_static_string() const
Get the static string pointer (only valid if is_static_string() returns true) The pointer is always d...
Definition automation.h:413
optional< std::string > optional_value(X... x) const
Definition automation.h:394
std::function< std::string(X...)> * f_
Definition automation.h:488
Primary TemplatableValue: stores either a constant value or a function pointer.
Definition automation.h:159
TemplatableValue(const TemplatableValue &other)
Definition automation.h:190
TemplatableValue & operator=(TemplatableValue &&other) noexcept
Definition automation.h:221
union esphome::TemplatableValue::Storage storage_
TemplatableValue(TemplatableValue &&other) noexcept
Definition automation.h:198
T value(X... x) const
Definition automation.h:240
optional< T > optional_value(X... x) const
Definition automation.h:248
TemplatableValue & operator=(const TemplatableValue &other)
Definition automation.h:208
T value_or(X... x, T default_value) const
Definition automation.h:254
TemplatableValue(std::nullptr_t)=delete
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:536
bool is_action_running()
Returns true if any action connected to this trigger is running.
Definition automation.h:542
void set_automation_parent(Automation< Ts... > *automation_parent)
Definition automation.h:533
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:528
bool state
Definition fan.h:2
std::vector< uint8_t > & out
Definition gif.cpp:49
std::string progmem_string(ProgmemStr str)
Copies a string stored with ESPHOME_F into a std::string.
Definition helpers.cpp:23
std::conditional_t< std::is_copy_constructible_v< std::remove_cvref_t< T > >, std::remove_cvref_t< T >, T > stored_trigger_arg_t
How deferring actions store a trigger argument: a copy, since a referenced object may be gone once th...
Definition automation.h:498
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:48
std::conditional_t< std::is_trivially_copyable_v< T >, TemplatableFn< T, X... >, TemplatableValue< T, X... > > TemplatableStorage
Selects TemplatableFn (4 bytes) for trivially copyable types, TemplatableValue (8 bytes) otherwise.
Definition automation.h:144
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:43
STL namespace.
const __FlashStringHelper * ProgmemStr
Definition progmem.h:29
Callback forwarder that triggers an Automation directly.
Definition automation.h:704
Automation< Ts... > * automation
Definition automation.h:705
void operator()(const Ts &...args) const
Definition automation.h:706
Callback forwarder that triggers an Automation<> only when the bool arg is false.
Definition automation.h:721
void operator()(bool state) const
Definition automation.h:723
Callback forwarder that triggers an Automation<> only when the bool arg is true.
Definition automation.h:711
void operator()(bool state) const
Definition automation.h:713
uint32_t len
uint16_t x
Definition tt21100.cpp:5