mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 01:07:45 +01:00
RestoringGlobalsComponent: Store value on shutdown (#3586)
This commit is contained in:
parent
34adbf0588
commit
5aaac06f5b
1 changed files with 8 additions and 4 deletions
|
@ -44,7 +44,14 @@ template<typename T> class RestoringGlobalsComponent : public Component {
|
|||
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
void loop() override {
|
||||
void loop() override { store_value_(); }
|
||||
|
||||
void on_shutdown() override { store_value_(); }
|
||||
|
||||
void set_name_hash(uint32_t name_hash) { this->name_hash_ = name_hash; }
|
||||
|
||||
protected:
|
||||
void store_value_() {
|
||||
int diff = memcmp(&this->value_, &this->prev_value_, sizeof(T));
|
||||
if (diff != 0) {
|
||||
this->rtc_.save(&this->value_);
|
||||
|
@ -52,9 +59,6 @@ template<typename T> class RestoringGlobalsComponent : public Component {
|
|||
}
|
||||
}
|
||||
|
||||
void set_name_hash(uint32_t name_hash) { this->name_hash_ = name_hash; }
|
||||
|
||||
protected:
|
||||
T value_{};
|
||||
T prev_value_{};
|
||||
uint32_t name_hash_{};
|
||||
|
|
Loading…
Reference in a new issue