From 59170d3c54bec9ff8ca06dcbe43c5f58d3c77910 Mon Sep 17 00:00:00 2001 From: Jc2k Date: Sun, 10 Jul 2022 19:50:54 +0100 Subject: [PATCH] Prepare homekit_controller for _hap._udp.local. (#74857) * Prepare homekit_controller for _hap._udp.local. --- .../homekit_controller/config_flow.py | 7 ++++++- .../homekit_controller/manifest.json | 4 ++-- homeassistant/generated/zeroconf.py | 5 +++++ requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/homekit_controller/common.py | 2 +- .../homekit_controller/test_config_flow.py | 19 ++++++++++++++++++- 7 files changed, 34 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/homekit_controller/config_flow.py b/homeassistant/components/homekit_controller/config_flow.py index 24a04874f20..493dd05a8b2 100644 --- a/homeassistant/components/homekit_controller/config_flow.py +++ b/homeassistant/components/homekit_controller/config_flow.py @@ -8,6 +8,7 @@ from typing import Any import aiohomekit from aiohomekit.exceptions import AuthenticationError from aiohomekit.model import Accessories, CharacteristicsTypes, ServicesTypes +from aiohomekit.utils import domain_supported, domain_to_name import voluptuous as vol from homeassistant import config_entries @@ -203,8 +204,12 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): hkid = properties[zeroconf.ATTR_PROPERTIES_ID] normalized_hkid = normalize_hkid(hkid) + # If this aiohomekit doesn't support this particular device, ignore it. + if not domain_supported(discovery_info.name): + return self.async_abort(reason="ignored_model") + model = properties["md"] - name = discovery_info.name.replace("._hap._tcp.local.", "") + name = domain_to_name(discovery_info.name) status_flags = int(properties["sf"]) paired = not status_flags & 0x01 diff --git a/homeassistant/components/homekit_controller/manifest.json b/homeassistant/components/homekit_controller/manifest.json index 6517b078454..a83d6264603 100644 --- a/homeassistant/components/homekit_controller/manifest.json +++ b/homeassistant/components/homekit_controller/manifest.json @@ -3,8 +3,8 @@ "name": "HomeKit Controller", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/homekit_controller", - "requirements": ["aiohomekit==0.7.21"], - "zeroconf": ["_hap._tcp.local."], + "requirements": ["aiohomekit==0.7.22"], + "zeroconf": ["_hap._tcp.local.", "_hap._udp.local."], "after_dependencies": ["zeroconf"], "codeowners": ["@Jc2k", "@bdraco"], "iot_class": "local_push", diff --git a/homeassistant/generated/zeroconf.py b/homeassistant/generated/zeroconf.py index 3c9d21d1d95..5284eef02a7 100644 --- a/homeassistant/generated/zeroconf.py +++ b/homeassistant/generated/zeroconf.py @@ -167,6 +167,11 @@ ZEROCONF = { "name": "*z.wave-me*" } ], + "_hap._udp.local.": [ + { + "domain": "homekit_controller" + } + ], "_homekit._tcp.local.": [ { "domain": "homekit" diff --git a/requirements_all.txt b/requirements_all.txt index c25a6dd40eb..6141bb5f450 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -168,7 +168,7 @@ aioguardian==2022.03.2 aioharmony==0.2.9 # homeassistant.components.homekit_controller -aiohomekit==0.7.21 +aiohomekit==0.7.22 # homeassistant.components.emulated_hue # homeassistant.components.http diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 832bfa1bdee..0c879ca216a 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -152,7 +152,7 @@ aioguardian==2022.03.2 aioharmony==0.2.9 # homeassistant.components.homekit_controller -aiohomekit==0.7.21 +aiohomekit==0.7.22 # homeassistant.components.emulated_hue # homeassistant.components.http diff --git a/tests/components/homekit_controller/common.py b/tests/components/homekit_controller/common.py index 749bd4b0f07..4cad585d135 100644 --- a/tests/components/homekit_controller/common.py +++ b/tests/components/homekit_controller/common.py @@ -224,7 +224,7 @@ async def device_config_changed(hass, accessories): host="127.0.0.1", addresses=["127.0.0.1"], hostname="mock_hostname", - name="TestDevice", + name="TestDevice._hap._tcp.local.", port=8080, properties={ "md": "TestDevice", diff --git a/tests/components/homekit_controller/test_config_flow.py b/tests/components/homekit_controller/test_config_flow.py index 170a2a797d0..7b62c1e9d6d 100644 --- a/tests/components/homekit_controller/test_config_flow.py +++ b/tests/components/homekit_controller/test_config_flow.py @@ -141,7 +141,7 @@ def get_device_discovery_info( result = zeroconf.ZeroconfServiceInfo( host="127.0.0.1", hostname=device.description.name, - name=device.description.name, + name=device.description.name + "._hap._tcp.local.", addresses=["127.0.0.1"], port=8080, properties={ @@ -265,6 +265,23 @@ async def test_pair_already_paired_1(hass, controller): assert result["reason"] == "already_paired" +async def test_unknown_domain_type(hass, controller): + """Test that aiohomekit can reject discoveries it doesn't support.""" + device = setup_mock_accessory(controller) + # Flag device as already paired + discovery_info = get_device_discovery_info(device) + discovery_info.name = "TestDevice._music._tap.local." + + # Device is discovered + result = await hass.config_entries.flow.async_init( + "homekit_controller", + context={"source": config_entries.SOURCE_ZEROCONF}, + data=discovery_info, + ) + assert result["type"] == "abort" + assert result["reason"] == "ignored_model" + + async def test_id_missing(hass, controller): """Test id is missing.""" device = setup_mock_accessory(controller)