From 5aaac06f5b0f49027406901df966bdc7a4434c94 Mon Sep 17 00:00:00 2001 From: RoboMagus <68224306+RoboMagus@users.noreply.github.com> Date: Tue, 21 Jun 2022 01:27:53 +0200 Subject: [PATCH] RestoringGlobalsComponent: Store value on shutdown (#3586) --- esphome/components/globals/globals_component.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/esphome/components/globals/globals_component.h b/esphome/components/globals/globals_component.h index 3286e43575..101adeb311 100644 --- a/esphome/components/globals/globals_component.h +++ b/esphome/components/globals/globals_component.h @@ -44,7 +44,14 @@ template 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 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_{};