Skip to content

Commit

Permalink
fix: restores preset mode temperatures on restart
Browse files Browse the repository at this point in the history
  • Loading branch information
= authored and swingerman committed Jul 22, 2024
1 parent bf9c99b commit 998b9a8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -218,18 +226,32 @@ 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
)
self._environment.saved_target_temp_high = (
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]
10 changes: 9 additions & 1 deletion tests/test_dual_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
(
Expand All @@ -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

Expand Down

0 comments on commit 998b9a8

Please sign in to comment.