ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
select.cpp
Go to the documentation of this file.
1#include "select.h"
4#include "esphome/core/log.h"
5#include <cstring>
6
7namespace esphome::select {
8
9static const char *const TAG = "select";
10
11void Select::publish_state(const char *state) {
12 auto index = this->index_of(state);
13 if (index.has_value()) {
14 this->publish_state(index.value());
15 } else {
16 ESP_LOGE(TAG, "'%s': Invalid option %s", this->get_name().c_str(), state);
17 }
18}
19
20void Select::publish_state(size_t index) {
21 if (!this->has_index(index)) {
22 ESP_LOGE(TAG, "'%s': Invalid index %zu", this->get_name().c_str(), index);
23 return;
24 }
25 const char *option = this->option_at(index);
26 this->set_has_state(true);
27 this->active_index_ = index;
28 ESP_LOGV(TAG, "'%s' >> %s (%zu)", this->get_name().c_str(), option, index);
29 this->state_callback_.call(index);
30#if defined(USE_SELECT) && defined(USE_CONTROLLER_REGISTRY)
31 ControllerRegistry::notify_select_update(this);
32#endif
33}
34
35optional<size_t> Select::index_of(const char *option, size_t len) const {
36 const auto &options = traits.get_options();
37 for (size_t i = 0; i < options.size(); i++) {
38 if (strncmp(options[i], option, len) == 0 && options[i][len] == '\0') {
39 return i;
40 }
41 }
42 return {};
43}
44
45optional<size_t> Select::active_index() const {
46 if (this->has_state()) {
47 return this->active_index_;
48 }
49 return {};
50}
51
52optional<std::string> Select::at(size_t index) const {
53 if (this->has_index(index)) {
54 const auto &options = traits.get_options();
55 return std::string(options.at(index));
56 }
57 return {};
58}
59
60const char *Select::option_at(size_t index) const { return traits.get_options().at(index); }
61
62} // namespace esphome::select
const StringRef & get_name() const
Definition entity_base.h:71
void set_has_state(bool state)
bool has_state() const
T & at(size_t i)
Access element with bounds checking (matches std::vector behavior) Note: No exception thrown on out o...
Definition helpers.h:723
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:60
optional< size_t > active_index() const
Return the (optional) index offset of the currently active option.
Definition select.cpp:45
LazyCallbackManager< void(size_t)> state_callback_
Definition select.h:112
optional< std::string > at(size_t index) const
Return the (optional) option value at the provided index offset.
Definition select.cpp:52
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
bool has_index(size_t index) const
Return whether this select component contains the provided index offset.
Definition select.h:55
SelectTraits traits
Definition select.h:31
void publish_state(const std::string &state)
Definition select.h:36
const FixedVector< const char * > & get_options() const
uint8_t options
bool state
Definition fan.h:2
const void size_t len
Definition hal.h:64