Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mac address as connection for matter device #121257

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions homeassistant/components/matter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

from typing import TYPE_CHECKING, cast

from chip.clusters.Objects import GeneralDiagnostics
from matter_server.client.models.device_types import BridgedDevice
from matter_server.common.helpers.util import convert_mac_address
from matter_server.common.models import EventType, ServerInfoMessage

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


def get_connections_for_endpoint(endpoint: MatterEndpoint) -> set[tuple[str, str]]:
"""Return a set of connections for a MatterEndpoint."""
network_interfaces: list[GeneralDiagnostics.Structs.NetworkInterface] = (
endpoint.get_attribute_value(
None, GeneralDiagnostics.Attributes.NetworkInterfaces
)
or []
)

hardware_addresses: set[str] = {
convert_mac_address(network_interface.hardwareAddress)
for network_interface in network_interfaces
if network_interface.hardwareAddress
}

return {
(dr.CONNECTION_NETWORK_MAC, address)
if len(address) == 17
else (dr.CONNECTION_ZIGBEE, address)
for address in hardware_addresses
if len(address) in (17, 23) # EUI-48 -> 17, EUI-64 -> 23
}


def get_clean_name(name: str | None) -> str | None:
"""Strip spaces and null char from the name."""
if name is None:
Expand Down Expand Up @@ -185,6 +211,9 @@ def _create_device_registry(
endpoint,
)
identifiers = {(DOMAIN, f"{ID_TYPE_DEVICE_ID}_{node_device_id}")}

connections = get_connections_for_endpoint(endpoint)

serial_number: str | None = None
# if available, we also add the serialnumber as identifier
if (
Expand All @@ -203,6 +232,7 @@ def _create_device_registry(
name=name,
config_entry_id=self.config_entry.entry_id,
identifiers=identifiers,
connections=connections,
hw_version=basic_info.hardwareVersionString,
sw_version=basic_info.softwareVersionString,
manufacturer=basic_info.vendorName or endpoint.node.device_info.vendorName,
Expand Down
54 changes: 54 additions & 0 deletions tests/components/matter/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,60 @@ async def test_device_registry_single_node_composed_device(
assert len(dev_reg.devices) == 1


async def test_device_registry_single_node_with_connection(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
matter_client: MagicMock,
) -> None:
"""Test that a device with mac address adds a connection to the HA device entry."""
await setup_integration_with_node_fixture(
hass,
"thermostat",
matter_client,
)

assert device_registry.async_get_device(connections={("mac", "DC:54:75:5F:BA:AC")})


async def test_device_registry_single_node_without_mac_address_has_no_mac_connection(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
matter_client: MagicMock,
) -> None:
"""Test that a device without mac address doesn't have a `mac` connection in the HA device entry."""
await setup_integration_with_node_fixture(
hass,
"temperature-sensor",
matter_client,
)

entry = device_registry.async_get_device(
identifiers={
(DOMAIN, "deviceid_00000000000004D2-0000000000000001-MatterNodeDevice")
}
)

for connection_type, _ in entry.connections:
assert connection_type != dr.CONNECTION_NETWORK_MAC


async def test_device_registry_node_with_EUI64_address(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
matter_client: MagicMock,
) -> None:
"""Test that a device with a mac address has a `zigbee` connection in the HA device entry."""
await setup_integration_with_node_fixture(
hass,
"eve-energy-plug",
matter_client,
)

assert device_registry.async_get_device(
connections={("zigbee", "ca:6b:4a:23:f6:f8:bb:ee")}
)


async def test_multi_endpoint_name(
hass: HomeAssistant,
matter_client: MagicMock,
Expand Down