diff --git a/esphome/components/template/select/__init__.py b/esphome/components/template/select/__init__.py index 08c489a3f3..fd2fd62f59 100644 --- a/esphome/components/template/select/__init__.py +++ b/esphome/components/template/select/__init__.py @@ -17,6 +17,9 @@ from .. import template_ns TemplateSelect = template_ns.class_( "TemplateSelect", select.Select, cg.PollingComponent ) +TemplateSelectWithSetAction = template_ns.class_( + "TemplateSelectWithSetAction", TemplateSelect +) def validate(config): @@ -62,7 +65,9 @@ CONFIG_SCHEMA = cv.All( async def to_code(config): - var = cg.new_Pvariable(config[CONF_ID]) + # Use subclass with trigger only when set_action is configured + cls = TemplateSelectWithSetAction if CONF_SET_ACTION in config else TemplateSelect + var = cg.new_Pvariable(config[CONF_ID], cls) await cg.register_component(var, config) await select.register_select(var, config, options=config[CONF_OPTIONS]) @@ -87,7 +92,6 @@ async def to_code(config): cg.add(var.set_restore_value(True)) if CONF_SET_ACTION in config: - cg.add_define("USE_TEMPLATE_SELECT_SET_TRIGGER") await automation.build_automation( var.get_set_trigger(), [(cg.StringRef, "x")], config[CONF_SET_ACTION] ) diff --git a/esphome/components/template/select/template_select.cpp b/esphome/components/template/select/template_select.cpp index 2d1f48157f..0849e1a434 100644 --- a/esphome/components/template/select/template_select.cpp +++ b/esphome/components/template/select/template_select.cpp @@ -41,10 +41,6 @@ void TemplateSelect::update() { } void TemplateSelect::control(size_t index) { -#ifdef USE_TEMPLATE_SELECT_SET_TRIGGER - this->set_trigger_->trigger(StringRef(this->option_at(index))); -#endif - if (this->optimistic_) this->publish_state(index); @@ -52,6 +48,11 @@ void TemplateSelect::control(size_t index) { this->pref_.save(&index); } +void TemplateSelectWithSetAction::control(size_t index) { + this->set_trigger_.trigger(StringRef(this->option_at(index))); + TemplateSelect::control(index); +} + void TemplateSelect::dump_config() { LOG_SELECT("", "Template Select", this); LOG_UPDATE_INTERVAL(this); diff --git a/esphome/components/template/select/template_select.h b/esphome/components/template/select/template_select.h index ff5aff7bd3..2173d5ee24 100644 --- a/esphome/components/template/select/template_select.h +++ b/esphome/components/template/select/template_select.h @@ -9,7 +9,8 @@ namespace esphome::template_ { -class TemplateSelect final : public select::Select, public PollingComponent { +/// Base template select class - used when no set_action is configured +class TemplateSelect : public select::Select, public PollingComponent { public: template void set_template(F &&f) { this->f_.set(std::forward(f)); } @@ -18,9 +19,6 @@ class TemplateSelect final : public select::Select, public PollingComponent { void dump_config() override; float get_setup_priority() const override { return setup_priority::HARDWARE; } -#ifdef USE_TEMPLATE_SELECT_SET_TRIGGER - Trigger *get_set_trigger() const { return this->set_trigger_; } -#endif void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; } void set_initial_option_index(size_t initial_option_index) { this->initial_option_index_ = initial_option_index; } void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; } @@ -30,12 +28,19 @@ class TemplateSelect final : public select::Select, public PollingComponent { bool optimistic_ = false; size_t initial_option_index_{0}; bool restore_value_ = false; -#ifdef USE_TEMPLATE_SELECT_SET_TRIGGER - Trigger *set_trigger_ = new Trigger(); -#endif TemplateLambda f_; ESPPreferenceObject pref_; }; +/// Template select with set_action trigger - only instantiated when set_action is configured +class TemplateSelectWithSetAction final : public TemplateSelect { + public: + Trigger *get_set_trigger() { return &this->set_trigger_; } + + protected: + void control(size_t index) override; + Trigger set_trigger_; +}; + } // namespace esphome::template_