From e11bdf8dad055fd02eb0f2c56ac9214d3d1bf37b Mon Sep 17 00:00:00 2001 From: jieinfo <970504888@qq.com> Date: Tue, 24 Nov 2026 15:53:58 +0800 Subject: [PATCH] Notify HomeKit clients when falling back from an unsupported thermostat mode (#193048) --- .../components/homekit/type_thermostats.py | 3 +- .../homekit/test_type_thermostats.py | 85 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/homekit/type_thermostats.py b/homeassistant/components/homekit/type_thermostats.py index 94b7cb3fae10..a8067d1190b2 100644 --- a/homeassistant/components/homekit/type_thermostats.py +++ b/homeassistant/components/homekit/type_thermostats.py @@ -320,7 +320,8 @@ class Thermostat(HomeKitClimateAccessory): target_hc, hc_fallback, ) - self.char_target_heat_cool.value = target_hc = hc_fallback + target_hc = hc_fallback + self.char_target_heat_cool.set_value(target_hc) break params[ClimateServiceArgument.HVAC_MODE] = self.hc_homekit_to_hass[ diff --git a/tests/components/homekit/test_type_thermostats.py b/tests/components/homekit/test_type_thermostats.py index c119086cec37..b415fabdff7c 100644 --- a/tests/components/homekit/test_type_thermostats.py +++ b/tests/components/homekit/test_type_thermostats.py @@ -1299,6 +1299,91 @@ async def test_thermostat_hvac_modes_with_auto_only( assert call_set_hvac_mode[0].data[ATTR_HVAC_MODE] == HVACMode.AUTO +@pytest.mark.parametrize( + ("initial_mode", "requested_mode", "expected_mode", "expected_notifications"), + [ + pytest.param( + HVACMode.OFF, + 1, + HVACMode.AUTO, + [(1, "mock_addr"), (3, None)], + id="heat-from-off", + ), + pytest.param( + HVACMode.AUTO, + 1, + HVACMode.AUTO, + [(1, "mock_addr"), (3, None)], + id="heat-from-auto", + ), + pytest.param( + HVACMode.OFF, + 2, + HVACMode.AUTO, + [(2, "mock_addr"), (3, None)], + id="cool-from-off", + ), + pytest.param( + HVACMode.AUTO, + 2, + HVACMode.AUTO, + [(2, "mock_addr"), (3, None)], + id="cool-from-auto", + ), + pytest.param( + HVACMode.OFF, 3, HVACMode.AUTO, [(3, "mock_addr")], id="supported-auto" + ), + pytest.param( + HVACMode.AUTO, 0, HVACMode.OFF, [(0, "mock_addr")], id="supported-off" + ), + ], +) +async def test_thermostat_target_mode_correction_notifies_clients( + hass: HomeAssistant, + hk_driver: HomeDriver, + initial_mode: HVACMode, + requested_mode: int, + expected_mode: HVACMode, + expected_notifications: list[tuple[int, str | None]], +) -> None: + """Notify the writing client when an unsupported mode is normalized.""" + entity_id = "climate.test" + hass.states.async_set( + entity_id, initial_mode, {ATTR_HVAC_MODES: [HVACMode.OFF, HVACMode.AUTO]} + ) + call_set_hvac_mode = async_mock_service(hass, CLIMATE_DOMAIN, "set_hvac_mode") + await hass.async_block_till_done() + acc = Thermostat(hass, hk_driver, "Climate", entity_id, 1, None) + hk_driver.add_accessory(acc) + acc.run() + await hass.async_block_till_done() + + with patch.object(hk_driver, "publish") as publish: + hk_driver.set_characteristics( + { + HAP_REPR_CHARS: [ + { + HAP_REPR_AID: acc.aid, + HAP_REPR_IID: acc.char_target_heat_cool.to_HAP()[HAP_REPR_IID], + HAP_REPR_VALUE: requested_mode, + } + ] + }, + "mock_addr", + ) + await hass.async_block_till_done() + + assert len(call_set_hvac_mode) == 1 + assert call_set_hvac_mode[0].data == { + ATTR_ENTITY_ID: entity_id, + ATTR_HVAC_MODE: expected_mode, + } + assert [ + (notification.args[0][HAP_REPR_VALUE], notification.args[1]) + for notification in publish.call_args_list + ] == expected_notifications + + async def test_thermostat_hvac_modes_with_heat_only( hass: HomeAssistant, hk_driver ) -> None: