ESPHome  2024.4.0
template_select.cpp
Go to the documentation of this file.
1 #include "template_select.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace template_ {
6 
7 static const char *const TAG = "template.select";
8 
10  if (this->f_.has_value())
11  return;
12 
13  std::string value;
14  ESP_LOGD(TAG, "Setting up Template Select");
15  if (!this->restore_value_) {
16  value = this->initial_option_;
17  ESP_LOGD(TAG, "State from initial: %s", value.c_str());
18  } else {
19  size_t index;
21  if (!this->pref_.load(&index)) {
22  value = this->initial_option_;
23  ESP_LOGD(TAG, "State from initial (could not load stored index): %s", value.c_str());
24  } else if (!this->has_index(index)) {
25  value = this->initial_option_;
26  ESP_LOGD(TAG, "State from initial (restored index %d out of bounds): %s", index, value.c_str());
27  } else {
28  value = this->at(index).value();
29  ESP_LOGD(TAG, "State from restore: %s", value.c_str());
30  }
31  }
32 
33  this->publish_state(value);
34 }
35 
37  if (!this->f_.has_value())
38  return;
39 
40  auto val = (*this->f_)();
41  if (!val.has_value())
42  return;
43 
44  if (!this->has_option(*val)) {
45  ESP_LOGE(TAG, "Lambda returned an invalid option: %s", (*val).c_str());
46  return;
47  }
48 
49  this->publish_state(*val);
50 }
51 
52 void TemplateSelect::control(const std::string &value) {
53  this->set_trigger_->trigger(value);
54 
55  if (this->optimistic_)
56  this->publish_state(value);
57 
58  if (this->restore_value_) {
59  auto index = this->index_of(value);
60  this->pref_.save(&index.value());
61  }
62 }
63 
65  LOG_SELECT("", "Template Select", this);
66  LOG_UPDATE_INTERVAL(this);
67  if (this->f_.has_value())
68  return;
69  ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
70  ESP_LOGCONFIG(TAG, " Initial Option: %s", this->initial_option_.c_str());
71  ESP_LOGCONFIG(TAG, " Restore Value: %s", YESNO(this->restore_value_));
72 }
73 
74 } // namespace template_
75 } // namespace esphome
value_type const & value() const
Definition: optional.h:89
bool has_option(const std::string &option) const
Return whether this select component contains the provided option.
Definition: select.cpp:26
optional< std::string > at(size_t index) const
Return the (optional) option value at the provided index offset.
Definition: select.cpp:52
mopeka_std_values val[4]
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
bool save(const T *src)
Definition: preferences.h:21
const char *const TAG
Definition: spi.cpp:8
optional< std::function< optional< std::string >)> > f_
ESPPreferences * global_preferences
void control(const std::string &value) override
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
void publish_state(const std::string &state)
Definition: select.cpp:9
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
bool has_index(size_t index) const
Return whether this select component contains the provided index offset.
Definition: select.cpp:28
optional< size_t > index_of(const std::string &option) const
Find the (optional) index offset of the provided option value.
Definition: select.cpp:35
uint32_t get_object_id_hash()
Definition: entity_base.cpp:76
Trigger< std::string > * set_trigger_