From 16130308f908d6a41c39a4669f68a73edee89077 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 25 Oct 2025 10:26:53 -0700 Subject: [PATCH 1/2] touch ups --- esphome/components/template/select/template_select.cpp | 4 +--- tests/integration/test_host_mode_empty_string_options.py | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/esphome/components/template/select/template_select.cpp b/esphome/components/template/select/template_select.cpp index a07215e77e..3765cf02bf 100644 --- a/esphome/components/template/select/template_select.cpp +++ b/esphome/components/template/select/template_select.cpp @@ -60,13 +60,11 @@ void TemplateSelect::dump_config() { LOG_UPDATE_INTERVAL(this); if (this->f_.has_value()) return; - auto initial_option = this->at(this->initial_option_index_); ESP_LOGCONFIG(TAG, " Optimistic: %s\n" " Initial Option: %s\n" " Restore Value: %s", - YESNO(this->optimistic_), - initial_option.has_value() ? initial_option.value().c_str() : LOG_STR_LITERAL("unknown"), + YESNO(this->optimistic_), this->at(this->initial_option_index_).value().c_str(), YESNO(this->restore_value_)); } diff --git a/tests/integration/test_host_mode_empty_string_options.py b/tests/integration/test_host_mode_empty_string_options.py index 1316e43a9f..1180ce75fc 100644 --- a/tests/integration/test_host_mode_empty_string_options.py +++ b/tests/integration/test_host_mode_empty_string_options.py @@ -129,7 +129,7 @@ async def test_host_mode_empty_string_options( initial_state = states[initial_option_test.key] assert initial_state.state == "Third", ( f"Expected initial state 'Third' but got '{initial_state.state}' - " - f"initial_option_index optimization may not be working correctly" + f"initial_option not correctly applied" ) # The main test is that we got here without protobuf errors From 3a491035845dc84f44b960c95c997b5a0f1a8ed1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 25 Oct 2025 10:31:13 -0700 Subject: [PATCH 2/2] touch ups --- esphome/components/template/select/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/esphome/components/template/select/__init__.py b/esphome/components/template/select/__init__.py index b998a1c2c7..93aa2c8b05 100644 --- a/esphome/components/template/select/__init__.py +++ b/esphome/components/template/select/__init__.py @@ -75,7 +75,10 @@ async def to_code(config): else: cg.add(var.set_optimistic(config[CONF_OPTIMISTIC])) initial_option_index = config[CONF_OPTIONS].index(config[CONF_INITIAL_OPTION]) - cg.add(var.set_initial_option_index(initial_option_index)) + # Only set if non-zero to avoid bloating setup() function + # (initial_option_index_ is zero-initialized in the header) + if initial_option_index != 0: + cg.add(var.set_initial_option_index(initial_option_index)) if CONF_RESTORE_VALUE in config: cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE]))