ESPHome 2026.10.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) { this->publish_state(state.c_str()); }
37 void publish_state(const char *state);
38 void publish_state(size_t index);
39
44 return this->has_state() ? StringRef(this->option_at(this->active_index_)) : StringRef();
45 }
46
48 SelectCall make_call() { return SelectCall(this); }
49
51 bool has_option(const std::string &option) const { return this->index_of(option).has_value(); }
52 bool has_option(const char *option) const { return this->index_of(option).has_value(); }
53
55 bool has_index(size_t index) const { return index < this->size(); }
56
58 size_t size() const { return this->traits.get_options().size(); }
59
61 optional<size_t> index_of(const char *option, size_t len) const;
62 optional<size_t> index_of(const std::string &option) const { return this->index_of(option.data(), option.size()); }
63 optional<size_t> index_of(const char *option) const { return this->index_of(option, strlen(option)); }
64
66 optional<size_t> active_index() const;
67
69 optional<std::string> at(size_t index) const;
70
72 const char *option_at(size_t index) const;
73
74 template<typename F> void add_on_state_callback(F &&callback) {
75 this->state_callback_.add(std::forward<F>(callback));
76 }
77
78 protected:
79 friend class SelectCall;
80
81 size_t active_index_{0};
82
93 virtual void control(size_t index) { this->control(this->option_at(index)); }
94
105 virtual void control(const std::string &value) {
106 auto index = this->index_of(value);
107 if (index.has_value()) {
108 this->control(index.value());
109 }
110 }
111
113};
114
115} // namespace esphome::select
bool has_state() const
size_t size() const
Definition helpers.h:711
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:74
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:48
virtual void control(size_t index)
Set the value of the select by index, this is an optional virtual method.
Definition select.h:93
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:112
size_t size() const
Return the number of options in this select component.
Definition select.h:58
bool has_option(const char *option) const
Definition select.h:52
friend class SelectCall
Definition select.h:79
optional< size_t > index_of(const std::string &option) const
Definition select.h:62
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:51
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:55
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:105
SelectTraits traits
Definition select.h:31
optional< size_t > index_of(const char *option) const
Definition select.h:63
StringRef current_option() const
Return the currently selected option, or empty StringRef if no state.
Definition select.h:43
void publish_state(const std::string &state)
Definition select.h:36
const FixedVector< const char * > & get_options() const
bool state
Definition fan.h:2
const void size_t len
Definition hal.h:64