ESPHome 2026.11.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 (log_entity_header(TAG, prefix, LOG_STR_LITERAL(type), (obj))) { \
14 LOG_ENTITY_ICON(TAG, prefix, *(obj)); \
15 }
16
17#define SUB_SELECT(name) \
18 protected: \
19 select::Select *name##_select_{nullptr}; \
20\
21 public: \
22 void set_##name##_select(select::Select *select) { this->name##_select_ = select; }
23
28class Select : public EntityBase {
29 public:
31
32 Select() = default;
33 ~Select() = default;
34
35 void publish_state(const std::string &state) { this->publish_state(state.c_str()); }
36 void publish_state(const char *state);
37 void publish_state(size_t index);
38
43 return this->has_state() ? StringRef(this->option_at(this->active_index_)) : StringRef();
44 }
45
47 SelectCall make_call() { return SelectCall(this); }
48
50 bool has_option(const std::string &option) const { return this->index_of(option).has_value(); }
51 bool has_option(const char *option) const { return this->index_of(option).has_value(); }
52
54 bool has_index(size_t index) const { return index < this->size(); }
55
57 size_t size() const { return this->traits.get_options().size(); }
58
60 optional<size_t> index_of(const char *option, size_t len) const;
61 optional<size_t> index_of(const std::string &option) const { return this->index_of(option.data(), option.size()); }
62 optional<size_t> index_of(const char *option) const { return this->index_of(option, strlen(option)); }
63
65 optional<size_t> active_index() const;
66
68 optional<std::string> at(size_t index) const;
69
71 const char *option_at(size_t index) const;
72
73 template<typename F> void add_on_state_callback(F &&callback) {
74 this->state_callback_.add(std::forward<F>(callback));
75 }
76
77 protected:
78 friend class SelectCall;
79
80 size_t active_index_{0};
81
92 virtual void control(size_t index) { this->control(this->option_at(index)); }
93
104 virtual void control(const std::string &value) {
105 auto index = this->index_of(value);
106 if (index.has_value()) {
107 this->control(index.value());
108 }
109 }
110
112};
113
115template<auto Fn> class ApplySelect final : public Select {
116 public:
117 // Not "= default": placement new would zero-fill .bss that is already zero.
119
120 protected:
121 void control(size_t index) override { Fn(this, index); }
122};
123
124} // namespace esphome::select
constexpr size_t size() const
Definition helpers.h:140
bool has_state() const
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
Calls the codegen-generated Fn(this, index) on a new index; the hub and any publish are baked into Fn...
Definition select.h:115
void control(size_t index) override
Definition select.h:121
Base-class for all selects.
Definition select.h:28
void add_on_state_callback(F &&callback)
Definition select.h:73
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:60
SelectCall make_call()
Instantiate a SelectCall object to modify this select component's state.
Definition select.h:47
virtual void control(size_t index)
Set the value of the select by index, this is an optional virtual method.
Definition select.h:92
optional< size_t > active_index() const
Return the (optional) index offset of the currently active option.
Definition select.cpp:45
LazyCallbackManager< void(size_t)> state_callback_
Definition select.h:111
size_t size() const
Return the number of options in this select component.
Definition select.h:57
bool has_option(const char *option) const
Definition select.h:51
friend class SelectCall
Definition select.h:78
optional< size_t > index_of(const std::string &option) const
Definition select.h:61
optional< std::string > at(size_t index) const
Return the (optional) option value at the provided index offset.
Definition select.cpp:52
bool has_option(const std::string &option) const
Return whether this select component contains the provided option.
Definition select.h:50
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:35
bool has_index(size_t index) const
Return whether this select component contains the provided index offset.
Definition select.h:54
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:104
SelectTraits traits
Definition select.h:30
optional< size_t > index_of(const char *option) const
Definition select.h:62
StringRef current_option() const
Return the currently selected option, or empty StringRef if no state.
Definition select.h:42
void publish_state(const std::string &state)
Definition select.h:35
const SelectOptions & get_options() const
bool state
Definition fan.h:2
const void size_t len
Definition hal.h:68