From f8b4adc13c7ec9c137f00310f1e8fd028a9979fd Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 21 Mar 2024 13:05:58 +0000 Subject: [PATCH] fix: preset mode not awailable Fixes #80 --- .../dual_smart_thermostat/climate.py | 14 +++++- tests/test_dual_mode.py | 45 ++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/custom_components/dual_smart_thermostat/climate.py b/custom_components/dual_smart_thermostat/climate.py index b4b1cc1..c7eaa6b 100644 --- a/custom_components/dual_smart_thermostat/climate.py +++ b/custom_components/dual_smart_thermostat/climate.py @@ -230,11 +230,13 @@ async def async_setup_platform( presets_dict = { key: config[value] for key, value in CONF_PRESETS.items() if value in config } + _LOGGER.debug("Presets dict: %s", presets_dict) presets = { key: values[ATTR_TEMPERATURE] for key, values in presets_dict.items() if ATTR_TEMPERATURE in values } + _LOGGER.debug("Presets: %s", presets) presets_range = { key: [values[ATTR_TARGET_TEMP_LOW], values[ATTR_TARGET_TEMP_HIGH]] for key, values in presets_dict.items() @@ -242,6 +244,7 @@ async def async_setup_platform( and ATTR_TARGET_TEMP_HIGH in values and values[ATTR_TARGET_TEMP_LOW] < values[ATTR_TARGET_TEMP_HIGH] } + _LOGGER.debug("Presets range: %s", presets_range) # Try to load presets in old format and use if new format not available in config old_presets = {k: config[v] for k, v in CONF_PRESETS_OLD.items() if v in config} @@ -401,11 +404,20 @@ def __init__( _LOGGER.debug("INIT - Setting support flag: presets - no presets set") self._presets = presets if len(presets_range): + _LOGGER.debug( + "INIT - Setting support flag: presets range: %s", presets_range + ) self._preset_range_modes = [PRESET_NONE] + list(presets_range.keys()) else: + _LOGGER.debug("INIT - Setting support flag: presets range none") self._preset_range_modes = [PRESET_NONE] self._presets_range = presets_range - self._preset_mode = PRESET_NONE + + if self._preset_range_modes: + # if range mode is enabled, we need to add the range presets to the preset modes avoiding duplicates + self._attr_preset_modes = self._attr_preset_modes + list( + set(self._preset_range_modes) - set(self._attr_preset_modes) + ) # aux heater last run time if self.aux_heater_entity_id and self.aux_heater_timeout: diff --git a/tests/test_dual_mode.py b/tests/test_dual_mode.py index ba86c5e..d433835 100644 --- a/tests/test_dual_mode.py +++ b/tests/test_dual_mode.py @@ -15,7 +15,7 @@ PRESET_SLEEP, HVACMode, ) -from homeassistant.components.climate.const import DOMAIN as CLIMATE +from homeassistant.components.climate.const import ATTR_PRESET_MODE, DOMAIN as CLIMATE from homeassistant.const import ENTITY_MATCH_ALL, STATE_OFF, STATE_ON from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError @@ -144,6 +144,49 @@ async def test_setup_gets_current_temp_from_sensor( assert hass.states.get(common.ENTITY).attributes["current_temperature"] == 18 +async def test_use_case_1( + hass: HomeAssistant, +) -> None: # noqa: F811 + """Test that current temperature is updated on entity addition.""" + hass.config.units = METRIC_SYSTEM + setup_sensor(hass, 18) + await hass.async_block_till_done() + + assert await async_setup_component( + hass, + CLIMATE, + { + "climate": { + "platform": DOMAIN, + "name": "test", + "heater": common.ENT_HEATER, + "cooler": common.ENT_COOLER, + "target_sensor": common.ENT_SENSOR, + "min_cycle_duration": timedelta(seconds=60), + "precision": 0.5, + "min_temp": 20, + "max_temp": 25, + "heat_cool_mode": True, + "target_temp_high": 30, + "target_temp_low": 10, + PRESET_AWAY: { + "target_temp_low": 0, + "target_temp_high": 50, + }, + } + }, + ) + await hass.async_block_till_done() + + state = hass.states.get(common.ENTITY) + assert state.attributes["supported_features"] == 402 + + await common.async_set_preset_mode(hass, PRESET_AWAY) + + state = hass.states.get(common.ENTITY) + assert state.attributes[ATTR_PRESET_MODE] == PRESET_AWAY + + async def test_default_setup_params( hass: HomeAssistant, setup_comp_dual # noqa: F811 ) -> None: