ESPHome  2024.10.2
select.cpp
Go to the documentation of this file.
1 #include "select.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace select {
6 
7 static const char *const TAG = "select";
8 
9 void 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 
22 void Select::add_on_state_callback(std::function<void(std::string, size_t)> &&callback) {
23  this->state_callback_.add(std::move(callback));
24 }
25 
26 bool Select::has_option(const std::string &option) const { return this->index_of(option).has_value(); }
27 
28 bool Select::has_index(size_t index) const { return index < this->size(); }
29 
30 size_t Select::size() const {
31  auto options = traits.get_options();
32  return options.size();
33 }
34 
35 optional<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 
52 optional<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 char * name
Definition: stm32flash.h:78
bool has_option(const std::string &option) const
Return whether this select component contains the provided option.
Definition: select.cpp:26
optional< std::string > at(size_t index) const
Return the (optional) option value at the provided index offset.
Definition: select.cpp:52
CallbackManager< void(std::string, size_t)> state_callback_
Definition: select.h:75
SelectTraits traits
Definition: select.h:34
bool has_value() const
Definition: optional.h:87
optional< size_t > active_index() const
Return the (optional) index offset of the currently active option.
Definition: select.cpp:44
std::vector< std::string > get_options() const
size_t size() const
Return the number of options in this select component.
Definition: select.cpp:30
uint8_t options
constexpr const char * c_str() const
Definition: string_ref.h:68
std::string state
Definition: select.h:33
void publish_state(const std::string &state)
Definition: select.cpp:9
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool has_state() const
Return whether this select component has gotten a full state yet.
Definition: select.h:39
bool has_index(size_t index) const
Return whether this select component contains the provided index offset.
Definition: select.cpp:28
optional< size_t > index_of(const std::string &option) const
Find the (optional) index offset of the provided option value.
Definition: select.cpp:35
void add_on_state_callback(std::function< void(std::string, size_t)> &&callback)
Definition: select.cpp:22
const StringRef & get_name() const
Definition: entity_base.cpp:10
bool state
Definition: fan.h:34