From f9df925181321b01f8d06a84043671fce3932649 Mon Sep 17 00:00:00 2001 From: Jean-Marc Collin Date: Sat, 9 Nov 2024 18:44:13 +0100 Subject: [PATCH] Issue #615 - VTherm switch to manual on its own (#618) Co-authored-by: Jean-Marc Collin --- .devcontainer/configuration.yaml | 1 + .../thermostat_climate.py | 3 +- tests/test_overclimate.py | 72 +++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/.devcontainer/configuration.yaml b/.devcontainer/configuration.yaml index ac57f03..583b2ab 100644 --- a/.devcontainer/configuration.yaml +++ b/.devcontainer/configuration.yaml @@ -152,6 +152,7 @@ climate: name: Underlying thermostat2 heater: input_boolean.fake_heater_switch3 target_sensor: input_number.fake_temperature_sensor1 + ac_mode: false - platform: generic_thermostat name: Underlying thermostat3 heater: input_boolean.fake_heater_switch3 diff --git a/custom_components/versatile_thermostat/thermostat_climate.py b/custom_components/versatile_thermostat/thermostat_climate.py index f7ef9ed..5e8dc0d 100644 --- a/custom_components/versatile_thermostat/thermostat_climate.py +++ b/custom_components/versatile_thermostat/thermostat_climate.py @@ -852,7 +852,8 @@ class ThermostatOverClimate(BaseThermostat[UnderlyingClimate]): changes = True # try to manage new target temperature set if state if no other changes have been found - if not changes: + # and if a target temperature have already been sent + if not changes and under.last_sent_temperature is not None: _LOGGER.debug( "Do temperature check. under.last_sent_temperature is %s, new_target_temp is %s", under.last_sent_temperature, diff --git a/tests/test_overclimate.py b/tests/test_overclimate.py index aa21565..2696877 100644 --- a/tests/test_overclimate.py +++ b/tests/test_overclimate.py @@ -320,6 +320,78 @@ async def test_bug_101( assert entity.preset_mode is PRESET_NONE +@pytest.mark.parametrize("expected_lingering_tasks", [True]) +@pytest.mark.parametrize("expected_lingering_timers", [True]) +async def test_bug_615( + hass: HomeAssistant, + skip_hass_states_is_state, + skip_turn_on_off_heater, + skip_send_event, +): + """Test that when a underlying climate target temp is changed, the VTherm don't change its own temperature target if no + target_temperature have already been sent""" + + tz = get_tz(hass) # pylint: disable=invalid-name + now: datetime = datetime.now(tz=tz) + + entry = MockConfigEntry( + domain=DOMAIN, + title="TheOverClimateMockName", + unique_id="uniqueId", + data=PARTIAL_CLIMATE_NOT_REGULATED_CONFIG, # 5 minutes security delay + ) + + # Underlying is in HEAT mode but should be shutdown at startup + fake_underlying_climate = MockClimate( + hass, "mockUniqueId", "MockClimateName", {}, HVACMode.HEAT, HVACAction.HEATING + ) + + # 1. create the thermostat + with patch( + "custom_components.versatile_thermostat.base_thermostat.BaseThermostat.send_event" + ), patch( + "custom_components.versatile_thermostat.underlyings.UnderlyingClimate.find_underlying_climate", + return_value=fake_underlying_climate, + ): + vtherm = await create_thermostat(hass, entry, "climate.theoverclimatemockname") + + assert vtherm + + assert vtherm.name == "TheOverClimateMockName" + assert vtherm.is_over_climate is True + assert vtherm.hvac_mode is HVACMode.OFF + # because in MockClimate HVACAction is HEATING if hvac_mode is not set + assert vtherm.hvac_action is HVACAction.HEATING + + # Force a preset_mode without sending a temperature (as it was restored with a preset) + vtherm._attr_preset_mode = PRESET_BOOST + + assert vtherm.target_temperature == vtherm.min_temp + assert vtherm.preset_mode is PRESET_BOOST + + with patch( + "custom_components.versatile_thermostat.underlyings.UnderlyingClimate.set_hvac_mode" + ) as mock_underlying_set_hvac_mode: + # 2. Change the target temp of underlying thermostat at now + 1 min + now = now + timedelta(minutes=1) + await send_climate_change_event_with_temperature( + vtherm, + HVACMode.OFF, + HVACMode.OFF, + HVACAction.OFF, + HVACAction.OFF, + now, + 25, + True, + "climate.mock_climate", # the underlying climate entity id + ) + # Should NOT have been taken the new target temp nor have change the preset + assert vtherm.target_temperature == vtherm.min_temp + assert vtherm.preset_mode is PRESET_BOOST + + mock_underlying_set_hvac_mode.assert_not_called() + + @pytest.mark.parametrize("expected_lingering_timers", [True]) async def test_bug_508( hass: HomeAssistant,