Store instance name in Thread router discovery (#127253)

* Store instance name in Thread router discovery

Store the DNS-SD instance name in the Thread router discovery message.
The instance name is the actual name given to a Thread border router,
e.g. when configuring the name of a Thread border router in Apple Home
the name appears as the DNS-SD instance name. This will allow to make
the Thread border router list more user friendly.

* Use instance_name_from_service_info to get instance name
pull/127332/head
Stefan Agner 2024-10-02 21:41:55 +02:00 committed by GitHub
parent a83d38a5fe
commit 88ad7e98e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 1 deletions

View File

@ -8,7 +8,13 @@ import logging
from typing import cast
from python_otbr_api.mdns import StateBitmap
from zeroconf import BadTypeInNameException, DNSPointer, ServiceListener, Zeroconf
from zeroconf import (
BadTypeInNameException,
DNSPointer,
ServiceListener,
Zeroconf,
instance_name_from_service_info,
)
from zeroconf.asyncio import AsyncServiceInfo, AsyncZeroconf
from homeassistant.components import zeroconf
@ -37,6 +43,7 @@ TYPE_PTR = 12
class ThreadRouterDiscoveryData:
"""Thread router discovery data."""
instance_name: str
addresses: list[str]
border_agent_id: str | None
brand: str | None
@ -89,6 +96,7 @@ def async_discovery_data_from_service(
unconfigured = True
return ThreadRouterDiscoveryData(
instance_name=instance_name_from_service_info(service),
addresses=service.parsed_addresses(),
border_agent_id=border_agent_id.hex() if border_agent_id is not None else None,
brand=brand,

View File

@ -74,6 +74,7 @@ async def test_discover_routers(
assert discovered[-1] == (
"aeeb2f594b570bbf",
discovery.ThreadRouterDiscoveryData(
instance_name="HomeAssistant OpenThreadBorderRouter #0BBF",
addresses=["192.168.0.115"],
border_agent_id="230c6a1ac57f6f4be262acf32e5ef52c",
brand="homeassistant",
@ -101,6 +102,7 @@ async def test_discover_routers(
assert discovered[-1] == (
"f6a99b425a67abed",
discovery.ThreadRouterDiscoveryData(
instance_name="Google-Nest-Hub-#ABED",
addresses=["192.168.0.124"],
border_agent_id="bc3740c3e963aa8735bebecd7cc503c7",
brand="google",
@ -180,6 +182,7 @@ async def test_discover_routers_unconfigured(
router_discovered_removed.assert_called_once_with(
"aeeb2f594b570bbf",
discovery.ThreadRouterDiscoveryData(
instance_name="HomeAssistant OpenThreadBorderRouter #0BBF",
addresses=["192.168.0.115"],
border_agent_id="230c6a1ac57f6f4be262acf32e5ef52c",
brand="homeassistant",
@ -226,6 +229,7 @@ async def test_discover_routers_bad_or_missing_optional_data(
router_discovered_removed.assert_called_once_with(
"aeeb2f594b570bbf",
discovery.ThreadRouterDiscoveryData(
instance_name="HomeAssistant OpenThreadBorderRouter #0BBF",
addresses=["192.168.0.115"],
border_agent_id="230c6a1ac57f6f4be262acf32e5ef52c",
brand=None,

View File

@ -353,6 +353,7 @@ async def test_discover_routers(
assert msg == {
"event": {
"data": {
"instance_name": "HomeAssistant OpenThreadBorderRouter #0BBF",
"addresses": ["192.168.0.115"],
"border_agent_id": "230c6a1ac57f6f4be262acf32e5ef52c",
"brand": "homeassistant",
@ -388,6 +389,7 @@ async def test_discover_routers(
"brand": "google",
"extended_address": "f6a99b425a67abed",
"extended_pan_id": "9e75e256f61409a3",
"instance_name": "Google-Nest-Hub-#ABED",
"model_name": "Google Nest Hub",
"network_name": "NEST-PAN-E1AF",
"server": "2d99f293-cd8e-2770-8dd2-6675de9fa000.local.",