From e8465bfcdafbaf80580f9acd25f5bfe4b07a9bf6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 8 Jan 2026 22:58:49 -1000 Subject: [PATCH] [select] Return std::string_view from current_option() --- esphome/components/api/api_connection.cpp | 3 ++- esphome/components/ld2410/ld2410.cpp | 7 ++++--- esphome/components/ld2412/ld2412.cpp | 7 ++++--- esphome/components/ld2450/ld2450.cpp | 6 ++++-- esphome/components/mqtt/mqtt_select.cpp | 2 +- esphome/components/prometheus/prometheus_handler.cpp | 2 +- esphome/components/select/select.cpp | 4 +++- esphome/components/select/select.h | 12 ++++++++---- esphome/components/web_server/web_server.cpp | 4 ++-- esphome/components/web_server/web_server.h | 3 ++- 10 files changed, 31 insertions(+), 19 deletions(-) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index fb3548d117..4291dedc20 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -914,7 +914,8 @@ uint16_t APIConnection::try_send_select_state(EntityBase *entity, APIConnection bool is_single) { auto *select = static_cast(entity); SelectStateResponse resp; - resp.state = StringRef(select->current_option()); + auto state = select->current_option(); + resp.state = StringRef(state.data(), state.size()); resp.missing_state = !select->has_state(); return fill_and_encode_entity_state(select, resp, SelectStateResponse::MESSAGE_TYPE, conn, remaining_size, is_single); } diff --git a/esphome/components/ld2410/ld2410.cpp b/esphome/components/ld2410/ld2410.cpp index c9b4333f7e..aead6b822d 100644 --- a/esphome/components/ld2410/ld2410.cpp +++ b/esphome/components/ld2410/ld2410.cpp @@ -442,7 +442,8 @@ bool LD2410Component::handle_ack_data_() { ESP_LOGV(TAG, "Baud rate change"); #ifdef USE_SELECT if (this->baud_rate_select_ != nullptr) { - ESP_LOGE(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->current_option()); + auto baud = this->baud_rate_select_->current_option(); + ESP_LOGE(TAG, "Change baud rate to %.*s and reinstall", (int) baud.size(), baud.data()); } #endif break; @@ -766,10 +767,10 @@ void LD2410Component::set_light_out_control() { #endif #ifdef USE_SELECT if (this->light_function_select_ != nullptr && this->light_function_select_->has_state()) { - this->light_function_ = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_select_->current_option()); + this->light_function_ = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_select_->current_option().data()); } if (this->out_pin_level_select_ != nullptr && this->out_pin_level_select_->has_state()) { - this->out_pin_level_ = find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->current_option()); + this->out_pin_level_ = find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->current_option().data()); } #endif this->set_config_mode_(true); diff --git a/esphome/components/ld2412/ld2412.cpp b/esphome/components/ld2412/ld2412.cpp index 620ac9886b..859b9ccef5 100644 --- a/esphome/components/ld2412/ld2412.cpp +++ b/esphome/components/ld2412/ld2412.cpp @@ -486,7 +486,8 @@ bool LD2412Component::handle_ack_data_() { ESP_LOGV(TAG, "Baud rate change"); #ifdef USE_SELECT if (this->baud_rate_select_ != nullptr) { - ESP_LOGW(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->current_option()); + auto baud = this->baud_rate_select_->current_option(); + ESP_LOGW(TAG, "Change baud rate to %.*s and reinstall", (int) baud.size(), baud.data()); } #endif break; @@ -790,7 +791,7 @@ void LD2412Component::set_basic_config() { 1, TOTAL_GATES, DEFAULT_PRESENCE_TIMEOUT, 0, #endif #ifdef USE_SELECT - find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->current_option()), + find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->current_option().data()), #else 0x01, // Default value if not using select #endif @@ -844,7 +845,7 @@ void LD2412Component::set_light_out_control() { #endif #ifdef USE_SELECT if (this->light_function_select_ != nullptr && this->light_function_select_->has_state()) { - this->light_function_ = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_select_->current_option()); + this->light_function_ = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_select_->current_option().data()); } #endif uint8_t value[2] = {this->light_function_, this->light_threshold_}; diff --git a/esphome/components/ld2450/ld2450.cpp b/esphome/components/ld2450/ld2450.cpp index 3b85694bc0..f31b486a75 100644 --- a/esphome/components/ld2450/ld2450.cpp +++ b/esphome/components/ld2450/ld2450.cpp @@ -637,7 +637,8 @@ bool LD2450Component::handle_ack_data_() { ESP_LOGV(TAG, "Baud rate change"); #ifdef USE_SELECT if (this->baud_rate_select_ != nullptr) { - ESP_LOGE(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->current_option()); + auto baud = this->baud_rate_select_->current_option(); + ESP_LOGE(TAG, "Change baud rate to %.*s and reinstall", (int) baud.size(), baud.data()); } #endif break; @@ -718,7 +719,8 @@ bool LD2450Component::handle_ack_data_() { this->publish_zone_type(); #ifdef USE_SELECT if (this->zone_type_select_ != nullptr) { - ESP_LOGV(TAG, "Change zone type to: %s", this->zone_type_select_->current_option()); + auto zone = this->zone_type_select_->current_option(); + ESP_LOGV(TAG, "Change zone type to: %.*s", (int) zone.size(), zone.data()); } #endif if (this->buffer_data_[10] == 0x00) { diff --git a/esphome/components/mqtt/mqtt_select.cpp b/esphome/components/mqtt/mqtt_select.cpp index 09d90ed46e..b8bb5b9126 100644 --- a/esphome/components/mqtt/mqtt_select.cpp +++ b/esphome/components/mqtt/mqtt_select.cpp @@ -43,7 +43,7 @@ void MQTTSelectComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCon } bool MQTTSelectComponent::send_initial_state() { if (this->select_->has_state()) { - return this->publish_state(this->select_->current_option()); + return this->publish_state(std::string(this->select_->current_option())); } else { return true; } diff --git a/esphome/components/prometheus/prometheus_handler.cpp b/esphome/components/prometheus/prometheus_handler.cpp index 88b357041a..e29d890d78 100644 --- a/esphome/components/prometheus/prometheus_handler.cpp +++ b/esphome/components/prometheus/prometheus_handler.cpp @@ -709,7 +709,7 @@ void PrometheusHandler::select_row_(AsyncResponseStream *stream, select::Select stream->print(ESPHOME_F("\",name=\"")); stream->print(relabel_name_(obj).c_str()); stream->print(ESPHOME_F("\",value=\"")); - stream->print(obj->current_option()); + stream->print(obj->current_option().data()); stream->print(ESPHOME_F("\"} ")); stream->print(ESPHOME_F("1.0")); stream->print(ESPHOME_F("\n")); diff --git a/esphome/components/select/select.cpp b/esphome/components/select/select.cpp index 28d7eb07d4..39d74eab9b 100644 --- a/esphome/components/select/select.cpp +++ b/esphome/components/select/select.cpp @@ -38,7 +38,9 @@ void Select::publish_state(size_t index) { #endif } -const char *Select::current_option() const { return this->has_state() ? this->option_at(this->active_index_) : ""; } +std::string_view Select::current_option() const { + return this->has_state() ? std::string_view(this->option_at(this->active_index_)) : std::string_view(); +} void Select::add_on_state_callback(std::function &&callback) { this->state_callback_.add(std::move(callback)); diff --git a/esphome/components/select/select.h b/esphome/components/select/select.h index 330d18ce6f..7b44ca952b 100644 --- a/esphome/components/select/select.h +++ b/esphome/components/select/select.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "esphome/core/component.h" #include "esphome/core/entity_base.h" #include "esphome/core/helpers.h" @@ -33,8 +35,8 @@ class Select : public EntityBase { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" - /// @deprecated Use current_option() instead. This member will be removed in ESPHome 2026.5.0. - ESPDEPRECATED("Use current_option() instead of .state. Will be removed in 2026.5.0", "2025.11.0") + /// @deprecated Use current_option() instead. This member will be removed in ESPHome 2026.7.0. + ESPDEPRECATED("Use current_option() instead of .state. Will be removed in 2026.7.0", "2026.1.0") std::string state{}; Select() = default; @@ -45,8 +47,10 @@ class Select : public EntityBase { void publish_state(const char *state); void publish_state(size_t index); - /// Return the currently selected option (as const char* from flash). - const char *current_option() const; + /// Return the currently selected option, or empty view if no state. + /// The returned view points to string literals from codegen (static storage). + /// Traits are set once at startup and valid for the lifetime of the program. + std::string_view current_option() const; /// Instantiate a SelectCall object to modify this select component's state. SelectCall make_call() { return SelectCall(this); } diff --git a/esphome/components/web_server/web_server.cpp b/esphome/components/web_server/web_server.cpp index cab177c182..27fe47842e 100644 --- a/esphome/components/web_server/web_server.cpp +++ b/esphome/components/web_server/web_server.cpp @@ -1416,11 +1416,11 @@ std::string WebServer::select_all_json_generator(WebServer *web_server, void *so auto *obj = (select::Select *) (source); return web_server->select_json_(obj, obj->has_state() ? obj->current_option() : "", DETAIL_ALL); } -std::string WebServer::select_json_(select::Select *obj, const char *value, JsonDetail start_config) { +std::string WebServer::select_json_(select::Select *obj, std::string_view value, JsonDetail start_config) { json::JsonBuilder builder; JsonObject root = builder.root(); - set_json_icon_state_value(root, obj, "select", value, value, start_config); + set_json_icon_state_value(root, obj, "select", value.data(), value.data(), start_config); if (start_config == DETAIL_ALL) { JsonArray opt = root[ESPHOME_F("option")].to(); for (auto &option : obj->traits.get_options()) { diff --git a/esphome/components/web_server/web_server.h b/esphome/components/web_server/web_server.h index 3e1dd867c6..f698f967d1 100644 --- a/esphome/components/web_server/web_server.h +++ b/esphome/components/web_server/web_server.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -588,7 +589,7 @@ class WebServer : public Controller, std::string text_json_(text::Text *obj, const std::string &value, JsonDetail start_config); #endif #ifdef USE_SELECT - std::string select_json_(select::Select *obj, const char *value, JsonDetail start_config); + std::string select_json_(select::Select *obj, std::string_view value, JsonDetail start_config); #endif #ifdef USE_CLIMATE std::string climate_json_(climate::Climate *obj, JsonDetail start_config);