mirror of
https://github.com/esphome/esphome.git
synced 2026-07-11 09:25:34 +00:00
stringref
This commit is contained in:
@@ -914,8 +914,7 @@ uint16_t APIConnection::try_send_select_state(EntityBase *entity, APIConnection
|
||||
bool is_single) {
|
||||
auto *select = static_cast<select::Select *>(entity);
|
||||
SelectStateResponse resp;
|
||||
auto state = select->current_option();
|
||||
resp.state = StringRef(state.data(), state.size());
|
||||
resp.state = select->current_option();
|
||||
resp.missing_state = !select->has_state();
|
||||
return fill_and_encode_entity_state(select, resp, SelectStateResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
|
||||
@@ -42,7 +42,8 @@ std::string MenuItemSelect::get_value_text() const {
|
||||
result = this->value_getter_.value()(this);
|
||||
} else {
|
||||
if (this->select_var_ != nullptr) {
|
||||
result = this->select_var_->current_option();
|
||||
auto option = this->select_var_->current_option();
|
||||
result.assign(option.c_str(), option.size());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -443,7 +443,7 @@ bool LD2410Component::handle_ack_data_() {
|
||||
#ifdef USE_SELECT
|
||||
if (this->baud_rate_select_ != nullptr) {
|
||||
auto baud = this->baud_rate_select_->current_option();
|
||||
ESP_LOGE(TAG, "Change baud rate to %.*s and reinstall", (int) baud.size(), baud.data());
|
||||
ESP_LOGE(TAG, "Change baud rate to %.*s and reinstall", (int) baud.size(), baud.c_str());
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
@@ -767,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().data());
|
||||
this->light_function_ = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_select_->current_option().c_str());
|
||||
}
|
||||
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().data());
|
||||
this->out_pin_level_ = find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->current_option().c_str());
|
||||
}
|
||||
#endif
|
||||
this->set_config_mode_(true);
|
||||
|
||||
@@ -487,7 +487,7 @@ bool LD2412Component::handle_ack_data_() {
|
||||
#ifdef USE_SELECT
|
||||
if (this->baud_rate_select_ != nullptr) {
|
||||
auto baud = this->baud_rate_select_->current_option();
|
||||
ESP_LOGW(TAG, "Change baud rate to %.*s and reinstall", (int) baud.size(), baud.data());
|
||||
ESP_LOGW(TAG, "Change baud rate to %.*s and reinstall", (int) baud.size(), baud.c_str());
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
@@ -791,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().data()),
|
||||
find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->current_option().c_str()),
|
||||
#else
|
||||
0x01, // Default value if not using select
|
||||
#endif
|
||||
@@ -845,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().data());
|
||||
this->light_function_ = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_select_->current_option().c_str());
|
||||
}
|
||||
#endif
|
||||
uint8_t value[2] = {this->light_function_, this->light_threshold_};
|
||||
|
||||
@@ -638,7 +638,7 @@ bool LD2450Component::handle_ack_data_() {
|
||||
#ifdef USE_SELECT
|
||||
if (this->baud_rate_select_ != nullptr) {
|
||||
auto baud = this->baud_rate_select_->current_option();
|
||||
ESP_LOGE(TAG, "Change baud rate to %.*s and reinstall", (int) baud.size(), baud.data());
|
||||
ESP_LOGE(TAG, "Change baud rate to %.*s and reinstall", (int) baud.size(), baud.c_str());
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
@@ -720,7 +720,7 @@ bool LD2450Component::handle_ack_data_() {
|
||||
#ifdef USE_SELECT
|
||||
if (this->zone_type_select_ != nullptr) {
|
||||
auto zone = this->zone_type_select_->current_option();
|
||||
ESP_LOGV(TAG, "Change zone type to: %.*s", (int) zone.size(), zone.data());
|
||||
ESP_LOGV(TAG, "Change zone type to: %.*s", (int) zone.size(), zone.c_str());
|
||||
}
|
||||
#endif
|
||||
if (this->buffer_data_[10] == 0x00) {
|
||||
|
||||
@@ -43,7 +43,8 @@ void MQTTSelectComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCon
|
||||
}
|
||||
bool MQTTSelectComponent::send_initial_state() {
|
||||
if (this->select_->has_state()) {
|
||||
return this->publish_state(std::string(this->select_->current_option()));
|
||||
auto option = this->select_->current_option();
|
||||
return this->publish_state(std::string(option.c_str(), option.size()));
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -709,8 +709,8 @@ 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=\""));
|
||||
// current_option() returns string_view pointing to null-terminated string literals from codegen
|
||||
stream->print(obj->current_option().data());
|
||||
auto option = obj->current_option();
|
||||
stream->write(option.c_str(), option.size());
|
||||
stream->print(ESPHOME_F("\"} "));
|
||||
stream->print(ESPHOME_F("1.0"));
|
||||
stream->print(ESPHOME_F("\n"));
|
||||
|
||||
@@ -38,8 +38,8 @@ void Select::publish_state(size_t index) {
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string_view Select::current_option() const {
|
||||
return this->has_state() ? std::string_view(this->option_at(this->active_index_)) : std::string_view();
|
||||
StringRef Select::current_option() const {
|
||||
return this->has_state() ? StringRef(this->option_at(this->active_index_)) : StringRef();
|
||||
}
|
||||
|
||||
void Select::add_on_state_callback(std::function<void(size_t)> &&callback) {
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/entity_base.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/string_ref.h"
|
||||
#include "select_call.h"
|
||||
#include "select_traits.h"
|
||||
|
||||
@@ -47,10 +46,10 @@ class Select : public EntityBase {
|
||||
void publish_state(const char *state);
|
||||
void publish_state(size_t index);
|
||||
|
||||
/// Return the currently selected option, or empty view if no state.
|
||||
/// The returned view points to string literals from codegen (static storage).
|
||||
/// Return the currently selected option, or empty StringRef if no state.
|
||||
/// The returned StringRef 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;
|
||||
StringRef current_option() const;
|
||||
|
||||
/// Instantiate a SelectCall object to modify this select component's state.
|
||||
SelectCall make_call() { return SelectCall(this); }
|
||||
|
||||
@@ -1420,12 +1420,12 @@ 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, std::string_view value, JsonDetail start_config) {
|
||||
std::string WebServer::select_json_(select::Select *obj, StringRef value, JsonDetail start_config) {
|
||||
json::JsonBuilder builder;
|
||||
JsonObject root = builder.root();
|
||||
|
||||
// value points to null-terminated string literals from codegen (via current_option())
|
||||
set_json_icon_state_value(root, obj, "select", value.data(), value.data(), start_config);
|
||||
set_json_icon_state_value(root, obj, "select", value.c_str(), value.c_str(), start_config);
|
||||
if (start_config == DETAIL_ALL) {
|
||||
JsonArray opt = root[ESPHOME_F("option")].to<JsonArray>();
|
||||
for (auto &option : obj->traits.get_options()) {
|
||||
|
||||
@@ -628,7 +628,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, std::string_view value, JsonDetail start_config);
|
||||
std::string select_json_(select::Select *obj, StringRef value, JsonDetail start_config);
|
||||
#endif
|
||||
#ifdef USE_CLIMATE
|
||||
std::string climate_json_(climate::Climate *obj, JsonDetail start_config);
|
||||
|
||||
Reference in New Issue
Block a user