diff --git a/esphome/core/automation.h b/esphome/core/automation.h index 3b04373fc6..d286782293 100644 --- a/esphome/core/automation.h +++ b/esphome/core/automation.h @@ -62,9 +62,18 @@ template class TemplatableFn { T (*f_)(X...){nullptr}; }; +// Forward declaration for TemplatableValue (string specialization needs it) +template class TemplatableValue; + +/// Selects TemplatableFn (4 bytes) for non-string types, TemplatableValue (8 bytes) for std::string. +/// std::string needs TemplatableValue for const char*, __FlashStringHelper*, and PROGMEM support. +template +using TemplatableStorage = + std::conditional_t, TemplatableValue, TemplatableFn>; + #define TEMPLATABLE_VALUE_(type, name) \ protected: \ - TemplatableFn name##_{}; \ + TemplatableStorage name##_{}; \ \ public: \ template void set_##name(V name) { this->name##_ = name; }