From 21f5ac77154e3e1b3ba0c2ac856d54cbceffea00 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Tue, 28 May 2024 12:47:46 +0200 Subject: [PATCH] Fix Matter device ID for non-bridged composed device (#118256) --- homeassistant/components/matter/helpers.py | 12 ++++++------ tests/components/matter/test_adapter.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/matter/helpers.py b/homeassistant/components/matter/helpers.py index cab9b602753..fc06bfd4822 100644 --- a/homeassistant/components/matter/helpers.py +++ b/homeassistant/components/matter/helpers.py @@ -59,12 +59,12 @@ def get_device_id( ) -> str: """Return HA device_id for the given MatterEndpoint.""" operational_instance_id = get_operational_instance_id(server_info, endpoint.node) - # Append endpoint ID if this endpoint is a bridged or composed device - if endpoint.is_composed_device: - compose_parent = endpoint.node.get_compose_parent(endpoint.endpoint_id) - assert compose_parent is not None - postfix = str(compose_parent.endpoint_id) - elif endpoint.is_bridged_device: + # if this is a composed device we need to get the compose parent + # example: Philips Hue motion sensor on Hue Hub (bridged to Matter) + if compose_parent := endpoint.node.get_compose_parent(endpoint.endpoint_id): + endpoint = compose_parent + if endpoint.is_bridged_device: + # Append endpoint ID if this endpoint is a bridged device postfix = str(endpoint.endpoint_id) else: # this should be compatible with previous versions diff --git a/tests/components/matter/test_adapter.py b/tests/components/matter/test_adapter.py index 5f6c48dfcc6..415ea91d58b 100644 --- a/tests/components/matter/test_adapter.py +++ b/tests/components/matter/test_adapter.py @@ -172,6 +172,20 @@ async def test_node_added_subscription( assert entity_state +async def test_device_registry_single_node_composed_device( + hass: HomeAssistant, + matter_client: MagicMock, +) -> None: + """Test that a composed device within a standalone node only creates one HA device entry.""" + await setup_integration_with_node_fixture( + hass, + "air-purifier", + matter_client, + ) + dev_reg = dr.async_get(hass) + assert len(dev_reg.devices) == 1 + + async def test_get_clean_name_() -> None: """Test get_clean_name helper.