ESPHome  2022.8.0
select.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
6 #include "select_call.h"
7 #include "select_traits.h"
8 
9 namespace esphome {
10 namespace select {
11 
12 #define LOG_SELECT(prefix, type, obj) \
13  if ((obj) != nullptr) { \
14  ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
15  if (!(obj)->get_icon().empty()) { \
16  ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
17  } \
18  }
19 
24 class Select : public EntityBase {
25  public:
26  std::string state;
28 
29  void publish_state(const std::string &state);
30 
32  bool has_state() const { return has_state_; }
33 
35  SelectCall make_call() { return SelectCall(this); }
36 
38  bool has_option(const std::string &option) const;
39 
41  bool has_index(size_t index) const;
42 
44  size_t size() const;
45 
47  optional<size_t> index_of(const std::string &option) const;
48 
51 
53  optional<std::string> at(size_t index) const;
54 
55  void add_on_state_callback(std::function<void(std::string, size_t)> &&callback);
56 
57  protected:
58  friend class SelectCall;
59 
66  virtual void control(const std::string &value) = 0;
67 
69  bool has_state_{false};
70 };
71 
72 } // namespace select
73 } // namespace esphome
virtual void control(const std::string &value)=0
Set the value of the select, this is a virtual method that each select integration must implement...
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:68
SelectTraits traits
Definition: select.h:27
optional< size_t > active_index() const
Return the (optional) index offset of the currently active option.
Definition: select.cpp:44
SelectCall make_call()
Instantiate a SelectCall object to modify this select component&#39;s state.
Definition: select.h:35
size_t size() const
Return the number of options in this select component.
Definition: select.cpp:30
std::string state
Definition: select.h:26
void publish_state(const std::string &state)
Definition: select.cpp:9
Base-class for all selects.
Definition: select.h:24
Definition: a4988.cpp:4
bool has_state() const
Return whether this select component has gotten a full state yet.
Definition: select.h:32
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
friend class SelectCall
Definition: select.h:58