From 538ef7764eaf709212874e31e39683f87e27cc0e Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Wed, 14 Feb 2024 16:36:42 +0100 Subject: [PATCH] Move Shelly thermostat check to status endpoint (#110543) * Move Shelly thermostat check to status endpoint * fix key path --- homeassistant/components/shelly/climate.py | 9 +++++++-- homeassistant/components/shelly/switch.py | 3 ++- homeassistant/components/shelly/utils.py | 5 +++++ tests/components/shelly/conftest.py | 4 ++-- tests/components/shelly/test_diagnostics.py | 3 ++- tests/components/shelly/test_switch.py | 6 +++--- 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/shelly/climate.py b/homeassistant/components/shelly/climate.py index ef0e4879540..3ceb38c84c3 100644 --- a/homeassistant/components/shelly/climate.py +++ b/homeassistant/components/shelly/climate.py @@ -43,7 +43,12 @@ from .const import ( ) from .coordinator import ShellyBlockCoordinator, ShellyRpcCoordinator, get_entry_data from .entity import ShellyRpcEntity -from .utils import async_remove_shelly_entity, get_device_entry_gen, get_rpc_key_ids +from .utils import ( + async_remove_shelly_entity, + get_device_entry_gen, + get_rpc_key_ids, + is_rpc_thermostat_internal_actuator, +) async def async_setup_entry( @@ -127,7 +132,7 @@ def async_setup_rpc_entry( for id_ in climate_key_ids: climate_ids.append(id_) - if coordinator.device.shelly.get("relay_in_thermostat", False): + if is_rpc_thermostat_internal_actuator(coordinator.device.status): # Wall Display relay is used as the thermostat actuator, # we need to remove a switch entity unique_id = f"{coordinator.mac}-switch:{id_}" diff --git a/homeassistant/components/shelly/switch.py b/homeassistant/components/shelly/switch.py index 64b4f77701b..a45fd9295f2 100644 --- a/homeassistant/components/shelly/switch.py +++ b/homeassistant/components/shelly/switch.py @@ -41,6 +41,7 @@ from .utils import ( get_rpc_key_ids, is_block_channel_type_light, is_rpc_channel_type_light, + is_rpc_thermostat_internal_actuator, ) @@ -134,7 +135,7 @@ def async_setup_rpc_entry( continue if coordinator.model == MODEL_WALL_DISPLAY: - if not coordinator.device.shelly.get("relay_in_thermostat", False): + if not is_rpc_thermostat_internal_actuator(coordinator.device.status): # Wall Display relay is not used as the thermostat actuator, # we need to remove a climate entity unique_id = f"{coordinator.mac}-thermostat:{id_}" diff --git a/homeassistant/components/shelly/utils.py b/homeassistant/components/shelly/utils.py index f5196504fe6..652b6cf99ed 100644 --- a/homeassistant/components/shelly/utils.py +++ b/homeassistant/components/shelly/utils.py @@ -367,6 +367,11 @@ def is_rpc_channel_type_light(config: dict[str, Any], channel: int) -> bool: return cast(str, con_types[channel]).lower().startswith("light") +def is_rpc_thermostat_internal_actuator(status: dict[str, Any]) -> bool: + """Return true if the thermostat uses an internal relay.""" + return cast(bool, status["sys"].get("relay_in_thermostat", False)) + + def get_rpc_input_triggers(device: RpcDevice) -> list[tuple[str, str]]: """Return list of input triggers for RPC device.""" triggers = [] diff --git a/tests/components/shelly/conftest.py b/tests/components/shelly/conftest.py index 1c09f870ae4..72a587df579 100644 --- a/tests/components/shelly/conftest.py +++ b/tests/components/shelly/conftest.py @@ -201,7 +201,6 @@ MOCK_SHELLY_RPC = { "auth_en": False, "auth_domain": None, "profile": "cover", - "relay_in_thermostat": True, } MOCK_STATUS_COAP = { @@ -248,7 +247,8 @@ MOCK_STATUS_RPC = { "available_updates": { "beta": {"version": "some_beta_version"}, "stable": {"version": "some_beta_version"}, - } + }, + "relay_in_thermostat": True, }, "voltmeter": {"voltage": 4.321}, "wifi": {"rssi": -63}, diff --git a/tests/components/shelly/test_diagnostics.py b/tests/components/shelly/test_diagnostics.py index 1cfdd494e61..d65244ee0b7 100644 --- a/tests/components/shelly/test_diagnostics.py +++ b/tests/components/shelly/test_diagnostics.py @@ -146,7 +146,8 @@ async def test_rpc_config_entry_diagnostics( "available_updates": { "beta": {"version": "some_beta_version"}, "stable": {"version": "some_beta_version"}, - } + }, + "relay_in_thermostat": True, }, "wifi": {"rssi": -63}, }, diff --git a/tests/components/shelly/test_switch.py b/tests/components/shelly/test_switch.py index 76946e4fb48..e3ba9c9da73 100644 --- a/tests/components/shelly/test_switch.py +++ b/tests/components/shelly/test_switch.py @@ -327,9 +327,9 @@ async def test_wall_display_relay_mode( climate_entity_id = "climate.test_name" switch_entity_id = "switch.test_switch_0" - new_shelly = deepcopy(mock_rpc_device.shelly) - new_shelly["relay_in_thermostat"] = False - monkeypatch.setattr(mock_rpc_device, "shelly", new_shelly) + new_status = deepcopy(mock_rpc_device.status) + new_status["sys"]["relay_in_thermostat"] = False + monkeypatch.setattr(mock_rpc_device, "status", new_status) await init_integration(hass, 2, model=MODEL_WALL_DISPLAY)