26 template<
typename F>
TemplatableFn(F f)
requires std::convertible_to<F, T (*)(X...)> :
f_(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...)); }) {}
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;
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/");
57 bool has_value()
const {
return this->f_ !=
nullptr; }
59 T
value(X...
x)
const {
return this->f_ ? this->f_(
x...) : T{}; }
64 return this->f_(
x...);
67 T
value_or(X...
x, T default_value)
const {
return this->f_ ? this->f_(
x...) : default_value; }
70 T (*f_)(X...){
nullptr};
78 this->code_.func = func;
82 this->code_.data = data;
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 {
92 return this->code_.func(
x...);
93 return to_vector(this->code_.data, this->size());
97 template<
size_t N = 64,
typename F>
void visit(F &&fn,
const Ts &...
x)
const {
99 const std::vector<uint8_t> bytes = this->code_.func(
x...);
100 fn(bytes.data(), bytes.size());
107 fn(buf.
get(), this->size());
109 fn(this->code_.data, this->size());
114 static std::vector<uint8_t>
to_vector(
const uint8_t *data,
size_t len) {
115 std::vector<uint8_t>
out(
len);
123 std::vector<uint8_t> (*func)(Ts...);
129#define TEMPLATABLE_BYTES(name) \
131 TemplatableBytes<Ts...> name##_{}; \
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); }
138template<
typename T,
typename... X>
class TemplatableValue;
143template<
typename T,
typename... X>
147#define TEMPLATABLE_VALUE_(type, name) \
149 TemplatableStorage<type, Ts...> name##_{}; \
152 template<typename V> void set_##name(V name) { this->name##_ = name; }
154#define TEMPLATABLE_VALUE(type, name) TEMPLATABLE_VALUE_(type, name)
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)));
170 template<
typename F>
TemplatableValue(F f)
requires std::convertible_to<F, T (*)(X...)> : tag_(FN) {
171 this->storage_.f_ = f;
176 [[deprecated(
"Lambda return type does not match TemplatableValue<T> — use the correct type in "
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...)); };
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;
191 if (this->tag_ == VALUE) {
193 }
else if (this->tag_ == FN) {
199 if (this->tag_ == VALUE) {
200 new (&this->storage_.value_) T(std::move(other.storage_.value_));
202 }
else if (this->tag_ == FN) {
203 this->storage_.f_ = other.storage_.f_;
209 if (
this != &other) {
211 this->tag_ = other.tag_;
212 if (this->tag_ == VALUE) {
214 }
else if (this->tag_ == FN) {
222 if (
this != &other) {
224 this->tag_ = other.tag_;
225 if (this->tag_ == VALUE) {
226 new (&this->storage_.value_) T(std::move(other.storage_.value_));
228 }
else if (this->tag_ == FN) {
229 this->storage_.f_ = other.storage_.f_;
241 if (this->tag_ == FN)
242 return this->storage_.f_(
x...);
243 if (this->tag_ == VALUE)
244 return this->storage_.value_;
249 if (this->tag_ == NONE)
251 return this->value(
x...);
255 if (this->tag_ == NONE)
256 return default_value;
257 return this->value(
x...);
262 if constexpr (!std::is_trivially_destructible_v<T>) {
263 if (this->tag_ == VALUE)
264 this->storage_.value_.~T();
268 enum Tag : uint8_t { NONE, VALUE, FN } tag_{NONE};
295 this->static_str_ =
reinterpret_cast<const char *
>(str);
299 template<
typename F>
TemplatableValue(F value)
requires(!std::invocable<F, X...>) : type_(VALUE) {
300 this->value_ =
new std::string(std::move(value));
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;
312 TemplatableValue(F f)
requires std::invocable<F, X...> &&(!std::convertible_to<F, std::string (*)(X...)>)
314 this->f_ =
new std::function<std::string(X...)>(std::move(f));
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_;
332 if (this->type_ == VALUE) {
333 this->value_ = other.value_;
334 other.value_ =
nullptr;
335 }
else if (this->type_ == LAMBDA) {
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_;
348 if (
this != &other) {
356 if (
this != &other) {
364 if (this->type_ == VALUE) {
366 }
else if (this->type_ == LAMBDA) {
375 switch (this->type_) {
376 case STATELESS_LAMBDA:
377 return this->stateless_f_(
x...);
379 return (*this->f_)(
x...);
381 return *this->value_;
383 return std::string(this->static_str_);
395 if (!this->has_value())
397 return this->value(
x...);
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...);
419 switch (this->type_) {
423 return this->static_str_ ==
nullptr || this->static_str_[0] ==
'\0';
427 return this->static_str_ ==
nullptr ||
431 return this->value_->empty();
433 return this->value().empty();
445 switch (this->type_) {
449 if (this->static_str_ ==
nullptr)
451 return StringRef(this->static_str_, strlen(this->static_str_));
454 if (this->static_str_ ==
nullptr)
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';
466 return StringRef(this->value_->data(), this->value_->size());
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';
488 std::function<std::string(X...)> *
f_;
489 std::string (*stateless_f_)(X...);
499 std::conditional_t<std::is_copy_constructible_v<std::remove_cvref_t<T>>, std::remove_cvref_t<T>, T>;
512 return this->check_tuple_(tuple, std::make_index_sequence<
sizeof...(Ts)>{});
516 template<
size_t... S>
bool check_tuple_(
const std::tuple<Ts...> &tuple, std::index_sequence<S...> ) {
517 return this->check(std::get<S>(tuple)...);
521template<
typename... Ts>
class Automation;
528 inline void trigger(
const Ts &...
x) ESPHOME_ALWAYS_INLINE {
529 if (this->automation_parent_ ==
nullptr)
531 this->automation_parent_->trigger(
x...);
537 if (this->automation_parent_ ==
nullptr)
539 this->automation_parent_->stop();
543 if (this->automation_parent_ ==
nullptr)
545 return this->automation_parent_->is_running();
552template<
typename... Ts>
class ActionList;
557 this->num_running_++;
559 this->play_next_(
x...);
564 this->num_running_ = 0;
569 bool is_running() {
return this->num_running_ > 0 || this->is_running_next_(); }
574 int total = this->num_running_;
575 if (this->next_ !=
nullptr)
576 total += this->next_->num_running_total();
584 virtual void play(
const Ts &...
x) = 0;
586 if (this->num_running_ > 0) {
587 this->num_running_--;
588 if (this->next_ !=
nullptr) {
589 this->next_->play_complex(
x...);
593 template<
size_t... S>
void play_next_tuple_(
const std::tuple<Ts...> &tuple, std::index_sequence<S...> ) {
594 this->play_next_(std::get<S>(tuple)...);
597 this->play_next_tuple_(tuple, std::make_index_sequence<
sizeof...(Ts)>{});
602 if (this->next_ !=
nullptr) {
603 this->next_->stop_complex();
608 if (this->next_ ==
nullptr)
610 return this->next_->is_running();
624 Action<Ts...> **tail = &this->actions_;
625 while (*tail !=
nullptr)
626 tail = &(*tail)->
next_;
631 Action<Ts...> **tail = &this->actions_;
632 while (*tail !=
nullptr)
633 tail = &(*tail)->
next_;
634 for (
auto *action : actions) {
636 tail = &action->next_;
641 inline void play(
const Ts &...
x) ESPHOME_ALWAYS_INLINE {
642 if (this->actions_ !=
nullptr)
643 this->actions_->play_complex(
x...);
646 this->play_tuple_(tuple, std::make_index_sequence<
sizeof...(Ts)>{});
649 if (this->actions_ !=
nullptr)
650 this->actions_->stop_complex();
652 bool empty()
const {
return this->actions_ ==
nullptr; }
656 if (this->actions_ ==
nullptr)
658 return this->actions_->is_running();
662 if (this->actions_ ==
nullptr)
664 return this->actions_->num_running_total();
668 template<
size_t... S>
void play_tuple_(
const std::tuple<Ts...> &tuple, std::index_sequence<S...> ) {
669 this->play(std::get<S>(tuple)...);
686 void stop() { this->actions_.stop(); }
690 inline void trigger(
const Ts &...
x) ESPHOME_ALWAYS_INLINE { this->actions_.play(
x...); }
706 void operator()(
const Ts &...args)
const { this->automation->trigger(args...); }
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>);
bool is_running()
Check if this or any of the following actions are currently running.
void play_next_(const Ts &...x)
virtual void play(const Ts &...x)=0
void play_next_tuple_(const std::tuple< Ts... > &tuple)
int num_running_total()
The total number of actions that are currently running in this plus any of the following actions in t...
void play_next_tuple_(const std::tuple< Ts... > &tuple, std::index_sequence< S... >)
virtual void play_complex(const Ts &...x)
void add_action(Action< Ts... > *action)
void play_tuple(const std::tuple< Ts... > &tuple)
bool is_running()
Check if any action in this action list is currently running.
void add_actions(const std::initializer_list< Action< Ts... > * > &actions)
void play_tuple_(const std::tuple< Ts... > &tuple, std::index_sequence< S... >)
void play(const Ts &...x) ESPHOME_ALWAYS_INLINE
int num_running()
Return the number of actions in this action list that are currently running.
void add_action(Action< Ts... > *action)
int num_running()
Return the number of actions in the action part of this automation that are currently running.
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Automation(Trigger< Ts... > *trigger)
void add_actions(const std::initializer_list< Action< Ts... > * > &actions)
ActionList< Ts... > actions_
constexpr Automation() noexcept
Default constructor for use with TriggerForwarder (no Trigger object needed).
Base class for all automation conditions.
bool check_tuple_(const std::tuple< Ts... > &tuple, std::index_sequence< S... >)
bool check_tuple(const std::tuple< Ts... > &tuple)
Call check with a tuple of values as parameter.
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...
StringRef is a reference to a string owned by something else.
Byte payload that is either a stateless lambda or a static table, which may be in PROGMEM.
static std::vector< uint8_t > to_vector(const uint8_t *data, size_t len)
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,...
size_t size() const
Only valid when is_static().
void set_static(const uint8_t *data, uint16_t len)
void set_template(std::vector< uint8_t >(*func)(Ts...))
std::vector< uint8_t > value(const Ts &...x) const
const uint8_t * data() const
Only valid when is_static(); may point to PROGMEM, so read it with progmem_memcpy or progmem_read_byt...
Function-pointer-only templatable storage (4 bytes on 32-bit).
T value_or(X... x, T default_value) const
TemplatableFn(std::nullptr_t)=delete
optional< T > optional_value(X... x) const
TemplatableValue(const __FlashStringHelper *str)
TemplatableValue(TemplatableValue &&other) noexcept
TemplatableValue(const char *str)
TemplatableValue & operator=(const TemplatableValue &other)
bool is_empty() const
Check if the string value is empty without allocating.
bool is_static_string() const
Check if this holds a static string (const char* stored without allocation) The pointer is always dir...
TemplatableValue(const TemplatableValue &other)
TemplatableValue & operator=(TemplatableValue &&other) noexcept
TemplatableValue(F value)
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.
std::string value_or(X... x, std::string default_value) const
const char * get_static_string() const
Get the static string pointer (only valid if is_static_string() returns true) The pointer is always d...
std::string value(X... x) const
optional< std::string > optional_value(X... x) const
std::function< std::string(X...)> * f_
Primary TemplatableValue: stores either a constant value or a function pointer.
TemplatableValue(const TemplatableValue &other)
TemplatableValue & operator=(TemplatableValue &&other) noexcept
union esphome::TemplatableValue::Storage storage_
TemplatableValue(V value)
TemplatableValue(TemplatableValue &&other) noexcept
optional< T > optional_value(X... x) const
TemplatableValue()=default
TemplatableValue & operator=(const TemplatableValue &other)
T value_or(X... x, T default_value) const
TemplatableValue(F)=delete
TemplatableValue(std::nullptr_t)=delete
void stop_action()
Stop any action connected to this trigger.
bool is_action_running()
Returns true if any action connected to this trigger is running.
void set_automation_parent(Automation< Ts... > *automation_parent)
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
std::vector< uint8_t > & out
std::string progmem_string(ProgmemStr str)
Copies a string stored with ESPHOME_F into a std::string.
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...
void progmem_memcpy(void *dst, const void *src, size_t len)
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.
uint8_t progmem_read_byte(const uint8_t *addr)
const __FlashStringHelper * ProgmemStr
Callback forwarder that triggers an Automation directly.
Automation< Ts... > * automation
void operator()(const Ts &...args) const
Callback forwarder that triggers an Automation<> only when the bool arg is false.
void operator()(bool state) const
Callback forwarder that triggers an Automation<> only when the bool arg is true.
void operator()(bool state) const