ESPHome 2026.2.1
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
72
74 optional<std::string> at(size_t index) const;
75
77 const char *option_at(size_t index) const;
78
79 void add_on_state_callback(std::function<void(size_t)> &&callback);
80
81 protected:
82 friend class SelectCall;
83
84 size_t active_index_{0};
85
96 virtual void control(size_t index) { this->control(this->option_at(index)); }
97
108 virtual void control(const std::string &value) {
109 auto index = this->index_of(value);
110 if (index.has_value()) {
111 this->control(index.value());
112 }
113 }
114
116};
117
118} // namespace esphome::select
ESPDEPRECATED("Use make_entity_preference<T>() instead, or preferences won't be migrated. " "See https://github.com/esphome/backlog/issues/85. Will be removed in 2027.1.0.", "2026.7.0") uint32_t get_preference_hash()
Get a unique hash for storing preferences/settings for this entity.
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
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:85
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:96
optional< size_t > active_index() const
Return the (optional) index offset of the currently active option.
Definition select.cpp:70
LazyCallbackManager< void(size_t)> state_callback_
Definition select.h:115
size_t size() const
Return the number of options in this select component.
Definition select.cpp:55
friend class SelectCall
Definition select.h:82
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:77
bool has_option(const std::string &option) const
Return whether this select component contains the provided option.
Definition select.cpp:49
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:60
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:53
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:108
void add_on_state_callback(std::function< void(size_t)> &&callback)
Definition select.cpp:45
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:692