ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
template_text.h
Go to the documentation of this file.
1#pragma once
2
8
9namespace esphome {
10namespace template_ {
11
12// We keep this separate so we don't have to template and duplicate
13// the text input for each different size flash allocation.
15 public:
16 virtual bool save(const std::string &value) { return true; }
17
18 virtual void setup(uint32_t id, std::string &value) {}
19
20 protected:
22 std::string prev_;
23};
24
25template<uint8_t SZ> class TextSaver : public TemplateTextSaverBase {
26 public:
27 bool save(const std::string &value) override {
28 int diff = value.compare(this->prev_);
29 if (diff != 0) {
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 // Make it into a length prefixed thing
35 unsigned char temp[SZ + 1];
36 memcpy(temp + 1, value.c_str(), size);
37 // SZ should be pre checked at the schema level, it can't go past the char range.
38 temp[0] = ((unsigned char) size);
39 this->pref_.save(&temp);
40 this->prev_.assign(value);
41 return true;
42 }
43 }
44 return false;
45 }
46
47 // Make the preference object. Fill the provided location with the saved data
48 // If it is available, else leave it alone
49 void setup(uint32_t id, std::string &value) override {
50 this->pref_ = global_preferences->make_preference<uint8_t[SZ + 1]>(id);
51
52 char temp[SZ + 1];
53 bool hasdata = this->pref_.load(&temp);
54
55 if (hasdata) {
56 value.assign(temp + 1, (size_t) temp[0]);
57 }
58
59 this->prev_.assign(value);
60 }
61};
62
63class TemplateText final : public text::Text, public PollingComponent {
64 public:
65 template<typename F> void set_template(F &&f) { this->f_.set(std::forward<F>(f)); }
66
67 void setup() override;
68 void update() override;
69 void dump_config() override;
70 float get_setup_priority() const override { return setup_priority::HARDWARE; }
71
73 void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
74 void set_initial_value(const std::string &initial_value) { this->initial_value_ = initial_value; }
75 void set_value_saver(TemplateTextSaverBase *restore_value_saver) { this->pref_ = restore_value_saver; }
76
77 protected:
78 void control(const std::string &value) override;
79 bool optimistic_ = false;
80 std::string initial_value_;
83
85};
86
87} // namespace template_
88} // namespace esphome
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:437
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)
TemplateLambda< std::string > f_
void set_value_saver(TemplateTextSaverBase *restore_value_saver)
Trigger< std::string > * get_set_trigger() const
void set_initial_value(const std::string &initial_value)
TemplateTextSaverBase * pref_
Trigger< std::string > * set_trigger_
void control(const std::string &value) override
float get_setup_priority() const override
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:24
uint16_t id
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:58
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
ESPPreferences * global_preferences