ESPHome 2025.5.2
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
template_text.h
Go to the documentation of this file.
1#pragma once
2
7
8namespace esphome {
9namespace 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
63 public:
64 void set_template(std::function<optional<std::string>()> &&f) { this->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 std::string &initial_value) { this->initial_value_ = initial_value; }
74 void set_value_saver(TemplateTextSaverBase *restore_value_saver) { this->pref_ = restore_value_saver; }
75
76 protected:
77 void control(const std::string &value) override;
78 bool optimistic_ = false;
79 std::string initial_value_;
82
84};
85
86} // namespace template_
87} // 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:301
void set_optimistic(bool optimistic)
void set_template(std::function< optional< 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_
optional< std::function< optional< std::string >()> > f_
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
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:18
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
ESPPreferences * global_preferences
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.
Definition helpers.h:799