ESPHome 2026.5.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 std::string &state) { this->publish_state(state.c_str()); }
12
13void Select::publish_state(const char *state) {
14 auto index = this->index_of(state);
15 if (index.has_value()) {
16 this->publish_state(index.value());
17 } else {
18 ESP_LOGE(TAG, "'%s': Invalid option %s", this->get_name().c_str(), state);
19 }
20}
21
22void Select::publish_state(size_t index) {
23 if (!this->has_index(index)) {
24 ESP_LOGE(TAG, "'%s': Invalid index %zu", this->get_name().c_str(), index);
25 return;
26 }
27 const char *option = this->option_at(index);
28 this->set_has_state(true);
29 this->active_index_ = index;
30#pragma GCC diagnostic push
31#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
32 this->state = option; // Update deprecated member for backward compatibility
33#pragma GCC diagnostic pop
34 ESP_LOGV(TAG, "'%s' >> %s (%zu)", this->get_name().c_str(), option, index);
35 this->state_callback_.call(index);
36#if defined(USE_SELECT) && defined(USE_CONTROLLER_REGISTRY)
37 ControllerRegistry::notify_select_update(this);
38#endif
39}
40
42 return this->has_state() ? StringRef(this->option_at(this->active_index_)) : StringRef();
43}
44
45bool Select::has_option(const std::string &option) const { return this->index_of(option.c_str()).has_value(); }
46
47bool Select::has_option(const char *option) const { return this->index_of(option).has_value(); }
48
49bool Select::has_index(size_t index) const { return index < this->size(); }
50
51size_t Select::size() const {
52 const auto &options = traits.get_options();
53 return options.size();
54}
55
56optional<size_t> Select::index_of(const char *option, size_t len) const {
57 const auto &options = traits.get_options();
58 for (size_t i = 0; i < options.size(); i++) {
59 if (strncmp(options[i], option, len) == 0 && options[i][len] == '\0') {
60 return i;
61 }
62 }
63 return {};
64}
65
66optional<size_t> Select::active_index() const {
67 if (this->has_state()) {
68 return this->active_index_;
69 }
70 return {};
71}
72
73optional<std::string> Select::at(size_t index) const {
74 if (this->has_index(index)) {
75 const auto &options = traits.get_options();
76 return std::string(options.at(index));
77 }
78 return {};
79}
80
81const char *Select::option_at(size_t index) const { return traits.get_options().at(index); }
82
83} // namespace esphome::select
const StringRef & get_name() const
Definition entity_base.h:71
void set_has_state(bool state)
bool has_state() const
size_t size() const
Definition helpers.h:679
T & at(size_t i)
Access element with bounds checking (matches std::vector behavior) Note: No exception thrown on out o...
Definition helpers.h:691
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
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:81
optional< size_t > active_index() const
Return the (optional) index offset of the currently active option.
Definition select.cpp:66
LazyCallbackManager< void(size_t)> state_callback_
Definition select.h:117
size_t size() const
Return the number of options in this select component.
Definition select.cpp:51
optional< std::string > at(size_t index) const
Return the (optional) option value at the provided index offset.
Definition select.cpp:73
bool has_option(const std::string &option) const
Return whether this select component contains the provided option.
Definition select.cpp:45
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:56
bool has_index(size_t index) const
Return whether this select component contains the provided index offset.
Definition select.cpp:49
SelectTraits traits
Definition select.h:31
StringRef current_option() const
Return the currently selected option, or empty StringRef if no state.
Definition select.cpp:41
void publish_state(const std::string &state)
Definition select.cpp:11
const FixedVector< const char * > & get_options() const
uint8_t options
bool state
Definition fan.h:2
std::string size_t len
Definition helpers.h:1045