diff --git a/custom_components/versatile_thermostat/base_thermostat.py b/custom_components/versatile_thermostat/base_thermostat.py index 02dc7e7..915d499 100644 --- a/custom_components/versatile_thermostat/base_thermostat.py +++ b/custom_components/versatile_thermostat/base_thermostat.py @@ -448,8 +448,8 @@ class BaseThermostat(ClimateEntity, RestoreEntity): if len(presets): self._support_flags = SUPPORT_FLAGS | ClimateEntityFeature.PRESET_MODE - for key, val in CONF_PRESETS.items(): - if val != 0.0: + for key, _ in CONF_PRESETS.items(): + if self.find_preset_temp(key) > 0: self._attr_preset_modes.append(key) _LOGGER.debug( @@ -1037,7 +1037,6 @@ class BaseThermostat(ClimateEntity, RestoreEntity): @property def preset_modes(self) -> list[str] | None: """Return a list of available preset modes. - Requires ClimateEntityFeature.PRESET_MODE. """ return self._attr_preset_modes @@ -1197,12 +1196,25 @@ class BaseThermostat(ClimateEntity, RestoreEntity): def find_preset_temp(self, preset_mode): """Find the right temperature of a preset considering the presence if configured""" + if preset_mode is None or preset_mode == "none": + return ( + self._attr_max_temp + if self._ac_mode and self._hvac_mode == HVACMode.COOL + else self._attr_min_temp + ) + if preset_mode == PRESET_SECURITY: return ( self._target_temp ) # in security just keep the current target temperature, the thermostat should be off if preset_mode == PRESET_POWER: return self._power_temp + if preset_mode == PRESET_ACTIVITY: + return self._presets[ + self._motion_preset + if self._motion_state == STATE_ON + else self._no_motion_preset + ] else: # Select _ac presets if in COOL Mode (or over_switch with _ac_mode) if self._ac_mode and self._hvac_mode == HVACMode.COOL: @@ -1210,7 +1222,7 @@ class BaseThermostat(ClimateEntity, RestoreEntity): _LOGGER.info("%s - find preset temp: %s", self, preset_mode) - if self._presence_on is False or self._presence_state in [ + if not self._presence_on or self._presence_state in [ STATE_ON, STATE_HOME, ]: diff --git a/tests/test_start.py b/tests/test_start.py index 819ad30..5c1f73f 100644 --- a/tests/test_start.py +++ b/tests/test_start.py @@ -13,8 +13,12 @@ from homeassistant.components.climate import ClimateEntity, DOMAIN as CLIMATE_DO from pytest_homeassistant_custom_component.common import MockConfigEntry from custom_components.versatile_thermostat.base_thermostat import BaseThermostat -from custom_components.versatile_thermostat.thermostat_climate import ThermostatOverClimate -from custom_components.versatile_thermostat.thermostat_switch import ThermostatOverSwitch +from custom_components.versatile_thermostat.thermostat_climate import ( + ThermostatOverClimate, +) +from custom_components.versatile_thermostat.thermostat_switch import ( + ThermostatOverSwitch, +) from .commons import * # pylint: disable=wildcard-import, unused-wildcard-import @@ -224,3 +228,63 @@ async def test_over_4switch_full_start(hass: HomeAssistant, skip_hass_states_is_ ), ] ) + + +@pytest.mark.parametrize("expected_lingering_tasks", [True]) +@pytest.mark.parametrize("expected_lingering_timers", [True]) +async def test_over_switch_deactivate_preset( + hass: HomeAssistant, skip_hass_states_is_state +): + """Test the normal full start of a thermostat in thermostat_over_switch type""" + + entry = MockConfigEntry( + domain=DOMAIN, + title="TheOverSwitchMockName", + unique_id="uniqueId", + data={ + CONF_NAME: "TheOverSwitchMockName", + CONF_THERMOSTAT_TYPE: CONF_THERMOSTAT_SWITCH, + CONF_TEMP_SENSOR: "sensor.mock_temp_sensor", + CONF_EXTERNAL_TEMP_SENSOR: "sensor.mock_ext_temp_sensor", + CONF_CYCLE_MIN: 8, + CONF_TEMP_MIN: 15, + CONF_TEMP_MAX: 30, + "eco_temp": 17, + "comfort_temp": 0, + "boost_temp": 19, + CONF_USE_WINDOW_FEATURE: False, + CONF_USE_MOTION_FEATURE: False, + CONF_USE_POWER_FEATURE: False, + CONF_USE_PRESENCE_FEATURE: False, + CONF_HEATER: "switch.mock_switch1", + CONF_HEATER_2: None, + CONF_HEATER_3: None, + CONF_HEATER_4: None, + CONF_SECURITY_DELAY_MIN: 10, + CONF_MINIMAL_ACTIVATION_DELAY: 10, + }, + ) + + entity: BaseThermostat = await create_thermostat( + hass, entry, "climate.theoverswitchmockname" + ) + assert entity + assert isinstance(entity, ThermostatOverSwitch) + + assert entity.preset_modes == [ + PRESET_NONE, + PRESET_ECO, + # PRESET_COMFORT, + PRESET_BOOST, + ] + assert entity.preset_mode is PRESET_NONE + + # try to set the COMFORT Preset which is absent + try: + await entity.async_set_preset_mode(PRESET_COMFORT) + except ValueError as err: + print(err) + else: + assert False + finally: + assert entity.preset_mode is PRESET_NONE