ESPHome  2024.5.0
template_text.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 namespace esphome {
9 namespace 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 
24 template<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 
62 class TemplateText : public text::Text, public PollingComponent {
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 
71  Trigger<std::string> *get_set_trigger() const { return this->set_trigger_; }
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
void setup(uint32_t id, std::string &value) override
Definition: template_text.h:48
Trigger< std::string > * get_set_trigger() const
Definition: template_text.h:71
virtual void setup(uint32_t id, std::string &value)
Definition: template_text.h:17
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.
Definition: helpers.h:699
This class simplifies creating components that periodically check a state.
Definition: component.h:283
Base-class for all text inputs.
Definition: text.h:24
void set_initial_value(const std::string &initial_value)
Definition: template_text.h:73
bool save(const T *src)
Definition: preferences.h:21
void set_value_saver(TemplateTextSaverBase *restore_value_saver)
Definition: template_text.h:74
ESPPreferences * global_preferences
float get_setup_priority() const override
Definition: template_text.h:69
virtual bool save(const std::string &value)
Definition: template_text.h:15
void set_optimistic(bool optimistic)
Definition: template_text.h:72
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
void set_template(std::function< optional< std::string >()> &&f)
Definition: template_text.h:64
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
bool save(const std::string &value) override
Definition: template_text.h:26