ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
homeassistant_select.cpp
Go to the documentation of this file.
2
3#include "../homeassistant.h"
4
8#include "esphome/core/log.h"
11
12namespace esphome::homeassistant {
13
14ESPHOME_LOG_TAG(TAG, "homeassistant.select");
15
17 // Subscribe to the options first: Home Assistant answers subscriptions in order, so the options are
18 // known by the time the first state arrives.
20 this->entity_id_, "options", [this](StringRef options) { this->options_changed_(options); });
22 [this](StringRef state) { this->state_changed_(state); });
23}
24
26 // First pass: validate and measure the new options, and look for the active option among them, before
27 // anything is overwritten. A list that does not fit leaves the current options in place.
28 const char *active = this->has_state() ? this->option_at(this->active_index_) : nullptr;
29 size_t count = 0;
30 size_t bytes = 0;
31 size_t match_len = 0;
32 bool matching = active != nullptr;
33 optional<size_t> new_active;
35 options,
36 [&](char c) {
37 bytes++;
38 if (matching && active[match_len] == c) {
39 match_len++;
40 } else {
41 matching = false;
42 }
43 },
44 [&]() {
45 bytes++; // NUL terminator
46 if (matching && active[match_len] == '\0' && !new_active.has_value())
47 new_active = count;
48 count++;
49 match_len = 0;
50 matching = active != nullptr;
51 });
52 if (!valid) {
53 ESP_LOGW(TAG, "'%s': Can't parse options %s", this->entity_id_, options.c_str());
54 return;
55 }
56 if (count > this->max_options_) {
57 ESP_LOGE(TAG, "'%s': %zu options exceed max_options (%u)", this->entity_id_, count, this->max_options_);
58 return;
59 }
60 if (bytes > this->options_buffer_size_) {
61 ESP_LOGE(TAG, "'%s': Options need %zu bytes, more than options_buffer_size (%u)", this->entity_id_, bytes,
62 this->options_buffer_size_);
63 return;
64 }
65
66 // Second pass: the input is known to be valid and to fit, so write it out
67 char *buffer = this->options_buffer_;
68 size_t offset = 0;
69 size_t option_start = 0;
70 size_t index = 0;
72 options, [&](char c) { buffer[offset++] = c; },
73 [&]() {
74 buffer[offset++] = '\0';
75 this->option_list_[index++] = buffer + option_start;
76 option_start = offset;
77 });
78 // The traits only point at the static list; set_options() would copy it to the heap
79 this->traits.set_options_static(this->option_list_, count);
80
81 // The active option may have moved to another index, or may be gone
82 if (new_active.has_value()) {
83 this->active_index_ = *new_active;
84 } else {
85 this->set_has_state(false);
86 }
87 ESP_LOGD(TAG, "'%s': Got %zu options", this->entity_id_, count);
88}
89
91 auto index = this->index_of(state.c_str(), state.size());
92 if (!index.has_value()) {
93 ESP_LOGW(TAG, "'%s': State '%s' is not one of the options", this->entity_id_, state.c_str());
94 return;
95 }
96 if (this->has_state() && this->active_index_ == *index)
97 return;
98 ESP_LOGD(TAG, "'%s': Got state '%s'", this->entity_id_, state.c_str());
99 this->publish_state(*index);
100}
101
103 LOG_SELECT("", "Homeassistant Select", this);
104 ESP_LOGCONFIG(TAG,
105 " Entity ID: '%s'\n"
106 " Max options: %u\n"
107 " Options buffer size: %u",
108 this->entity_id_, this->max_options_, this->options_buffer_size_);
109}
110
112 if (!can_send_action(TAG, this->entity_id_, this))
113 return;
114
115 // In flash on the ESP8266 and encoded from there
116 static constexpr char SERVICE_SELECT[] PROGMEM = "select.select_option";
117 static constexpr char SERVICE_INPUT_SELECT[] PROGMEM = "input_select.select_option";
118 static constexpr char OPTION_KEY[] PROGMEM = "option";
119
121 resp.service = is_input_helper(this->entity_id_, SERVICE_INPUT_SELECT)
122 ? ProgmemStringRef::from_lit(SERVICE_INPUT_SELECT)
123 : ProgmemStringRef::from_lit(SERVICE_SELECT);
124
125 resp.data.init(2);
126 add_entity_id(resp, this->entity_id_);
127
128 auto &option = resp.data.emplace_back();
129 option.key = ProgmemStringRef::from_lit(OPTION_KEY);
130 option.value = ProgmemStringRef(StringRef(this->option_at(index)));
131
133}
134
135} // namespace esphome::homeassistant
void set_has_state(bool state)
bool has_state() const
A string that may live in flash (on ESP8266): pointer and length, with no byte-reading operations.
Definition string_ref.h:236
static constexpr ProgmemStringRef from_lit(const char(&s)[N])
A PROGMEM char array, with its length known at compile time.
Definition string_ref.h:242
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
constexpr const char * c_str() const
Definition string_ref.h:78
bool send_homeassistant_action(const HomeassistantActionRequest &call)
Returns false when no subscribed client took the request, so it was dropped.
void subscribe_home_assistant_state(const char *entity_id, const char *attribute, F &&f)
For internal components: entity_id and attribute must live as long as the server.
Definition api_server.h:279
FixedVector< HomeassistantServiceMap > data
Definition api_pb2.h:1514
const char * option_at(size_t index) const
Return the option value at the provided index offset (as const char* from flash).
Definition select.cpp:63
optional< size_t > index_of(const char *option, size_t len) const
Find the (optional) index offset of the provided option value.
Definition select.cpp:35
SelectTraits traits
Definition select.h:30
void publish_state(const std::string &state)
Definition select.h:35
void set_options_static(const char *const *options, size_t count)
Codegen only: points at a table that outlives the select.
uint8_t options
unsigned count
Definition gif.cpp:51
uint32_t buffer
Definition gif.cpp:50
APIServer * global_api_server
bool parse_python_list(StringRef src, C &&on_char, O &&on_item)
Walk a list attribute as Home Assistant sends it: the Python repr of a list of strings,...
void add_entity_id(api::HomeassistantActionRequest &resp, const char *entity_id)
Adds the entity_id entry to an action's data.
bool can_send_action(const char *tag, const char *entity_id, EntityBase *entity)
Logs and returns false when no API client is connected or no entity is linked yet.
ESPHOME_LOG_TAG(TAG, "homeassistant.binary_sensor")
bool is_input_helper(const char *entity_id, const char *input_service)
True if entity_id is an input helper; input_service must start with "input_".
static void const char const char * state
constexpr ColorMode COLOR_MODE_LOOKUP[] PROGMEM
Definition color_mode.h:114
bool valid