From 998b9a89428a67c0e394fa9d2184baf4ce38d6a1 Mon Sep 17 00:00:00 2001 From: = <=> Date: Mon, 22 Jul 2024 10:53:19 +0000 Subject: [PATCH] fix: restores preset mode temperatures on restart --- .../managers/environment_manager.py | 1 - .../managers/feature_manager.py | 1 + .../managers/preset_manager.py | 24 ++++++++++++++++++- tests/test_dual_mode.py | 10 +++++++- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/custom_components/dual_smart_thermostat/managers/environment_manager.py b/custom_components/dual_smart_thermostat/managers/environment_manager.py index 8d15e70..fb56c09 100644 --- a/custom_components/dual_smart_thermostat/managers/environment_manager.py +++ b/custom_components/dual_smart_thermostat/managers/environment_manager.py @@ -256,7 +256,6 @@ def set_temperature_range_from_hvac_mode( self.set_temperature_range(temperature, self.target_temp_low, temperature) def set_temperature_target(self, temperature: float) -> None: - _LOGGER.info("Setting target temperature: %s", temperature) if temperature is None: return diff --git a/custom_components/dual_smart_thermostat/managers/feature_manager.py b/custom_components/dual_smart_thermostat/managers/feature_manager.py index 3899059..227f6e8 100644 --- a/custom_components/dual_smart_thermostat/managers/feature_manager.py +++ b/custom_components/dual_smart_thermostat/managers/feature_manager.py @@ -230,6 +230,7 @@ def apply_old_state(self, old_state: State, hvac_mode, presets_range) -> None: if old_state is None: return + _LOGGER.debug("Features applying old state") old_supported_features = old_state.attributes.get(ATTR_SUPPORTED_FEATURES) if ( old_supported_features not in (None, 0) diff --git a/custom_components/dual_smart_thermostat/managers/preset_manager.py b/custom_components/dual_smart_thermostat/managers/preset_manager.py index 1136f7d..4f09b66 100644 --- a/custom_components/dual_smart_thermostat/managers/preset_manager.py +++ b/custom_components/dual_smart_thermostat/managers/preset_manager.py @@ -181,6 +181,14 @@ def _set_presets_when_have_preset_mode(self, preset_mode: str, hvac_mode: HVACMo self._environment.saved_target_temp_high = ( self._environment.target_temp_high ) + _LOGGER.debug( + "Preset Setting target temp low: %s", + self._presets_range[preset_mode][0], + ) + _LOGGER.debug( + "Preset Setting target temp high: %s", + self._presets_range[preset_mode][1], + ) self._environment.target_temp_low = self._presets_range[preset_mode][0] self._environment.target_temp_high = self._presets_range[preset_mode][1] else: @@ -218,10 +226,16 @@ def apply_old_state(self, old_state: State): if old_state is None: return + _LOGGER.debug("Presets applying old state") old_pres_mode = old_state.attributes.get(ATTR_PRESET_MODE) if self._features.is_range_mode: + _LOGGER.debug("Apply preset range mode - old state: %s", old_pres_mode) if self._preset_modes and old_pres_mode in self._presets_range: + _LOGGER.debug("Restoring previous preset mode range: %s", old_pres_mode) self._preset_mode = old_pres_mode + + # need to save the previous target temps + # before we apply a preset self._environment.saved_target_temp_low = ( self._environment.target_temp_low ) @@ -229,7 +243,15 @@ def apply_old_state(self, old_state: State): self._environment.target_temp_high ) + preset = self._presets_range[old_pres_mode] + if preset: + self._environment.target_temp_low = preset[0] + self._environment.target_temp_high = preset[1] + elif self._preset_modes and old_pres_mode in self._presets: - _LOGGER.debug("Restoring previous preset mode: %s", old_pres_mode) + _LOGGER.debug( + "Restoring previous preset mode target: %s", old_pres_mode + ) self._preset_mode = old_pres_mode self._environment.saved_target_temp = self._environment.target_temp + self._environment.target_temp = self._presets[old_pres_mode] diff --git a/tests/test_dual_mode.py b/tests/test_dual_mode.py index 4ab50cd..94ac569 100644 --- a/tests/test_dual_mode.py +++ b/tests/test_dual_mode.py @@ -874,6 +874,14 @@ async def test_set_heat_cool_preset_mode_and_restore_prev_temp_apply_preset_agai assert state.attributes.get("target_temp_low") == 18 assert state.attributes.get("target_temp_high") == 22 + await common.async_set_preset_mode(hass, preset) + + # targets match preset again + state = hass.states.get(common.ENTITY) + assert state.attributes.get("target_temp_low") == temp_low + assert state.attributes.get("target_temp_high") == temp_high + + # simulate restore state common.mock_restore_cache( hass, ( @@ -887,7 +895,7 @@ async def test_set_heat_cool_preset_mode_and_restore_prev_temp_apply_preset_agai hass.set_state(CoreState.starting) # targets match preset again after restart - await common.async_set_preset_mode(hass, preset) + # await common.async_set_preset_mode(hass, preset) assert state.attributes.get("target_temp_low") == temp_low assert state.attributes.get("target_temp_high") == temp_high