ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
select.h
Go to the documentation of this file.
1#pragma once
2
6#include "select_call.h"
7#include "select_traits.h"
8
9namespace esphome {
10namespace 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_ref().empty()) { \
16 ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon_ref().c_str()); \
17 } \
18 }
19
20#define SUB_SELECT(name) \
21 protected: \
22 select::Select *name##_select_{nullptr}; \
23\
24 public: \
25 void set_##name##_select(select::Select *select) { this->name##_select_ = select; }
26
31class Select : public EntityBase {
32 public:
34
35#pragma GCC diagnostic push
36#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
38 ESPDEPRECATED("Use current_option() instead of .state. Will be removed in 2026.5.0", "2025.11.0")
39 std::string state{};
40
41 Select() = default;
42 ~Select() = default;
43#pragma GCC diagnostic pop
44
45 void publish_state(const std::string &state);
46 void publish_state(const char *state);
47 void publish_state(size_t index);
48
50 const char *current_option() const;
51
53 SelectCall make_call() { return SelectCall(this); }
54
56 bool has_option(const std::string &option) const;
57 bool has_option(const char *option) const;
58
60 bool has_index(size_t index) const;
61
63 size_t size() const;
64
66 optional<size_t> index_of(const std::string &option) const;
67 optional<size_t> index_of(const char *option) const;
68
71
73 optional<std::string> at(size_t index) const;
74
76 const char *option_at(size_t index) const;
77
78 void add_on_state_callback(std::function<void(std::string, size_t)> &&callback);
79
80 protected:
81 friend class SelectCall;
82
83 size_t active_index_{0};
84
95 virtual void control(size_t index) { this->control(this->option_at(index)); }
96
107 virtual void control(const std::string &value) {
108 auto index = this->index_of(value);
109 if (index.has_value()) {
110 this->control(index.value());
111 }
112 }
113
114 CallbackManager<void(std::string, size_t)> state_callback_;
115};
116
117} // namespace select
118} // namespace esphome
Base-class for all selects.
Definition select.h:31
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
SelectCall make_call()
Instantiate a SelectCall object to modify this select component's state.
Definition select.h:53
virtual void control(size_t index)
Set the value of the select by index, this is an optional virtual method.
Definition select.h:95
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
friend class SelectCall
Definition select.h:81
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
virtual void control(const std::string &value)
Set the value of the select, this is a virtual method that each select integration can implement.
Definition select.h:107
ESPDEPRECATED("Use current_option() instead of .state. Will be removed in 2026.5.0", "2025.11.0") std Select()=default
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
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7