pulse_counter: save last_time_ as preference

This lets rates be calculated across deep sleeps when a time source is configured
This commit is contained in:
brisk 2024-04-28 21:38:22 +09:30
parent f2046573fb
commit 5efcbf8dfc
2 changed files with 10 additions and 3 deletions

View file

@ -252,7 +252,9 @@ void PulseCounterSensor::setup() {
return; return;
} }
#ifdef CONF_USE_TIME #ifdef CONF_USE_TIME
this->time_id_->add_on_time_sync_callback([this]() { this->time_is_synchronized = true; }); this->time_id_->add_on_time_sync_callback([this]() { this->time_is_synchronized_ = true; this->update(); });
this->pref_ = global_preferences->make_preference<timestamp_t>(this->get_object_id_hash());
this->pref_.load(&this->last_time_);
#endif #endif
} }
@ -274,7 +276,7 @@ void PulseCounterSensor::update() {
#ifdef CONF_USE_TIME #ifdef CONF_USE_TIME
// Can't clear the pulse count until we can report the rate, so there's // Can't clear the pulse count until we can report the rate, so there's
// nothing to do until the time is synchronized // nothing to do until the time is synchronized
if (!time_is_synchronized) { if (!time_is_synchronized_) {
return; return;
} }
#endif #endif
@ -302,6 +304,9 @@ void PulseCounterSensor::update() {
this->total_sensor_->publish_state(current_total_); this->total_sensor_->publish_state(current_total_);
} }
this->last_time_ = now; this->last_time_ = now;
#ifdef CONF_USE_TIME
this->pref_.save(&this->last_time_);
#endif
} }
} // namespace pulse_counter } // namespace pulse_counter

View file

@ -102,7 +102,9 @@ class PulseCounterSensor : public sensor::Sensor, public PollingComponent {
sensor::Sensor *total_sensor_{nullptr}; sensor::Sensor *total_sensor_{nullptr};
#ifdef USE_TIME #ifdef USE_TIME
time::RealTimeClock *time_id_{nullptr}; time::RealTimeClock *time_id_{nullptr};
bool time_is_synchronized{false}; bool time_is_synchronized_{false};
// Store last_time_ across deep sleep
ESPPreferenceObject pref_{};
#endif #endif
}; };