diff --git a/esphome/core/automation.h b/esphome/core/automation.h index 355968b284..7a054f9899 100644 --- a/esphome/core/automation.h +++ b/esphome/core/automation.h @@ -54,6 +54,9 @@ template class TemplatableFn { std::invocable &&std::convertible_to, T> &&std::is_empty_v &&std::default_initializable : f_([](X... x) -> T { return static_cast(F{}(x...)); }) {} + // Reject stateful lambdas (non-empty, i.e. capturing) with a clear error + template TemplatableFn(F) requires std::invocable &&(!std::is_empty_v) = delete; + bool has_value() const { return this->f_ != nullptr; } T value(X... x) const { return this->f_ ? this->f_(x...) : T{}; }