Skip to content

Commit

Permalink
fix: preset reselection should reset environment targets
Browse files Browse the repository at this point in the history
Fixes #204
  • Loading branch information
= authored and swingerman committed May 28, 2024
1 parent a879d9f commit d19ace0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ def set_preset_mode(self, preset_mode: str) -> None:
raise ValueError(
f"Got unsupported preset_mode {preset_mode}. Must be one of {self.preset_modes}"
)
if preset_mode == self._preset_mode:
# I don't think we need to call async_write_ha_state if we didn't change the state
if preset_mode == PRESET_NONE and preset_mode == self._preset_mode:
return
# if preset_mode == self._preset_mode we still need to continue
# to set the target environment to the preset mode
if preset_mode == PRESET_NONE:
self._set_presets_when_no_preset_mode()
else:
Expand Down
57 changes: 57 additions & 0 deletions tests/test_dual_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,63 @@ async def test_set_heat_cool_preset_mode_and_restore_prev_temp(
assert state.attributes.get("target_temp_high") == 22


@pytest.mark.parametrize(
("preset", "temp_low", "temp_high"),
[
(PRESET_AWAY, 16, 30),
(PRESET_COMFORT, 20, 27),
(PRESET_ECO, 18, 29),
(PRESET_HOME, 19, 23),
(PRESET_SLEEP, 17, 24),
(PRESET_ACTIVITY, 21, 28),
(PRESET_ANTI_FREEZE, 5, 32),
],
)
async def test_set_heat_cool_preset_mode_and_restore_prev_temp_2(
hass: HomeAssistant,
setup_comp_heat_cool_presets, # noqa: F811
preset,
temp_low,
temp_high,
) -> None:
"""Test the setting preset mode.
Verify original temperature is restored.
And verifies that if the preset set again it's temps are match the preset
"""
await common.async_set_temperature(hass, 23, common.ENTITY, 22, 18)
await common.async_set_preset_mode(hass, preset)
state = hass.states.get(common.ENTITY)
assert state.attributes.get("target_temp_low") == temp_low
assert state.attributes.get("target_temp_high") == temp_high

# set temperature updates targets and keeps preset
await common.async_set_temperature(hass, 23, common.ENTITY, 24, 17)
await hass.async_block_till_done()
state = hass.states.get(common.ENTITY)
assert state.attributes.get("target_temp_low") == 17
assert state.attributes.get("target_temp_high") == 24
assert state.attributes.get("preset_mode") == preset

# set preset moe again should set the temps to the preset
await common.async_set_preset_mode(hass, preset)
state = hass.states.get(common.ENTITY)
assert state.attributes.get("target_temp_low") == temp_low
assert state.attributes.get("target_temp_high") == temp_high

# preset non should restore the original temps
await common.async_set_preset_mode(hass, PRESET_NONE)
state = hass.states.get(common.ENTITY)
assert state.attributes.get("target_temp_low") == 18
assert state.attributes.get("target_temp_high") == 22

# set preset moe again should set the temps to the preset
await common.async_set_preset_mode(hass, preset)
state = hass.states.get(common.ENTITY)
assert state.attributes.get("target_temp_low") == temp_low
assert state.attributes.get("target_temp_high") == temp_high


@pytest.mark.parametrize(
("preset", "temp_low", "temp_high"),
[
Expand Down

0 comments on commit d19ace0

Please sign in to comment.