From 2394ac276cfaec0943dc397495dfff0363ae1cfc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 3 Feb 2026 11:39:14 +0100 Subject: [PATCH] avoid duplicating --- .../template/select/template_select.cpp | 29 +++++++++++++++++++ .../template/select/template_select.h | 25 +++++----------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/esphome/components/template/select/template_select.cpp b/esphome/components/template/select/template_select.cpp index f6871cf186..e68729c2d4 100644 --- a/esphome/components/template/select/template_select.cpp +++ b/esphome/components/template/select/template_select.cpp @@ -3,6 +3,8 @@ namespace esphome::template_ { +static const char *const TAG = "template.select"; + void dump_config_helper(BaseTemplateSelect *sel_comp, bool optimistic, bool has_lambda, const size_t initial_option_index, bool restore_value) { LOG_SELECT("", "Template Select", sel_comp); @@ -16,4 +18,31 @@ void dump_config_helper(BaseTemplateSelect *sel_comp, bool optimistic, bool has_ YESNO(optimistic), sel_comp->option_at(initial_option_index), YESNO(restore_value)); } } + +void setup_initial(BaseTemplateSelect *sel_comp, size_t initial_index) { + ESP_LOGD(TAG, "State from initial: %s", sel_comp->option_at(initial_index)); + sel_comp->publish_state(initial_index); +} + +void setup_with_restore(BaseTemplateSelect *sel_comp, ESPPreferenceObject &pref, size_t initial_index) { + size_t index = initial_index; + if (pref.load(&index) && sel_comp->has_index(index)) { + ESP_LOGD(TAG, "State from restore: %s", sel_comp->option_at(index)); + } else { + index = initial_index; + ESP_LOGD(TAG, "State from initial (no valid stored index): %s", sel_comp->option_at(initial_index)); + } + sel_comp->publish_state(index); +} + +void update_lambda(BaseTemplateSelect *sel_comp, const optional &val) { + if (val.has_value()) { + if (!sel_comp->has_option(*val)) { + ESP_LOGE(TAG, "Lambda returned an invalid option: %s", (*val).c_str()); + return; + } + sel_comp->publish_state(*val); + } +} + } // namespace esphome::template_ diff --git a/esphome/components/template/select/template_select.h b/esphome/components/template/select/template_select.h index 739d13e298..5da6d732bd 100644 --- a/esphome/components/template/select/template_select.h +++ b/esphome/components/template/select/template_select.h @@ -8,12 +8,15 @@ #include "esphome/core/template_lambda.h" namespace esphome::template_ { -static const char *const TAG = "template.select"; + struct Empty {}; class BaseTemplateSelect : public select::Select, public PollingComponent {}; void dump_config_helper(BaseTemplateSelect *sel_comp, bool optimistic, bool has_lambda, size_t initial_option_index, bool restore_value); +void setup_initial(BaseTemplateSelect *sel_comp, size_t initial_index); +void setup_with_restore(BaseTemplateSelect *sel_comp, ESPPreferenceObject &pref, size_t initial_index); +void update_lambda(BaseTemplateSelect *sel_comp, const optional &val); /// Base template select class - used when no set_action is configured @@ -28,32 +31,18 @@ class TemplateSelect : public BaseTemplateSelect { void setup() override { if constexpr (!HAS_LAMBDA) { - size_t index = INITIAL_OPTION_INDEX; if constexpr (RESTORE_VALUE) { this->pref_ = this->template make_entity_preference(); - if (this->pref_.load(&index) && this->has_index(index)) { - esph_log_d(TAG, "State from restore: %s", this->option_at(index)); - } else { - index = INITIAL_OPTION_INDEX; - esph_log_d(TAG, "State from initial (no valid stored index): %s", this->option_at(INITIAL_OPTION_INDEX)); - } + setup_with_restore(this, this->pref_, INITIAL_OPTION_INDEX); } else { - esph_log_d(TAG, "State from initial: %s", this->option_at(INITIAL_OPTION_INDEX)); + setup_initial(this, INITIAL_OPTION_INDEX); } - this->publish_state(index); } } void update() override { if constexpr (HAS_LAMBDA) { - auto val = this->f_(); - if (val.has_value()) { - if (!this->has_option(*val)) { - esph_log_e(TAG, "Lambda returned an invalid option: %s", (*val).c_str()); - return; - } - this->publish_state(*val); - } + update_lambda(this, this->f_()); } } void dump_config() override {