mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 06:33:51 +00:00 
			
		
		
		
	total_daily_energy: allow to disable restore mode (#2795)
This commit is contained in:
		| @@ -4,6 +4,7 @@ from esphome.components import sensor, time | |||||||
| from esphome.const import ( | from esphome.const import ( | ||||||
|     CONF_ICON, |     CONF_ICON, | ||||||
|     CONF_ID, |     CONF_ID, | ||||||
|  |     CONF_RESTORE, | ||||||
|     CONF_TIME_ID, |     CONF_TIME_ID, | ||||||
|     DEVICE_CLASS_ENERGY, |     DEVICE_CLASS_ENERGY, | ||||||
|     CONF_METHOD, |     CONF_METHOD, | ||||||
| @@ -36,6 +37,7 @@ CONFIG_SCHEMA = ( | |||||||
|             cv.GenerateID(): cv.declare_id(TotalDailyEnergy), |             cv.GenerateID(): cv.declare_id(TotalDailyEnergy), | ||||||
|             cv.GenerateID(CONF_TIME_ID): cv.use_id(time.RealTimeClock), |             cv.GenerateID(CONF_TIME_ID): cv.use_id(time.RealTimeClock), | ||||||
|             cv.Required(CONF_POWER_ID): cv.use_id(sensor.Sensor), |             cv.Required(CONF_POWER_ID): cv.use_id(sensor.Sensor), | ||||||
|  |             cv.Optional(CONF_RESTORE, default=True): cv.boolean, | ||||||
|             cv.Optional( |             cv.Optional( | ||||||
|                 CONF_MIN_SAVE_INTERVAL, default="0s" |                 CONF_MIN_SAVE_INTERVAL, default="0s" | ||||||
|             ): cv.positive_time_period_milliseconds, |             ): cv.positive_time_period_milliseconds, | ||||||
| @@ -70,5 +72,6 @@ async def to_code(config): | |||||||
|     cg.add(var.set_parent(sens)) |     cg.add(var.set_parent(sens)) | ||||||
|     time_ = await cg.get_variable(config[CONF_TIME_ID]) |     time_ = await cg.get_variable(config[CONF_TIME_ID]) | ||||||
|     cg.add(var.set_time(time_)) |     cg.add(var.set_time(time_)) | ||||||
|  |     cg.add(var.set_restore(config[CONF_RESTORE])) | ||||||
|     cg.add(var.set_min_save_interval(config[CONF_MIN_SAVE_INTERVAL])) |     cg.add(var.set_min_save_interval(config[CONF_MIN_SAVE_INTERVAL])) | ||||||
|     cg.add(var.set_method(config[CONF_METHOD])) |     cg.add(var.set_method(config[CONF_METHOD])) | ||||||
|   | |||||||
| @@ -7,14 +7,14 @@ namespace total_daily_energy { | |||||||
| static const char *const TAG = "total_daily_energy"; | static const char *const TAG = "total_daily_energy"; | ||||||
|  |  | ||||||
| void TotalDailyEnergy::setup() { | void TotalDailyEnergy::setup() { | ||||||
|   this->pref_ = global_preferences->make_preference<float>(this->get_object_id_hash()); |   float initial_value = 0; | ||||||
|  |  | ||||||
|   float recovered; |   if (this->restore_) { | ||||||
|   if (this->pref_.load(&recovered)) { |     this->pref_ = global_preferences->make_preference<float>(this->get_object_id_hash()); | ||||||
|     this->publish_state_and_save(recovered); |     this->pref_.load(&initial_value); | ||||||
|   } else { |  | ||||||
|     this->publish_state_and_save(0); |  | ||||||
|   } |   } | ||||||
|  |   this->publish_state_and_save(initial_value); | ||||||
|  |  | ||||||
|   this->last_update_ = millis(); |   this->last_update_ = millis(); | ||||||
|   this->last_save_ = this->last_update_; |   this->last_save_ = this->last_update_; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -17,6 +17,7 @@ enum TotalDailyEnergyMethod { | |||||||
|  |  | ||||||
| class TotalDailyEnergy : public sensor::Sensor, public Component { | class TotalDailyEnergy : public sensor::Sensor, public Component { | ||||||
|  public: |  public: | ||||||
|  |   void set_restore(bool restore) { restore_ = restore; } | ||||||
|   void set_min_save_interval(uint32_t min_interval) { this->min_save_interval_ = min_interval; } |   void set_min_save_interval(uint32_t min_interval) { this->min_save_interval_ = min_interval; } | ||||||
|   void set_time(time::RealTimeClock *time) { time_ = time; } |   void set_time(time::RealTimeClock *time) { time_ = time; } | ||||||
|   void set_parent(Sensor *parent) { parent_ = parent; } |   void set_parent(Sensor *parent) { parent_ = parent; } | ||||||
| @@ -41,6 +42,7 @@ class TotalDailyEnergy : public sensor::Sensor, public Component { | |||||||
|   uint32_t last_update_{0}; |   uint32_t last_update_{0}; | ||||||
|   uint32_t last_save_{0}; |   uint32_t last_save_{0}; | ||||||
|   uint32_t min_save_interval_{0}; |   uint32_t min_save_interval_{0}; | ||||||
|  |   bool restore_; | ||||||
|   float total_energy_{0.0f}; |   float total_energy_{0.0f}; | ||||||
|   float last_power_state_{0.0f}; |   float last_power_state_{0.0f}; | ||||||
| }; | }; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user