ESPHome 2026.2.3
Loading...
Searching...
No Matches
template_select.cpp
Go to the documentation of this file.
1#include "template_select.h"
2#include "esphome/core/log.h"
3
4namespace esphome::template_ {
5
6static const char *const TAG = "template.select";
7
8void dump_config_helper(BaseTemplateSelect *sel_comp, bool optimistic, bool has_lambda,
9 const size_t initial_option_index, bool restore_value) {
10 LOG_SELECT("", "Template Select", sel_comp);
11 if (has_lambda) {
12 LOG_UPDATE_INTERVAL(sel_comp);
13 } else {
14 ESP_LOGCONFIG(TAG,
15 " Optimistic: %s\n"
16 " Initial Option: %s\n"
17 " Restore Value: %s",
18 YESNO(optimistic), sel_comp->option_at(initial_option_index), YESNO(restore_value));
19 }
20}
21
22void setup_initial(BaseTemplateSelect *sel_comp, size_t initial_index) {
23 ESP_LOGD(TAG, "State from initial: %s", sel_comp->option_at(initial_index));
24 sel_comp->publish_state(initial_index);
25}
26
27void setup_with_restore(BaseTemplateSelect *sel_comp, ESPPreferenceObject &pref, size_t initial_index) {
28 size_t index = initial_index;
29 if (pref.load(&index) && sel_comp->has_index(index)) {
30 ESP_LOGD(TAG, "State from restore: %s", sel_comp->option_at(index));
31 } else {
32 index = initial_index;
33 ESP_LOGD(TAG, "State from initial (no valid stored index): %s", sel_comp->option_at(initial_index));
34 }
35 sel_comp->publish_state(index);
36}
37
39 if (val.has_value()) {
40 if (!sel_comp->has_option(*val)) {
41 ESP_LOGE(TAG, "Lambda returned an invalid option: %s", (*val).c_str());
42 return;
43 }
44 sel_comp->publish_state(*val);
45 }
46}
47
48} // namespace esphome::template_
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
bool has_option(const std::string &option) const
Return whether this select component contains the provided option.
Definition select.cpp:49
bool has_index(size_t index) const
Return whether this select component contains the provided index offset.
Definition select.cpp:53
void publish_state(const std::string &state)
Definition select.cpp:11
mopeka_std_values val[4]
const char *const TAG
Definition spi.cpp:7
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)