From d2f5fcd20169ef62e61990d9a79f9f1230f4ba81 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 27 Oct 2025 21:15:59 -0500 Subject: [PATCH] preen --- .../modbus_controller/select/modbus_select.cpp | 2 +- esphome/components/select/select.cpp | 2 ++ esphome/components/select/select.h | 3 +++ esphome/components/template/select/template_select.cpp | 9 ++++----- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/esphome/components/modbus_controller/select/modbus_select.cpp b/esphome/components/modbus_controller/select/modbus_select.cpp index 4d4b5a4ffc..48bf2835f2 100644 --- a/esphome/components/modbus_controller/select/modbus_select.cpp +++ b/esphome/components/modbus_controller/select/modbus_select.cpp @@ -28,7 +28,7 @@ void ModbusSelect::parse_and_publish(const std::vector &data) { if (map_it != this->mapping_.cend()) { size_t idx = std::distance(this->mapping_.cbegin(), map_it); - new_state = std::string(this->traits.get_options()[idx]); + new_state = std::string(this->option_at(idx)); ESP_LOGV(TAG, "Found option %s for value %lld", new_state->c_str(), value); } else { ESP_LOGE(TAG, "No option found for mapping %lld", value); diff --git a/esphome/components/select/select.cpp b/esphome/components/select/select.cpp index 66cd51e15a..5e30be3c13 100644 --- a/esphome/components/select/select.cpp +++ b/esphome/components/select/select.cpp @@ -60,5 +60,7 @@ optional Select::at(size_t index) const { } } +const char *Select::option_at(size_t index) const { return traits.get_options().at(index); } + } // namespace select } // namespace esphome diff --git a/esphome/components/select/select.h b/esphome/components/select/select.h index 902b8a78ce..eabb39898b 100644 --- a/esphome/components/select/select.h +++ b/esphome/components/select/select.h @@ -56,6 +56,9 @@ class Select : public EntityBase { /// Return the (optional) option value at the provided index offset. optional at(size_t index) const; + /// Return the option value at the provided index offset (as const char* from flash). + const char *option_at(size_t index) const; + void add_on_state_callback(std::function &&callback); protected: diff --git a/esphome/components/template/select/template_select.cpp b/esphome/components/template/select/template_select.cpp index 3765cf02bf..c7a1d8a344 100644 --- a/esphome/components/template/select/template_select.cpp +++ b/esphome/components/template/select/template_select.cpp @@ -16,12 +16,12 @@ void TemplateSelect::setup() { size_t restored_index; if (this->pref_.load(&restored_index) && this->has_index(restored_index)) { index = restored_index; - ESP_LOGD(TAG, "State from restore: %s", this->at(index).value().c_str()); + ESP_LOGD(TAG, "State from restore: %s", this->option_at(index)); } else { - ESP_LOGD(TAG, "State from initial (could not load or invalid stored index): %s", this->at(index).value().c_str()); + ESP_LOGD(TAG, "State from initial (could not load or invalid stored index): %s", this->option_at(index)); } } else { - ESP_LOGD(TAG, "State from initial: %s", this->at(index).value().c_str()); + ESP_LOGD(TAG, "State from initial: %s", this->option_at(index)); } this->publish_state(this->at(index).value()); @@ -64,8 +64,7 @@ void TemplateSelect::dump_config() { " Optimistic: %s\n" " Initial Option: %s\n" " Restore Value: %s", - YESNO(this->optimistic_), this->at(this->initial_option_index_).value().c_str(), - YESNO(this->restore_value_)); + YESNO(this->optimistic_), this->option_at(this->initial_option_index_), YESNO(this->restore_value_)); } } // namespace template_