Skip to content

Commit 2723ab3

Browse files
authored
Add mac address as connection for matter device (home-assistant#121257)
1 parent 260e98c commit 2723ab3

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

homeassistant/components/matter/adapter.py

+30
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
from typing import TYPE_CHECKING, cast
66

7+
from chip.clusters.Objects import GeneralDiagnostics
78
from matter_server.client.models.device_types import BridgedDevice
9+
from matter_server.common.helpers.util import convert_mac_address
810
from matter_server.common.models import EventType, ServerInfoMessage
911

1012
from homeassistant.config_entries import ConfigEntry
@@ -22,6 +24,30 @@
2224
from matter_server.client.models.node import MatterEndpoint, MatterNode
2325

2426

27+
def get_connections_for_endpoint(endpoint: MatterEndpoint) -> set[tuple[str, str]]:
28+
"""Return a set of connections for a MatterEndpoint."""
29+
network_interfaces: list[GeneralDiagnostics.Structs.NetworkInterface] = (
30+
endpoint.get_attribute_value(
31+
None, GeneralDiagnostics.Attributes.NetworkInterfaces
32+
)
33+
or []
34+
)
35+
36+
hardware_addresses: set[str] = {
37+
convert_mac_address(network_interface.hardwareAddress)
38+
for network_interface in network_interfaces
39+
if network_interface.hardwareAddress
40+
}
41+
42+
return {
43+
(dr.CONNECTION_NETWORK_MAC, address)
44+
if len(address) == 17
45+
else (dr.CONNECTION_ZIGBEE, address)
46+
for address in hardware_addresses
47+
if len(address) in (17, 23) # EUI-48 -> 17, EUI-64 -> 23
48+
}
49+
50+
2551
def get_clean_name(name: str | None) -> str | None:
2652
"""Strip spaces and null char from the name."""
2753
if name is None:
@@ -185,6 +211,9 @@ def _create_device_registry(
185211
endpoint,
186212
)
187213
identifiers = {(DOMAIN, f"{ID_TYPE_DEVICE_ID}_{node_device_id}")}
214+
215+
connections = get_connections_for_endpoint(endpoint)
216+
188217
serial_number: str | None = None
189218
# if available, we also add the serialnumber as identifier
190219
if (
@@ -203,6 +232,7 @@ def _create_device_registry(
203232
name=name,
204233
config_entry_id=self.config_entry.entry_id,
205234
identifiers=identifiers,
235+
connections=connections,
206236
hw_version=basic_info.hardwareVersionString,
207237
sw_version=basic_info.softwareVersionString,
208238
manufacturer=basic_info.vendorName or endpoint.node.device_info.vendorName,

tests/components/matter/test_adapter.py

+54
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,60 @@ async def test_device_registry_single_node_composed_device(
187187
assert len(dev_reg.devices) == 1
188188

189189

190+
async def test_device_registry_single_node_with_connection(
191+
hass: HomeAssistant,
192+
device_registry: dr.DeviceRegistry,
193+
matter_client: MagicMock,
194+
) -> None:
195+
"""Test that a device with mac address adds a connection to the HA device entry."""
196+
await setup_integration_with_node_fixture(
197+
hass,
198+
"thermostat",
199+
matter_client,
200+
)
201+
202+
assert device_registry.async_get_device(connections={("mac", "DC:54:75:5F:BA:AC")})
203+
204+
205+
async def test_device_registry_single_node_without_mac_address_has_no_mac_connection(
206+
hass: HomeAssistant,
207+
device_registry: dr.DeviceRegistry,
208+
matter_client: MagicMock,
209+
) -> None:
210+
"""Test that a device without mac address doesn't have a `mac` connection in the HA device entry."""
211+
await setup_integration_with_node_fixture(
212+
hass,
213+
"temperature-sensor",
214+
matter_client,
215+
)
216+
217+
entry = device_registry.async_get_device(
218+
identifiers={
219+
(DOMAIN, "deviceid_00000000000004D2-0000000000000001-MatterNodeDevice")
220+
}
221+
)
222+
223+
for connection_type, _ in entry.connections:
224+
assert connection_type != dr.CONNECTION_NETWORK_MAC
225+
226+
227+
async def test_device_registry_node_with_EUI64_address(
228+
hass: HomeAssistant,
229+
device_registry: dr.DeviceRegistry,
230+
matter_client: MagicMock,
231+
) -> None:
232+
"""Test that a device with a mac address has a `zigbee` connection in the HA device entry."""
233+
await setup_integration_with_node_fixture(
234+
hass,
235+
"eve-energy-plug",
236+
matter_client,
237+
)
238+
239+
assert device_registry.async_get_device(
240+
connections={("zigbee", "ca:6b:4a:23:f6:f8:bb:ee")}
241+
)
242+
243+
190244
async def test_multi_endpoint_name(
191245
hass: HomeAssistant,
192246
matter_client: MagicMock,

0 commit comments

Comments
 (0)