ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
select.h
Go to the documentation of this file.
1#pragma once
2
7#include "select_call.h"
8#include "select_traits.h"
9
10namespace esphome::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 LOG_ENTITY_ICON(TAG, prefix, *(obj)); \
16 }
17
18#define SUB_SELECT(name) \
19 protected: \
20 select::Select *name##_select_{nullptr}; \
21\
22 public: \
23 void set_##name##_select(select::Select *select) { this->name##_select_ = select; }
24
29class Select : public EntityBase {
30 public:
32
33#pragma GCC diagnostic push
34#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
36 ESPDEPRECATED("Use current_option() instead of .state. Will be removed in 2026.7.0", "2026.1.0")
37 std::string state{};
38
39 Select() = default;
40 ~Select() = default;
41#pragma GCC diagnostic pop
42
43 void publish_state(const std::string &state);
44 void publish_state(const char *state);
45 void publish_state(size_t index);
46
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 char *option, size_t len) const;
67 optional<size_t> index_of(const std::string &option) const { return this->index_of(option.data(), option.size()); }
68 optional<size_t> index_of(const char *option) const { return this->index_of(option, strlen(option)); }
69
71 optional<size_t> active_index() const;
72
74 optional<std::string> at(size_t index) const;
75
77 const char *option_at(size_t index) const;
78
79 template<typename F> void add_on_state_callback(F &&callback) {
80 this->state_callback_.add(std::forward<F>(callback));
81 }
82
83 protected:
84 friend class SelectCall;
85
86 size_t active_index_{0};
87
98 virtual void control(size_t index) { this->control(this->option_at(index)); }
99
110 virtual void control(const std::string &value) {
111 auto index = this->index_of(value);
112 if (index.has_value()) {
113 this->control(index.value());
114 }
115 }
116
118};
119
120} // namespace esphome::select
ESPDEPRECATED("set_internal() is undefined behavior at runtime — components and Home Assistant are NOT " "notified. Use the 'internal:' YAML key instead. Will be removed in 2027.3.0.", "2026.3.0") void set_internal(bool internal)
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
Base-class for all selects.
Definition select.h:29
void add_on_state_callback(F &&callback)
Definition select.h:79
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:81
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:98
optional< size_t > active_index() const
Return the (optional) index offset of the currently active option.
Definition select.cpp:66
LazyCallbackManager< void(size_t)> state_callback_
Definition select.h:117
size_t size() const
Return the number of options in this select component.
Definition select.cpp:51
friend class SelectCall
Definition select.h:84
optional< size_t > index_of(const std::string &option) const
Definition select.h:67
optional< std::string > at(size_t index) const
Return the (optional) option value at the provided index offset.
Definition select.cpp:73
bool has_option(const std::string &option) const
Return whether this select component contains the provided option.
Definition select.cpp:45
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:56
ESPDEPRECATED("Use current_option() instead of .state. Will be removed in 2026.7.0", "2026.1.0") std Select()=default
bool has_index(size_t index) const
Return whether this select component contains the provided index offset.
Definition select.cpp:49
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:110
SelectTraits traits
Definition select.h:31
optional< size_t > index_of(const char *option) const
Definition select.h:68
StringRef current_option() const
Return the currently selected option, or empty StringRef if no state.
Definition select.cpp:41
void publish_state(const std::string &state)
Definition select.cpp:11
bool state
Definition fan.h:2
std::string size_t len
Definition helpers.h:1045