total_daily_energy: allow to disable restore mode (#2795)

This commit is contained in:
Adrián Panella
2021-11-25 15:35:36 -06:00
committed by GitHub
parent 2347e043a9
commit e7827a6997
3 changed files with 11 additions and 6 deletions
@@ -7,14 +7,14 @@ namespace total_daily_energy {
static const char *const TAG = "total_daily_energy";
void TotalDailyEnergy::setup() {
this->pref_ = global_preferences->make_preference<float>(this->get_object_id_hash());
float initial_value = 0;
float recovered;
if (this->pref_.load(&recovered)) {
this->publish_state_and_save(recovered);
} else {
this->publish_state_and_save(0);
if (this->restore_) {
this->pref_ = global_preferences->make_preference<float>(this->get_object_id_hash());
this->pref_.load(&initial_value);
}
this->publish_state_and_save(initial_value);
this->last_update_ = millis();
this->last_save_ = this->last_update_;