ESPHome 2026.10.0
Loading...
Searching...
No Matches
template_select.h
Go to the documentation of this file.
1#pragma once
2
9
10namespace esphome::template_ {
11
12// Distinct per member: two empty members of the same type cannot share an address
13template<int N> struct Empty {};
15
16void dump_config_helper(BaseTemplateSelect *sel_comp, bool optimistic, bool has_lambda, size_t initial_option_index,
17 bool restore_value);
18void setup_initial(BaseTemplateSelect *sel_comp, size_t initial_index);
19void setup_with_restore(BaseTemplateSelect *sel_comp, ESPPreferenceObject &pref, size_t initial_index);
20void update_lambda(BaseTemplateSelect *sel_comp, const optional<std::string> &val);
21
23
24template<bool HAS_LAMBDA, bool OPTIMISTIC, bool RESTORE_VALUE, size_t INITIAL_OPTION_INDEX>
26 public:
27 // User provided, not "= default": `new(p) TemplateSelect()` would zero-fill .bss that is already zero.
29 template<typename F> void set_lambda(F &&f) {
30 if constexpr (HAS_LAMBDA) {
31 this->f_.set(std::forward<F>(f));
32 }
33 }
34
35 void setup() override {
36 if constexpr (!HAS_LAMBDA) {
37 if constexpr (RESTORE_VALUE) {
38 this->pref_ = this->template make_entity_preference<size_t>();
39 setup_with_restore(this, this->pref_, INITIAL_OPTION_INDEX);
40 } else {
41 setup_initial(this, INITIAL_OPTION_INDEX);
42 }
43 }
44 }
45
46 void update() override {
47 if constexpr (HAS_LAMBDA) {
48 update_lambda(this, this->f_());
49 }
50 }
51 void dump_config() override {
52 dump_config_helper(this, OPTIMISTIC, HAS_LAMBDA, INITIAL_OPTION_INDEX, RESTORE_VALUE);
53 };
54 float get_setup_priority() const override { return setup_priority::HARDWARE; }
55
56 protected:
57 void control(size_t index) override {
58 if constexpr (OPTIMISTIC)
59 this->publish_state(index);
60 if constexpr (RESTORE_VALUE)
61 this->pref_.save(&index);
62 }
63 [[no_unique_address]] std::conditional_t<HAS_LAMBDA, TemplateLambda<std::string>, Empty<0>> f_{};
64 [[no_unique_address]] std::conditional_t<RESTORE_VALUE, ESPPreferenceObject, Empty<1>> pref_{};
65};
66
68template<bool HAS_LAMBDA, bool OPTIMISTIC, bool RESTORE_VALUE, size_t INITIAL_OPTION_INDEX>
70 : public TemplateSelect<HAS_LAMBDA, OPTIMISTIC, RESTORE_VALUE, INITIAL_OPTION_INDEX> {
71 public:
73
74 protected:
80};
81
82} // namespace esphome::template_
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
This class simplifies creating components that periodically check a state.
Definition component.h:510
StringRef is a reference to a string owned by something else.
Definition string_ref.h:30
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:528
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:60
void publish_state(const std::string &state)
Definition select.h:36
Base template select class - used when no set_action is configured.
std::conditional_t< RESTORE_VALUE, ESPPreferenceObject, Empty< 1 > > pref_
void control(size_t index) override
float get_setup_priority() const override
std::conditional_t< HAS_LAMBDA, TemplateLambda< std::string >, Empty< 0 > > f_
Template select with set_action trigger - only instantiated when set_action is configured.
mopeka_std_values val[3]
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:43
void update_lambda(BaseTemplateSelect *sel_comp, const optional< std::string > &val)
void dump_config_helper(BaseTemplateSelect *sel_comp, bool optimistic, bool has_lambda, const size_t initial_option_index, bool restore_value)
void setup_initial(BaseTemplateSelect *sel_comp, size_t initial_index)
void setup_with_restore(BaseTemplateSelect *sel_comp, ESPPreferenceObject &pref, size_t initial_index)