ESPHome 2026.2.1
Loading...
Searching...
No Matches
template_text.h
Go to the documentation of this file.
1#pragma once
2
8
9namespace esphome::template_ {
10
11// We keep this separate so we don't have to template and duplicate
12// the text input for each different size flash allocation.
14 public:
15 virtual bool save(const std::string &value) { return true; }
16
17 virtual void setup(uint32_t id, std::string &value) {}
18
19 protected:
21 std::string prev_;
22};
23
24template<uint8_t SZ> class TextSaver : public TemplateTextSaverBase {
25 public:
26 bool save(const std::string &value) override {
27 int diff = value.compare(this->prev_);
28 if (diff != 0) {
29 // If string is bigger than the allocation, do not save it.
30 // We don't need to waste ram setting prev_value either.
31 int size = value.size();
32 if (size <= SZ) {
33 // Make it into a length prefixed thing
34 unsigned char temp[SZ + 1];
35 memcpy(temp + 1, value.c_str(), size);
36 // SZ should be pre checked at the schema level, it can't go past the char range.
37 temp[0] = ((unsigned char) size);
38 this->pref_.save(&temp);
39 this->prev_.assign(value);
40 return true;
41 }
42 }
43 return false;
44 }
45
46 // Make the preference object. Fill the provided location with the saved data
47 // If it is available, else leave it alone
48 void setup(uint32_t id, std::string &value) override {
49 this->pref_ = global_preferences->make_preference<uint8_t[SZ + 1]>(id);
50
51 char temp[SZ + 1];
52 bool hasdata = this->pref_.load(&temp);
53
54 if (hasdata) {
55 value.assign(temp + 1, (size_t) temp[0]);
56 }
57
58 this->prev_.assign(value);
59 }
60};
61
62class TemplateText final : public text::Text, public PollingComponent {
63 public:
64 template<typename F> void set_template(F &&f) { this->f_.set(std::forward<F>(f)); }
65
66 void setup() override;
67 void update() override;
68 void dump_config() override;
69 float get_setup_priority() const override { return setup_priority::HARDWARE; }
70
72 void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
73 void set_initial_value(const char *initial_value) { this->initial_value_ = initial_value; }
75 void set_initial_value(const std::string &initial_value) = delete;
76 void set_value_saver(TemplateTextSaverBase *restore_value_saver) { this->pref_ = restore_value_saver; }
77
78 protected:
79 void control(const std::string &value) override;
80 bool optimistic_ = false;
81 const char *initial_value_{nullptr};
84
86};
87
88} // 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
bool save(const T *src)
Definition preferences.h:21
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
This class simplifies creating components that periodically check a state.
Definition component.h:512
Lightweight wrapper for template platform lambdas (stateless function pointers only).
void set(optional< T >(*f)(Args...))
Set the lambda function pointer.
void set_optimistic(bool optimistic)
Trigger< std::string > set_trigger_
TemplateLambda< std::string > f_
void set_value_saver(TemplateTextSaverBase *restore_value_saver)
void set_initial_value(const std::string &initial_value)=delete
Prevent accidental use of std::string which would dangle.
TemplateTextSaverBase * pref_
void set_initial_value(const char *initial_value)
void control(const std::string &value) override
float get_setup_priority() const override
Trigger< std::string > * get_set_trigger()
virtual bool save(const std::string &value)
virtual void setup(uint32_t id, std::string &value)
void setup(uint32_t id, std::string &value) override
bool save(const std::string &value) override
Base-class for all text inputs.
Definition text.h:22
uint16_t id
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:83
size_t size
Definition helpers.h:729
ESPPreferences * global_preferences