ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
template_text.cpp
Go to the documentation of this file.
1#include "template_text.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace template_ {
6
7static const char *const TAG = "template.text";
8
10 if (!(this->f_ == nullptr)) {
11 if (this->f_.has_value())
12 return;
13 }
14 std::string value = this->initial_value_;
15 if (!this->pref_) {
16 ESP_LOGD(TAG, "State from initial: %s", value.c_str());
17 } else {
18 uint32_t key = this->get_object_id_hash();
19 key += this->traits.get_min_length() << 2;
20 key += this->traits.get_max_length() << 4;
21 key += fnv1_hash(this->traits.get_pattern()) << 6;
22 this->pref_->setup(key, value);
23 }
24 if (!value.empty())
25 this->publish_state(value);
26}
27
29 if (this->f_ == nullptr)
30 return;
31
32 if (!this->f_.has_value())
33 return;
34
35 auto val = (*this->f_)();
36 if (!val.has_value())
37 return;
38
39 this->publish_state(*val);
40}
41
42void TemplateText::control(const std::string &value) {
43 this->set_trigger_->trigger(value);
44
45 if (this->optimistic_)
46 this->publish_state(value);
47
48 if (this->pref_) {
49 if (!this->pref_->save(value)) {
50 ESP_LOGW(TAG, "Text value too long to save");
51 }
52 }
53}
55 LOG_TEXT("", "Template Text Input", this);
56 ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
57 LOG_UPDATE_INTERVAL(this);
58}
59
60} // namespace template_
61} // namespace esphome
uint32_t get_object_id_hash()
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition automation.h:145
TemplateTextSaverBase * pref_
optional< std::function< optional< std::string >()> > f_
Trigger< std::string > * set_trigger_
void control(const std::string &value) override
virtual bool save(const std::string &value)
virtual void setup(uint32_t id, std::string &value)
void publish_state(const std::string &state)
Definition text.cpp:9
TextTraits traits
Definition text.h:27
std::string get_pattern() const
Definition text_traits.h:26
mopeka_std_values val[4]
const char *const TAG
Definition spi.cpp:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
Definition helpers.cpp:134