ESPHome 2026.1.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::select {
10
11#define LOG_SELECT(prefix, type, obj) \
12 if ((obj) != nullptr) { \
13 ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
14 if (!(obj)->get_icon_ref().empty()) { \
15 ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon_ref().c_str()); \
16 } \
17 }
18
19#define SUB_SELECT(name) \
20 protected: \
21 select::Select *name##_select_{nullptr}; \
22\
23 public: \
24 void set_##name##_select(select::Select *select) { this->name##_select_ = select; }
25
30class Select : public EntityBase {
31 public:
33
34#pragma GCC diagnostic push
35#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
37 ESPDEPRECATED("Use current_option() instead of .state. Will be removed in 2026.5.0", "2025.11.0")
38 std::string state{};
39
40 Select() = default;
41 ~Select() = default;
42#pragma GCC diagnostic pop
43
44 void publish_state(const std::string &state);
45 void publish_state(const char *state);
46 void publish_state(size_t index);
47
49 const char *current_option() const;
50
52 SelectCall make_call() { return SelectCall(this); }
53
55 bool has_option(const std::string &option) const;
56 bool has_option(const char *option) const;
57
59 bool has_index(size_t index) const;
60
62 size_t size() const;
63
65 optional<size_t> index_of(const char *option, size_t len) const;
66 optional<size_t> index_of(const std::string &option) const { return this->index_of(option.data(), option.size()); }
67 optional<size_t> index_of(const char *option) const { return this->index_of(option, strlen(option)); }
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(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
115};
116
117} // namespace esphome::select
Base-class for all selects.
Definition select.h:30
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:83
SelectCall make_call()
Instantiate a SelectCall object to modify this select component's state.
Definition select.h:52
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:68
LazyCallbackManager< void(size_t)> state_callback_
Definition select.h:114
size_t size() const
Return the number of options in this select component.
Definition select.cpp:53
friend class SelectCall
Definition select.h:81
optional< size_t > index_of(const std::string &option) const
Definition select.h:66
optional< std::string > at(size_t index) const
Return the (optional) option value at the provided index offset.
Definition select.cpp:75
bool has_option(const std::string &option) const
Return whether this select component contains the provided option.
Definition select.cpp:47
optional< size_t > index_of(const char *option, size_t len) const
Find the (optional) index offset of the provided option value.
Definition select.cpp:58
bool has_index(size_t index) const
Return whether this select component contains the provided index offset.
Definition select.cpp:51
const char * current_option() const
Return the currently selected option (as const char* from flash).
Definition select.cpp:41
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
void add_on_state_callback(std::function< void(size_t)> &&callback)
Definition select.cpp:43
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:32
optional< size_t > index_of(const char *option) const
Definition select.h:67
void publish_state(const std::string &state)
Definition select.cpp:11
bool state
Definition fan.h:0
std::string size_t len
Definition helpers.h:533
struct ESPDEPRECATED("Use std::index_sequence instead. Removed in 2026.6.0", "2025.12.0") seq
Definition automation.h:24