ESPHome 2026.2.4
Loading...
Searching...
No Matches
template_select.h
Go to the documentation of this file.
1#pragma once
2
9
10namespace esphome::template_ {
11
12struct Empty {};
14
15void dump_config_helper(BaseTemplateSelect *sel_comp, bool optimistic, bool has_lambda, size_t initial_option_index,
16 bool restore_value);
17void setup_initial(BaseTemplateSelect *sel_comp, size_t initial_index);
18void setup_with_restore(BaseTemplateSelect *sel_comp, ESPPreferenceObject &pref, size_t initial_index);
20
22
23template<bool HAS_LAMBDA, bool OPTIMISTIC, bool RESTORE_VALUE, size_t INITIAL_OPTION_INDEX>
25 public:
26 template<typename F> void set_lambda(F &&f) {
27 if constexpr (HAS_LAMBDA) {
28 this->f_.set(std::forward<F>(f));
29 }
30 }
31
32 void setup() override {
33 if constexpr (!HAS_LAMBDA) {
34 if constexpr (RESTORE_VALUE) {
35 this->pref_ = this->template make_entity_preference<size_t>();
36 setup_with_restore(this, this->pref_, INITIAL_OPTION_INDEX);
37 } else {
38 setup_initial(this, INITIAL_OPTION_INDEX);
39 }
40 }
41 }
42
43 void update() override {
44 if constexpr (HAS_LAMBDA) {
45 update_lambda(this, this->f_());
46 }
47 }
48 void dump_config() override {
49 dump_config_helper(this, OPTIMISTIC, HAS_LAMBDA, INITIAL_OPTION_INDEX, RESTORE_VALUE);
50 };
51 float get_setup_priority() const override { return setup_priority::HARDWARE; }
52
53 protected:
54 void control(size_t index) override {
55 if constexpr (OPTIMISTIC)
56 this->publish_state(index);
57 if constexpr (RESTORE_VALUE)
58 this->pref_.save(&index);
59 }
60 [[no_unique_address]] std::conditional_t<HAS_LAMBDA, TemplateLambda<std::string>, Empty> f_{};
61 [[no_unique_address]] std::conditional_t<RESTORE_VALUE, ESPPreferenceObject, Empty> pref_{};
62};
63
65template<bool HAS_LAMBDA, bool OPTIMISTIC, bool RESTORE_VALUE, size_t INITIAL_OPTION_INDEX>
67 : public TemplateSelect<HAS_LAMBDA, OPTIMISTIC, RESTORE_VALUE, INITIAL_OPTION_INDEX> {
68 public:
70
71 protected:
77};
78
79} // namespace esphome::template_
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t std::function< RetryResult(uint8_t)> && f
Definition component.h:373
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:512
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:279
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
void publish_state(const std::string &state)
Definition select.cpp:11
Base template select class - used when no set_action is configured.
void control(size_t index) override
float get_setup_priority() const override
std::conditional_t< RESTORE_VALUE, ESPPreferenceObject, Empty > pref_
std::conditional_t< HAS_LAMBDA, TemplateLambda< std::string >, Empty > f_
Template select with set_action trigger - only instantiated when set_action is configured.
mopeka_std_values val[4]
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:83
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)