ESPHome 2025.12.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 {
8namespace select {
9
10static const char *const TAG = "select";
11
12void Select::publish_state(const std::string &state) { this->publish_state(state.c_str()); }
13
14void Select::publish_state(const char *state) {
15 auto index = this->index_of(state);
16 if (index.has_value()) {
17 this->publish_state(index.value());
18 } else {
19 ESP_LOGE(TAG, "'%s': Invalid option %s", this->get_name().c_str(), state);
20 }
21}
22
23void Select::publish_state(size_t index) {
24 if (!this->has_index(index)) {
25 ESP_LOGE(TAG, "'%s': Invalid index %zu", this->get_name().c_str(), index);
26 return;
27 }
28 const char *option = this->option_at(index);
29 this->set_has_state(true);
30 this->active_index_ = index;
31#pragma GCC diagnostic push
32#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
33 this->state = option; // Update deprecated member for backward compatibility
34#pragma GCC diagnostic pop
35 ESP_LOGD(TAG, "'%s': Sending state %s (index %zu)", this->get_name().c_str(), option, index);
36 // Callback signature requires std::string, create temporary for compatibility
37 this->state_callback_.call(std::string(option), index);
38#if defined(USE_SELECT) && defined(USE_CONTROLLER_REGISTRY)
40#endif
41}
42
43const char *Select::current_option() const { return this->has_state() ? this->option_at(this->active_index_) : ""; }
44
45void Select::add_on_state_callback(std::function<void(std::string, size_t)> &&callback) {
46 this->state_callback_.add(std::move(callback));
47}
48
49bool Select::has_option(const std::string &option) const { return this->index_of(option.c_str()).has_value(); }
50
51bool Select::has_option(const char *option) const { return this->index_of(option).has_value(); }
52
53bool Select::has_index(size_t index) const { return index < this->size(); }
54
55size_t Select::size() const {
56 const auto &options = traits.get_options();
57 return options.size();
58}
59
60optional<size_t> Select::index_of(const std::string &option) const { return this->index_of(option.c_str()); }
61
62optional<size_t> Select::index_of(const char *option) const {
63 const auto &options = traits.get_options();
64 for (size_t i = 0; i < options.size(); i++) {
65 if (strcmp(options[i], option) == 0) {
66 return i;
67 }
68 }
69 return {};
70}
71
73 if (this->has_state()) {
74 return this->active_index_;
75 }
76 return {};
77}
78
79optional<std::string> Select::at(size_t index) const {
80 if (this->has_index(index)) {
81 const auto &options = traits.get_options();
82 return std::string(options.at(index));
83 }
84 return {};
85}
86
87const char *Select::option_at(size_t index) const { return traits.get_options().at(index); }
88
89} // namespace select
90} // namespace esphome
static void notify_select_update(select::Select *obj)
const StringRef & get_name() const
void set_has_state(bool state)
Definition entity_base.h:93
bool has_state() const
Definition entity_base.h:90
size_t size() const
Definition helpers.h:338
T & at(size_t i)
Access element with bounds checking (matches std::vector behavior) Note: No exception thrown on out o...
Definition helpers.h:348
bool has_value() const
Definition optional.h:92
void add_on_state_callback(std::function< void(std::string, size_t)> &&callback)
Definition select.cpp:45
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:87
optional< size_t > active_index() const
Return the (optional) index offset of the currently active option.
Definition select.cpp:72
size_t size() const
Return the number of options in this select component.
Definition select.cpp:55
optional< size_t > index_of(const std::string &option) const
Find the (optional) index offset of the provided option value.
Definition select.cpp:60
optional< std::string > at(size_t index) const
Return the (optional) option value at the provided index offset.
Definition select.cpp:79
bool has_option(const std::string &option) const
Return whether this select component contains the provided option.
Definition select.cpp:49
bool has_index(size_t index) const
Return whether this select component contains the provided index offset.
Definition select.cpp:53
const char * current_option() const
Return the currently selected option (as const char* from flash).
Definition select.cpp:43
SelectTraits traits
Definition select.h:33
CallbackManager< void(std::string, size_t)> state_callback_
Definition select.h:114
void publish_state(const std::string &state)
Definition select.cpp:12
const FixedVector< const char * > & get_options() const
uint8_t options
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7