From 40c6832cc17048866b8c8dca0c09b1b15b29bf64 Mon Sep 17 00:00:00 2001 From: Jc2k Date: Sun, 13 Feb 2022 18:45:30 +0000 Subject: [PATCH] Update homekit_controller to use the new typed discovery data (#66462) --- .../homekit_controller/config_flow.py | 23 ++++++------- .../homekit_controller/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- .../homekit_controller/test_config_flow.py | 33 +++++++++---------- 5 files changed, 29 insertions(+), 33 deletions(-) diff --git a/homeassistant/components/homekit_controller/config_flow.py b/homeassistant/components/homekit_controller/config_flow.py index 48551129b67..d41eb0ed220 100644 --- a/homeassistant/components/homekit_controller/config_flow.py +++ b/homeassistant/components/homekit_controller/config_flow.py @@ -36,8 +36,6 @@ HOMEKIT_IGNORE = [ PAIRING_FILE = "pairing.json" -MDNS_SUFFIX = "._hap._tcp.local." - PIN_FORMAT = re.compile(r"^(\d{3})-{0,1}(\d{2})-{0,1}(\d{3})$") _LOGGER = logging.getLogger(__name__) @@ -113,9 +111,10 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): if user_input is not None: key = user_input["device"] - self.hkid = self.devices[key].device_id - self.model = self.devices[key].info["md"] - self.name = key[: -len(MDNS_SUFFIX)] if key.endswith(MDNS_SUFFIX) else key + self.hkid = self.devices[key].description.id + self.model = self.devices[key].description.model + self.name = self.devices[key].description.name + await self.async_set_unique_id( normalize_hkid(self.hkid), raise_on_progress=False ) @@ -127,12 +126,10 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): self.devices = {} - async for host in self.controller.async_discover(): - status_flags = int(host.info["sf"]) - paired = not status_flags & 0x01 - if paired: + async for discovery in self.controller.async_discover(): + if discovery.paired: continue - self.devices[host.info["name"]] = host + self.devices[discovery.description.name] = discovery if not self.devices: return self.async_abort(reason="no_devices") @@ -158,9 +155,9 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): except aiohomekit.AccessoryNotFoundError: return self.async_abort(reason="accessory_not_found_error") - self.name = device.info["name"].replace("._hap._tcp.local.", "") - self.model = device.info["md"] - self.hkid = normalize_hkid(device.info["id"]) + self.name = device.description.name + self.model = device.description.model + self.hkid = device.description.id return self._async_step_pair_show_form() diff --git a/homeassistant/components/homekit_controller/manifest.json b/homeassistant/components/homekit_controller/manifest.json index 43b0a614f44..44de321db4a 100644 --- a/homeassistant/components/homekit_controller/manifest.json +++ b/homeassistant/components/homekit_controller/manifest.json @@ -3,7 +3,7 @@ "name": "HomeKit Controller", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/homekit_controller", - "requirements": ["aiohomekit==0.7.13"], + "requirements": ["aiohomekit==0.7.14"], "zeroconf": ["_hap._tcp.local."], "after_dependencies": ["zeroconf"], "codeowners": ["@Jc2k", "@bdraco"], diff --git a/requirements_all.txt b/requirements_all.txt index 3f200675ad2..14d97bec67a 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -184,7 +184,7 @@ aioguardian==2021.11.0 aioharmony==0.2.9 # homeassistant.components.homekit_controller -aiohomekit==0.7.13 +aiohomekit==0.7.14 # homeassistant.components.emulated_hue # homeassistant.components.http diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 330f03b7b9c..d8c0eeaab49 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -134,7 +134,7 @@ aioguardian==2021.11.0 aioharmony==0.2.9 # homeassistant.components.homekit_controller -aiohomekit==0.7.13 +aiohomekit==0.7.14 # homeassistant.components.emulated_hue # homeassistant.components.http diff --git a/tests/components/homekit_controller/test_config_flow.py b/tests/components/homekit_controller/test_config_flow.py index 54408e5882d..a65d63b1af2 100644 --- a/tests/components/homekit_controller/test_config_flow.py +++ b/tests/components/homekit_controller/test_config_flow.py @@ -85,7 +85,7 @@ def _setup_flow_handler(hass, pairing=None): finish_pairing = unittest.mock.AsyncMock(return_value=pairing) discovery = mock.Mock() - discovery.device_id = "00:00:00:00:00:00" + discovery.description.id = "00:00:00:00:00:00" discovery.async_start_pairing = unittest.mock.AsyncMock(return_value=finish_pairing) flow.controller = mock.Mock() @@ -136,22 +136,21 @@ def get_device_discovery_info( device, upper_case_props=False, missing_csharp=False ) -> zeroconf.ZeroconfServiceInfo: """Turn a aiohomekit format zeroconf entry into a homeassistant one.""" - record = device.info result = zeroconf.ZeroconfServiceInfo( - host=record["address"], - addresses=[record["address"]], - hostname=record["name"], - name=record["name"], - port=record["port"], + host="127.0.0.1", + hostname=device.description.name, + name=device.description.name, + addresses=["127.0.0.1"], + port=8080, properties={ - "md": record["md"], - "pv": record["pv"], - zeroconf.ATTR_PROPERTIES_ID: device.device_id, - "c#": record["c#"], - "s#": record["s#"], - "ff": record["ff"], - "ci": record["ci"], - "sf": 0x01, # record["sf"], + "md": device.description.model, + "pv": "1.0", + zeroconf.ATTR_PROPERTIES_ID: device.description.id, + "c#": device.description.config_num, + "s#": device.description.state_num, + "ff": "0", + "ci": "0", + "sf": "1", "sh": "", }, type="_hap._tcp.local.", @@ -787,7 +786,7 @@ async def test_user_no_unpaired_devices(hass, controller): device = setup_mock_accessory(controller) # Pair the mock device so that it shows as paired in discovery - finish_pairing = await device.async_start_pairing(device.device_id) + finish_pairing = await device.async_start_pairing(device.description.id) await finish_pairing(device.pairing_code) # Device discovery is requested @@ -807,7 +806,7 @@ async def test_unignore_works(hass, controller): result = await hass.config_entries.flow.async_init( "homekit_controller", context={"source": config_entries.SOURCE_UNIGNORE}, - data={"unique_id": device.device_id}, + data={"unique_id": device.description.id}, ) assert result["type"] == "form" assert result["step_id"] == "pair"