ESPHome 2026.3.3
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 if (value == this->prev_) {
28 return true; // No change, nothing to save
29 }
30 // If string is bigger than the allocation, do not save it.
31 // We don't need to waste ram setting prev_value either.
32 int size = value.size();
33 if (size > SZ) {
34 return false;
35 }
36 // Make it into a length prefixed thing
37 unsigned char temp[SZ + 1];
38 memcpy(temp + 1, value.c_str(), size);
39 // SZ should be pre checked at the schema level, it can't go past the char range.
40 temp[0] = ((unsigned char) size);
41 this->pref_.save(&temp);
42 this->prev_.assign(value);
43 return true;
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 size_t len = static_cast<uint8_t>(temp[0]);
56 if (len > SZ) {
57 len = SZ;
58 }
59 value.assign(temp + 1, len);
60 }
61
62 this->prev_.assign(value);
63 }
64};
65
66class TemplateText final : public text::Text, public PollingComponent {
67 public:
68 template<typename F> void set_template(F &&f) { this->f_.set(std::forward<F>(f)); }
69
70 void setup() override;
71 void update() override;
72 void dump_config() override;
73 float get_setup_priority() const override { return setup_priority::HARDWARE; }
74
76 void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
77 void set_initial_value(const char *initial_value) { this->initial_value_ = initial_value; }
79 void set_initial_value(const std::string &initial_value) = delete;
80 void set_value_saver(TemplateTextSaverBase *restore_value_saver) { this->pref_ = restore_value_saver; }
81
82 protected:
83 void control(const std::string &value) override;
84 bool optimistic_ = false;
85 const char *initial_value_{nullptr};
88
90};
91
92} // 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:395
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:538
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:21
uint16_t id
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:29
std::string size_t len
Definition helpers.h:892
size_t size
Definition helpers.h:929
ESPPreferences * global_preferences
static void uint32_t