ESPHome 2025.5.0
Loading...
Searching...
No Matches
select.cpp
Go to the documentation of this file.
1#include "select.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace select {
6
7static const char *const TAG = "select";
8
9void Select::publish_state(const std::string &state) {
10 auto index = this->index_of(state);
11 const auto *name = this->get_name().c_str();
12 if (index.has_value()) {
13 this->has_state_ = true;
14 this->state = state;
15 ESP_LOGD(TAG, "'%s': Sending state %s (index %zu)", name, state.c_str(), index.value());
16 this->state_callback_.call(state, index.value());
17 } else {
18 ESP_LOGE(TAG, "'%s': invalid state for publish_state(): %s", name, state.c_str());
19 }
20}
21
22void Select::add_on_state_callback(std::function<void(std::string, size_t)> &&callback) {
23 this->state_callback_.add(std::move(callback));
24}
25
26bool Select::has_option(const std::string &option) const { return this->index_of(option).has_value(); }
27
28bool Select::has_index(size_t index) const { return index < this->size(); }
29
30size_t Select::size() const {
31 auto options = traits.get_options();
32 return options.size();
33}
34
35optional<size_t> Select::index_of(const std::string &option) const {
36 auto options = traits.get_options();
37 auto it = std::find(options.begin(), options.end(), option);
38 if (it == options.end()) {
39 return {};
40 }
41 return std::distance(options.begin(), it);
42}
43
45 if (this->has_state()) {
46 return this->index_of(this->state);
47 } else {
48 return {};
49 }
50}
51
52optional<std::string> Select::at(size_t index) const {
53 if (this->has_index(index)) {
54 auto options = traits.get_options();
55 return options.at(index);
56 } else {
57 return {};
58 }
59}
60
61} // namespace select
62} // namespace esphome
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:68
bool has_value() const
Definition optional.h:87
void add_on_state_callback(std::function< void(std::string, size_t)> &&callback)
Definition select.cpp:22
optional< size_t > active_index() const
Return the (optional) index offset of the currently active option.
Definition select.cpp:44
size_t size() const
Return the number of options in this select component.
Definition select.cpp:30
optional< size_t > index_of(const std::string &option) const
Find the (optional) index offset of the provided option value.
Definition select.cpp:35
optional< std::string > at(size_t index) const
Return the (optional) option value at the provided index offset.
Definition select.cpp:52
bool has_option(const std::string &option) const
Return whether this select component contains the provided option.
Definition select.cpp:26
std::string state
Definition select.h:33
bool has_index(size_t index) const
Return whether this select component contains the provided index offset.
Definition select.cpp:28
SelectTraits traits
Definition select.h:34
bool has_state() const
Return whether this select component has gotten a full state yet.
Definition select.h:39
CallbackManager< void(std::string, size_t)> state_callback_
Definition select.h:75
void publish_state(const std::string &state)
Definition select.cpp:9
std::vector< std::string > 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