Skip to content

Commit

Permalink
Issue #248 - Ignore preset which are 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Marc Collin committed Dec 6, 2023
1 parent cd50c9b commit d2829bb
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
20 changes: 16 additions & 4 deletions custom_components/versatile_thermostat/base_thermostat.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,8 @@ def post_init(self, entry_infos):
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(
Expand Down Expand Up @@ -1037,7 +1037,6 @@ def preset_mode(self) -> str | None:
@property
def preset_modes(self) -> list[str] | None:
"""Return a list of available preset modes.
Requires ClimateEntityFeature.PRESET_MODE.
"""
return self._attr_preset_modes
Expand Down Expand Up @@ -1197,20 +1196,33 @@ def reset_last_temperature_time(self, old_preset_mode=None):

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:
preset_mode = preset_mode + PRESET_AC_SUFFIX

_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,
]:
Expand Down
68 changes: 66 additions & 2 deletions tests/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
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

Expand Down Expand Up @@ -224,3 +228,63 @@ def find_my_entity(entity_id) -> ClimateEntity:
),
]
)


@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

0 comments on commit d2829bb

Please sign in to comment.