Issue #199 - persist and don't reset the accumulation error

This commit is contained in:
Jean-Marc Collin
2023-11-17 18:11:55 +00:00
parent a5c548bbee
commit f1595f93da
5 changed files with 124 additions and 54 deletions

View File

@@ -693,6 +693,12 @@ class BaseThermostat(ClimateEntity, RestoreEntity):
EVENT_HOMEASSISTANT_START, _async_startup_internal
)
def restore_specific_previous_state(self, old_state):
"""Should be overriden in each specific thermostat
if a specific previous state or attribute should be
restored
"""
async def get_my_previous_state(self):
"""Try to get my previou state"""
# Check If we have an old state
@@ -738,6 +744,8 @@ class BaseThermostat(ClimateEntity, RestoreEntity):
old_total_energy = old_state.attributes.get(ATTR_TOTAL_ENERGY)
if old_total_energy:
self._total_energy = old_total_energy
self.restore_specific_previous_state(old_state)
else:
# No previous state, try and restore defaults
if self._target_temp is None:

View File

@@ -40,6 +40,10 @@ class PITemperatureRegulator:
"""Reset the accumulated error"""
self.accumulated_error = 0
def set_accumulated_error(self, accumulated_error):
"""Allow to persist and restore the accumulated_error"""
self.accumulated_error = accumulated_error
def set_target_temp(self, target_temp):
"""Set the new target_temp"""
self.target_temp = target_temp
@@ -85,9 +89,10 @@ class PITemperatureRegulator:
total_offset = min(self.offset_max, max(-self.offset_max, total_offset))
# If temperature is near the target_temp, reset the accumulated_error
if abs(error) < self.stabilization_threshold:
_LOGGER.debug("Stabilisation")
self.accumulated_error = 0
# Issue #199 - don't reset the accumulation error
# if abs(error) < self.stabilization_threshold:
# _LOGGER.debug("Stabilisation")
# self.accumulated_error = 0
result = round(self.target_temp + total_offset, 1)

View File

@@ -302,6 +302,18 @@ class ThermostatOverClimate(BaseThermostat):
)
)
@overrides
def restore_specific_previous_state(self, old_state):
"""Restore my specific attributes from previous state"""
old_error = old_state.attributes.get("regulation_accumulated_error")
if old_error:
self._regulation_algo.set_accumulated_error(old_error)
_LOGGER.debug(
"%s - Old regulation accumulated_error have been restored to %f",
self,
old_error,
)
@overrides
def update_custom_attributes(self):
"""Custom attributes"""