Skip to content

Commit

Permalink
Auto select thermostat preset when selecting temperature
Browse files Browse the repository at this point in the history
  • Loading branch information
domingues committed Dec 30, 2024
1 parent 275c15e commit 087ad44
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions homeassistant/components/generic_thermostat/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ def __init__(
else:
self._attr_preset_modes = [PRESET_NONE]
self._presets = presets
self._presets_inv = {v: k for k, v in presets.items()}

async def async_added_to_hass(self) -> None:
"""Run when entity about to be added."""
Expand Down Expand Up @@ -421,6 +422,7 @@ async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperature."""
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
return
self._attr_preset_mode = self._presets_inv.get(temperature, PRESET_NONE)
self._target_temp = temperature
await self._async_control_heating(force=True)
self.async_write_ha_state()
Expand Down
14 changes: 14 additions & 0 deletions tests/components/generic_thermostat/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,20 @@ async def test_set_target_temp(hass: HomeAssistant) -> None:
assert state.attributes.get("temperature") == 30.0


@pytest.mark.usefixtures("setup_comp_2")
async def test_set_target_temp_change_preset(hass: HomeAssistant) -> None:
"""Test the setting of the target temperature.
Verify that preset is changed.
"""
await common.async_set_temperature(hass, 30)
state = hass.states.get(ENTITY)
assert state.attributes.get("preset_mode") == PRESET_NONE
await common.async_set_temperature(hass, 20)
state = hass.states.get(ENTITY)
assert state.attributes.get("preset_mode") == PRESET_COMFORT


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

0 comments on commit 087ad44

Please sign in to comment.