[select] Return std::string_view from current_option()

This commit is contained in:
J. Nick Koston
2026-01-08 22:58:49 -10:00
parent 7576e032f8
commit e8465bfcda
10 changed files with 31 additions and 19 deletions
+2 -1
View File
@@ -914,7 +914,8 @@ uint16_t APIConnection::try_send_select_state(EntityBase *entity, APIConnection
bool is_single) {
auto *select = static_cast<select::Select *>(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);
}
+4 -3
View File
@@ -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);
+4 -3
View File
@@ -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_};
+4 -2
View File
@@ -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) {
+1 -1
View File
@@ -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;
}
@@ -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"));
+3 -1
View File
@@ -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<void(size_t)> &&callback) {
this->state_callback_.add(std::move(callback));
+8 -4
View File
@@ -1,5 +1,7 @@
#pragma once
#include <string_view>
#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); }
+2 -2
View File
@@ -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<JsonArray>();
for (auto &option : obj->traits.get_options()) {
+2 -1
View File
@@ -15,6 +15,7 @@
#include <list>
#include <map>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
@@ -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);