From 0d24862a28313031a452b5e92ee70b7aad66a841 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 30 Nov 2021 16:06:18 +0100 Subject: [PATCH] Use dataclass properties in homekit_controller discovery (#60626) Co-authored-by: epenet --- .../components/homekit_controller/config_flow.py | 9 ++++----- tests/components/homekit_controller/common.py | 15 +++++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/homekit_controller/config_flow.py b/homeassistant/components/homekit_controller/config_flow.py index 557fc3894c7..0728048ec84 100644 --- a/homeassistant/components/homekit_controller/config_flow.py +++ b/homeassistant/components/homekit_controller/config_flow.py @@ -208,8 +208,7 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): # homekit_python has code to do this, but not in a form we can # easily use, so do the bare minimum ourselves here instead. properties = { - key.lower(): value - for (key, value) in discovery_info[zeroconf.ATTR_PROPERTIES].items() + key.lower(): value for (key, value) in discovery_info.properties.items() } if zeroconf.ATTR_PROPERTIES_ID not in properties: @@ -225,7 +224,7 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): # It changes if a device is factory reset. hkid = properties[zeroconf.ATTR_PROPERTIES_ID] model = properties["md"] - name = discovery_info[zeroconf.ATTR_NAME].replace("._hap._tcp.local.", "") + name = discovery_info.name.replace("._hap._tcp.local.", "") status_flags = int(properties["sf"]) paired = not status_flags & 0x01 @@ -243,8 +242,8 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): # Set unique-id and error out if it's already configured existing_entry = await self.async_set_unique_id(normalize_hkid(hkid)) updated_ip_port = { - "AccessoryIP": discovery_info[zeroconf.ATTR_HOST], - "AccessoryPort": discovery_info[zeroconf.ATTR_PORT], + "AccessoryIP": discovery_info.host, + "AccessoryPort": discovery_info.port, } # If the device is already paired and known to us we should monitor c# diff --git a/tests/components/homekit_controller/common.py b/tests/components/homekit_controller/common.py index c3c182c8b51..49c63a761c2 100644 --- a/tests/components/homekit_controller/common.py +++ b/tests/components/homekit_controller/common.py @@ -9,6 +9,7 @@ from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.services import ServicesTypes from aiohomekit.testing import FakeController +from homeassistant.components import zeroconf from homeassistant.components.homekit_controller import config_flow from homeassistant.components.homekit_controller.const import ( CONTROLLER, @@ -118,17 +119,19 @@ async def device_config_changed(hass, accessories): accessories_obj.add_accessory(accessory) pairing.accessories = accessories_obj - discovery_info = { - "name": "TestDevice", - "host": "127.0.0.1", - "port": 8080, - "properties": { + discovery_info = zeroconf.ZeroconfServiceInfo( + host="127.0.0.1", + hostname="mock_hostname", + name="TestDevice", + port=8080, + properties={ "md": "TestDevice", "id": "00:00:00:00:00:00", "c#": "2", "sf": "0", }, - } + type="mock_type", + ) # Config Flow will abort and notify us if the discovery event is of # interest - in this case c# has incremented