ESPHome 2026.8.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 Select() = default;
34 ~Select() = default;
35
36 void publish_state(const std::string &state);
37 void publish_state(const char *state);
38 void publish_state(size_t index);
39
44
46 SelectCall make_call() { return SelectCall(this); }
47
49 bool has_option(const std::string &option) const;
50 bool has_option(const char *option) const;
51
53 bool has_index(size_t index) const;
54
56 size_t size() const;
57
59 optional<size_t> index_of(const char *option, size_t len) const;
60 optional<size_t> index_of(const std::string &option) const { return this->index_of(option.data(), option.size()); }
61 optional<size_t> index_of(const char *option) const { return this->index_of(option, strlen(option)); }
62
64 optional<size_t> active_index() const;
65
67 optional<std::string> at(size_t index) const;
68
70 const char *option_at(size_t index) const;
71
72 template<typename F> void add_on_state_callback(F &&callback) {
73 this->state_callback_.add(std::forward<F>(callback));
74 }
75
76 protected:
77 friend class SelectCall;
78
79 size_t active_index_{0};
80
91 virtual void control(size_t index) { this->control(this->option_at(index)); }
92
103 virtual void control(const std::string &value) {
104 auto index = this->index_of(value);
105 if (index.has_value()) {
106 this->control(index.value());
107 }
108 }
109
111};
112
113} // namespace esphome::select
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:72
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:77
SelectCall make_call()
Instantiate a SelectCall object to modify this select component's state.
Definition select.h:46
virtual void control(size_t index)
Set the value of the select by index, this is an optional virtual method.
Definition select.h:91
optional< size_t > active_index() const
Return the (optional) index offset of the currently active option.
Definition select.cpp:62
LazyCallbackManager< void(size_t)> state_callback_
Definition select.h:110
size_t size() const
Return the number of options in this select component.
Definition select.cpp:47
friend class SelectCall
Definition select.h:77
optional< size_t > index_of(const std::string &option) const
Definition select.h:60
optional< std::string > at(size_t index) const
Return the (optional) option value at the provided index offset.
Definition select.cpp:69
bool has_option(const std::string &option) const
Return whether this select component contains the provided option.
Definition select.cpp:41
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:52
bool has_index(size_t index) const
Return whether this select component contains the provided index offset.
Definition select.cpp:45
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:103
SelectTraits traits
Definition select.h:31
optional< size_t > index_of(const char *option) const
Definition select.h:61
StringRef current_option() const
Return the currently selected option, or empty StringRef if no state.
Definition select.cpp:37
void publish_state(const std::string &state)
Definition select.cpp:11
bool state
Definition fan.h:2
const void size_t len
Definition hal.h:64