Notify HomeKit clients when falling back from an unsupported thermostat mode (#193048)

This commit is contained in:
jieinfo
2026-11-24 07:53:58 +00:00
committed by GitHub
parent 05a976eb1a
commit e11bdf8dad
2 changed files with 87 additions and 1 deletions
@@ -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[
@@ -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: