ESPHome 2025.12.2
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::template_ {
5
6static const char *const TAG = "template.text";
7
9 if (this->f_.has_value())
10 return;
11 std::string value = this->initial_value_;
12 if (!this->pref_) {
13 ESP_LOGD(TAG, "State from initial: %s", value.c_str());
14 } else {
15 uint32_t key = this->get_preference_hash();
16 key += this->traits.get_min_length() << 2;
17 key += this->traits.get_max_length() << 4;
18 key += fnv1_hash(this->traits.get_pattern_c_str()) << 6;
19 this->pref_->setup(key, value);
20 }
21 if (!value.empty())
22 this->publish_state(value);
23}
24
26 if (!this->f_.has_value())
27 return;
28
29 auto val = this->f_();
30 if (val.has_value()) {
31 this->publish_state(*val);
32 }
33}
34
35void TemplateText::control(const std::string &value) {
36 this->set_trigger_->trigger(value);
37
38 if (this->optimistic_)
39 this->publish_state(value);
40
41 if (this->pref_) {
42 if (!this->pref_->save(value)) {
43 ESP_LOGW(TAG, "Text value too long to save");
44 }
45 }
46}
48 LOG_TEXT("", "Template Text Input", this);
49 ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
50 LOG_UPDATE_INTERVAL(this);
51}
52
53} // namespace esphome::template_
uint32_t get_preference_hash()
Get a unique hash for storing preferences/settings for this entity.
bool has_value() const
Check if a lambda is set.
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:204
TemplateLambda< std::string > f_
TemplateTextSaverBase * pref_
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:11
TextTraits traits
Definition text.h:27
const char * get_pattern_c_str() const
Definition text_traits.h:26
mopeka_std_values val[4]
const char *const TAG
Definition spi.cpp:8
uint32_t fnv1_hash(const char *str)
Calculate a FNV-1 hash of str.
Definition helpers.cpp:146