From d6f33d57962b36d37d731e6bb3267ee224c0dabd Mon Sep 17 00:00:00 2001 From: Jean-Marc Collin Date: Tue, 16 Apr 2024 07:14:38 +0200 Subject: [PATCH] [#438] - manage total_energy none until restored or calculated (#446) Co-authored-by: Jean-Marc Collin --- .../versatile_thermostat/base_thermostat.py | 11 +++++++---- custom_components/versatile_thermostat/sensor.py | 12 ++++++------ .../versatile_thermostat/thermostat_climate.py | 6 +++++- .../versatile_thermostat/thermostat_switch.py | 6 +++++- .../versatile_thermostat/thermostat_valve.py | 6 +++++- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/custom_components/versatile_thermostat/base_thermostat.py b/custom_components/versatile_thermostat/base_thermostat.py index f6162cb..5ce03e2 100644 --- a/custom_components/versatile_thermostat/base_thermostat.py +++ b/custom_components/versatile_thermostat/base_thermostat.py @@ -532,7 +532,7 @@ class BaseThermostat(ClimateEntity, RestoreEntity, Generic[T]): self._overpowering_state = None self._presence_state = None - self._total_energy = 0 + self._total_energy = None # Read the parameter from configuration.yaml if it exists short_ema_params = DEFAULT_SHORT_EMA_PARAMS @@ -860,8 +860,7 @@ class BaseThermostat(ClimateEntity, RestoreEntity, Generic[T]): self._hvac_mode = HVACMode.OFF old_total_energy = old_state.attributes.get(ATTR_TOTAL_ENERGY) - if old_total_energy: - self._total_energy = old_total_energy + self._total_energy = old_total_energy if old_total_energy else 0 self.restore_specific_previous_state(old_state) else: @@ -874,6 +873,7 @@ class BaseThermostat(ClimateEntity, RestoreEntity, Generic[T]): _LOGGER.warning( "No previously saved temperature, setting to %s", self._target_temp ) + self._total_energy = 0 self._saved_target_temp = self._target_temp @@ -1063,7 +1063,10 @@ class BaseThermostat(ClimateEntity, RestoreEntity, Generic[T]): @property def total_energy(self) -> float | None: """Returns the total energy calculated for this thermostast""" - return round(self._total_energy, 2) + if self._total_energy is not None: + return round(self._total_energy, 2) + else: + return None @property def device_power(self) -> float | None: diff --git a/custom_components/versatile_thermostat/sensor.py b/custom_components/versatile_thermostat/sensor.py index 4fc5c02..e917b9f 100644 --- a/custom_components/versatile_thermostat/sensor.py +++ b/custom_components/versatile_thermostat/sensor.py @@ -125,15 +125,15 @@ class EnergySensor(VersatileThermostatBaseEntity, SensorEntity): """Called when my climate have change""" _LOGGER.debug("%s - climate state change", self._attr_unique_id) - if math.isnan(self.my_climate.total_energy) or math.isinf( - self.my_climate.total_energy - ): + energy = self.my_climate.total_energy + if energy is None: + return + + if math.isnan(energy) or math.isinf(energy): raise ValueError(f"Sensor has illegal state {self.my_climate.total_energy}") old_state = self._attr_native_value - self._attr_native_value = round( - self.my_climate.total_energy, self.suggested_display_precision - ) + self._attr_native_value = round(energy, self.suggested_display_precision) if old_state != self._attr_native_value: self.async_write_ha_state() return diff --git a/custom_components/versatile_thermostat/thermostat_climate.py b/custom_components/versatile_thermostat/thermostat_climate.py index 47ed4e7..de2042f 100644 --- a/custom_components/versatile_thermostat/thermostat_climate.py +++ b/custom_components/versatile_thermostat/thermostat_climate.py @@ -581,7 +581,11 @@ class ThermostatOverClimate(BaseThermostat[UnderlyingClimate]): ): added_energy = self._device_power * self._underlying_climate_delta_t - self._total_energy += added_energy + if self._total_energy is None: + self._total_energy = added_energy + else: + self._total_energy += added_energy + _LOGGER.debug( "%s - added energy is %.3f . Total energy is now: %.3f", self, diff --git a/custom_components/versatile_thermostat/thermostat_switch.py b/custom_components/versatile_thermostat/thermostat_switch.py index 46aa889..cdfd8b9 100644 --- a/custom_components/versatile_thermostat/thermostat_switch.py +++ b/custom_components/versatile_thermostat/thermostat_switch.py @@ -199,7 +199,11 @@ class ThermostatOverSwitch(BaseThermostat[UnderlyingSwitch]): if not self.is_over_climate and self.mean_cycle_power is not None: added_energy = self.mean_cycle_power * float(self._cycle_min) / 60.0 - self._total_energy += added_energy + if self._total_energy is None: + self._total_energy = added_energy + else: + self._total_energy += added_energy + _LOGGER.debug( "%s - added energy is %.3f . Total energy is now: %.3f", self, diff --git a/custom_components/versatile_thermostat/thermostat_valve.py b/custom_components/versatile_thermostat/thermostat_valve.py index d9cdd23..f560937 100644 --- a/custom_components/versatile_thermostat/thermostat_valve.py +++ b/custom_components/versatile_thermostat/thermostat_valve.py @@ -279,7 +279,11 @@ class ThermostatOverValve(BaseThermostat[UnderlyingValve]): # pylint: disable=a if not self.is_over_climate and self.mean_cycle_power is not None: added_energy = self.mean_cycle_power * float(self._cycle_min) / 60.0 - self._total_energy += added_energy + if self._total_energy is None: + self._total_energy = added_energy + else: + self._total_energy += added_energy + _LOGGER.debug( "%s - added energy is %.3f . Total energy is now: %.3f", self,