From 4c1fc396fbcfa81cfd597cb1e9c875b3a41e42b2 Mon Sep 17 00:00:00 2001 From: Jean-Marc Collin Date: Sun, 29 Sep 2024 11:34:39 +0200 Subject: [PATCH] Issue #500 - check feature is use central config is checked (#513) Co-authored-by: Jean-Marc Collin --- .../versatile_thermostat/config_flow.py | 21 +++--- tests/test_bugs.py | 75 +++++++++++++++++++ 2 files changed, 86 insertions(+), 10 deletions(-) diff --git a/custom_components/versatile_thermostat/config_flow.py b/custom_components/versatile_thermostat/config_flow.py index ba8cc64e..ead7339a 100644 --- a/custom_components/versatile_thermostat/config_flow.py +++ b/custom_components/versatile_thermostat/config_flow.py @@ -99,30 +99,31 @@ def __init__(self, infos) -> None: def _init_feature_flags(self, _): """Fix features selection depending to infos""" - is_empty: bool = False # TODO remove this not bool(infos) is_central_config = ( self._infos.get(CONF_THERMOSTAT_TYPE) == CONF_THERMOSTAT_CENTRAL_CONFIG ) self._infos[CONF_USE_WINDOW_FEATURE] = ( - is_empty + self._infos.get(CONF_USE_WINDOW_CENTRAL_CONFIG) or self._infos.get(CONF_WINDOW_SENSOR) is not None or self._infos.get(CONF_WINDOW_AUTO_OPEN_THRESHOLD) is not None ) - self._infos[CONF_USE_MOTION_FEATURE] = ( - is_empty - or self._infos.get(CONF_MOTION_SENSOR) is not None - or is_central_config - ) - self._infos[CONF_USE_POWER_FEATURE] = is_empty or ( + self._infos[CONF_USE_MOTION_FEATURE] = self._infos.get( + CONF_USE_MOTION_FEATURE + ) and (self._infos.get(CONF_MOTION_SENSOR) is not None or is_central_config) + + self._infos[CONF_USE_POWER_FEATURE] = self._infos.get( + CONF_USE_POWER_CENTRAL_CONFIG + ) or ( self._infos.get(CONF_POWER_SENSOR) is not None and self._infos.get(CONF_MAX_POWER_SENSOR) is not None ) self._infos[CONF_USE_PRESENCE_FEATURE] = ( - is_empty or self._infos.get(CONF_PRESENCE_SENSOR) is not None + self._infos.get(CONF_USE_PRESENCE_CENTRAL_CONFIG) + or self._infos.get(CONF_PRESENCE_SENSOR) is not None ) - self._infos[CONF_USE_CENTRAL_BOILER_FEATURE] = is_empty or ( + self._infos[CONF_USE_CENTRAL_BOILER_FEATURE] = ( self._infos.get(CONF_CENTRAL_BOILER_ACTIVATION_SRV) is not None and self._infos.get(CONF_CENTRAL_BOILER_DEACTIVATION_SRV) is not None ) diff --git a/tests/test_bugs.py b/tests/test_bugs.py index 353b463d..bd7f727e 100644 --- a/tests/test_bugs.py +++ b/tests/test_bugs.py @@ -12,6 +12,12 @@ SERVICE_SET_TEMPERATURE, ) +from homeassistant.config_entries import SOURCE_USER, ConfigEntry +from homeassistant.data_entry_flow import FlowResultType + +from custom_components.versatile_thermostat.config_flow import ( + VersatileThermostatBaseConfigFlow, +) from .commons import * logging.getLogger().setLevel(logging.DEBUG) @@ -1013,3 +1019,72 @@ async def test_bug_508( ), ] ) + + +@pytest.mark.parametrize("expected_lingering_tasks", [True]) +@pytest.mark.parametrize("expected_lingering_timers", [True]) +async def test_bug_500_1(hass: HomeAssistant, init_vtherm_api) -> None: + """Test that the form is served with no input""" + + config = { + CONF_THERMOSTAT_TYPE: CONF_THERMOSTAT_SWITCH, + CONF_USE_WINDOW_CENTRAL_CONFIG: True, + CONF_USE_POWER_CENTRAL_CONFIG: True, + CONF_USE_PRESENCE_CENTRAL_CONFIG: True, + CONF_USE_MOTION_FEATURE: True, + CONF_MOTION_SENSOR: "sensor.theMotionSensor", + } + + flow = VersatileThermostatBaseConfigFlow(config) + + assert flow._infos[CONF_USE_WINDOW_FEATURE] is True + assert flow._infos[CONF_USE_POWER_FEATURE] is True + assert flow._infos[CONF_USE_PRESENCE_FEATURE] is True + assert flow._infos[CONF_USE_MOTION_FEATURE] is True + + +@pytest.mark.parametrize("expected_lingering_tasks", [True]) +@pytest.mark.parametrize("expected_lingering_timers", [True]) +async def test_bug_500_2(hass: HomeAssistant, init_vtherm_api) -> None: + """Test that the form is served with no input""" + + config = { + CONF_THERMOSTAT_TYPE: CONF_THERMOSTAT_SWITCH, + CONF_USE_WINDOW_CENTRAL_CONFIG: False, + CONF_USE_POWER_CENTRAL_CONFIG: False, + CONF_USE_PRESENCE_CENTRAL_CONFIG: False, + CONF_USE_MOTION_FEATURE: False, + } + + flow = VersatileThermostatBaseConfigFlow(config) + + assert flow._infos[CONF_USE_WINDOW_FEATURE] is False + assert flow._infos[CONF_USE_POWER_FEATURE] is False + assert flow._infos[CONF_USE_PRESENCE_FEATURE] is False + assert flow._infos[CONF_USE_MOTION_FEATURE] is False + + +@pytest.mark.parametrize("expected_lingering_tasks", [True]) +@pytest.mark.parametrize("expected_lingering_timers", [True]) +async def test_bug_500_3(hass: HomeAssistant, init_vtherm_api) -> None: + """Test that the form is served with no input""" + + config = { + CONF_THERMOSTAT_TYPE: CONF_THERMOSTAT_SWITCH, + CONF_USE_WINDOW_CENTRAL_CONFIG: False, + CONF_WINDOW_SENSOR: "sensor.theWindowSensor", + CONF_USE_POWER_CENTRAL_CONFIG: False, + CONF_POWER_SENSOR: "sensor.thePowerSensor", + CONF_MAX_POWER_SENSOR: "sensor.theMaxPowerSensor", + CONF_USE_PRESENCE_CENTRAL_CONFIG: False, + CONF_PRESENCE_SENSOR: "sensor.thePresenceSensor", + CONF_USE_MOTION_FEATURE: True, # motion sensor need to be checked AND a motion sensor set + CONF_MOTION_SENSOR: "sensor.theMotionSensor", + } + + flow = VersatileThermostatBaseConfigFlow(config) + + assert flow._infos[CONF_USE_WINDOW_FEATURE] is True + assert flow._infos[CONF_USE_POWER_FEATURE] is True + assert flow._infos[CONF_USE_PRESENCE_FEATURE] is True + assert flow._infos[CONF_USE_MOTION_FEATURE] is True