diff --git a/homeassistant/components/gogogate2/config_flow.py b/homeassistant/components/gogogate2/config_flow.py index d3df62c3b6e..e71046f7974 100644 --- a/homeassistant/components/gogogate2/config_flow.py +++ b/homeassistant/components/gogogate2/config_flow.py @@ -39,7 +39,9 @@ class Gogogate2FlowHandler(ConfigFlow, domain=DOMAIN): self, discovery_info: zeroconf.ZeroconfServiceInfo ) -> data_entry_flow.FlowResult: """Handle homekit discovery.""" - await self.async_set_unique_id(discovery_info[zeroconf.ATTR_PROPERTIES]["id"]) + await self.async_set_unique_id( + discovery_info[zeroconf.ATTR_PROPERTIES][zeroconf.ATTR_PROPERTIES_ID] + ) return await self._async_discovery_handler(discovery_info[zeroconf.ATTR_HOST]) async def async_step_dhcp( diff --git a/homeassistant/components/homekit_controller/config_flow.py b/homeassistant/components/homekit_controller/config_flow.py index 905aec83fe2..557fc3894c7 100644 --- a/homeassistant/components/homekit_controller/config_flow.py +++ b/homeassistant/components/homekit_controller/config_flow.py @@ -167,7 +167,7 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): properties={ "md": record["md"], "pv": record["pv"], - "id": unique_id, + zeroconf.ATTR_PROPERTIES_ID: unique_id, "c#": record["c#"], "s#": record["s#"], "ff": record["ff"], @@ -212,7 +212,7 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): for (key, value) in discovery_info[zeroconf.ATTR_PROPERTIES].items() } - if "id" not in properties: + if zeroconf.ATTR_PROPERTIES_ID not in properties: # This can happen if the TXT record is received after the PTR record # we will wait for the next update in this case _LOGGER.debug( @@ -223,7 +223,7 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): # The hkid is a unique random number that looks like a pairing code. # It changes if a device is factory reset. - hkid = properties["id"] + hkid = properties[zeroconf.ATTR_PROPERTIES_ID] model = properties["md"] name = discovery_info[zeroconf.ATTR_NAME].replace("._hap._tcp.local.", "") status_flags = int(properties["sf"]) diff --git a/homeassistant/components/zeroconf/__init__.py b/homeassistant/components/zeroconf/__init__.py index 01b289f6d18..8735dc47b83 100644 --- a/homeassistant/components/zeroconf/__init__.py +++ b/homeassistant/components/zeroconf/__init__.py @@ -68,6 +68,9 @@ ATTR_PORT: Final = "port" ATTR_PROPERTIES: Final = "properties" ATTR_TYPE: Final = "type" +# Attributes for ZeroconfServiceInfo[ATTR_PROPERTIES] +ATTR_PROPERTIES_ID: Final = "id" + CONFIG_SCHEMA = vol.Schema( { diff --git a/tests/components/gogogate2/test_config_flow.py b/tests/components/gogogate2/test_config_flow.py index 202e7840456..a70959cfd09 100644 --- a/tests/components/gogogate2/test_config_flow.py +++ b/tests/components/gogogate2/test_config_flow.py @@ -108,7 +108,7 @@ async def test_form_homekit_unique_id_already_setup(hass): DOMAIN, context={"source": config_entries.SOURCE_HOMEKIT}, data=zeroconf.ZeroconfServiceInfo( - host="1.2.3.4", properties={"id": MOCK_MAC_ADDR} + host="1.2.3.4", properties={zeroconf.ATTR_PROPERTIES_ID: MOCK_MAC_ADDR} ), ) assert result["type"] == RESULT_TYPE_FORM @@ -130,7 +130,7 @@ async def test_form_homekit_unique_id_already_setup(hass): DOMAIN, context={"source": config_entries.SOURCE_HOMEKIT}, data=zeroconf.ZeroconfServiceInfo( - host="1.2.3.4", properties={"id": MOCK_MAC_ADDR} + host="1.2.3.4", properties={zeroconf.ATTR_PROPERTIES_ID: MOCK_MAC_ADDR} ), ) assert result["type"] == RESULT_TYPE_ABORT @@ -149,7 +149,7 @@ async def test_form_homekit_ip_address_already_setup(hass): DOMAIN, context={"source": config_entries.SOURCE_HOMEKIT}, data=zeroconf.ZeroconfServiceInfo( - host="1.2.3.4", properties={"id": MOCK_MAC_ADDR} + host="1.2.3.4", properties={zeroconf.ATTR_PROPERTIES_ID: MOCK_MAC_ADDR} ), ) assert result["type"] == RESULT_TYPE_ABORT @@ -162,7 +162,7 @@ async def test_form_homekit_ip_address(hass): DOMAIN, context={"source": config_entries.SOURCE_HOMEKIT}, data=zeroconf.ZeroconfServiceInfo( - host="1.2.3.4", properties={"id": MOCK_MAC_ADDR} + host="1.2.3.4", properties={zeroconf.ATTR_PROPERTIES_ID: MOCK_MAC_ADDR} ), ) assert result["type"] == RESULT_TYPE_FORM @@ -237,7 +237,7 @@ async def test_discovered_by_homekit_and_dhcp(hass): DOMAIN, context={"source": config_entries.SOURCE_HOMEKIT}, data=zeroconf.ZeroconfServiceInfo( - host="1.2.3.4", properties={"id": MOCK_MAC_ADDR} + host="1.2.3.4", properties={zeroconf.ATTR_PROPERTIES_ID: MOCK_MAC_ADDR} ), ) assert result["type"] == RESULT_TYPE_FORM diff --git a/tests/components/homekit_controller/test_config_flow.py b/tests/components/homekit_controller/test_config_flow.py index 7ed4b7e5422..c375dc3dd4d 100644 --- a/tests/components/homekit_controller/test_config_flow.py +++ b/tests/components/homekit_controller/test_config_flow.py @@ -148,7 +148,7 @@ def get_device_discovery_info( properties={ "md": record["md"], "pv": record["pv"], - "id": device.device_id, + zeroconf.ATTR_PROPERTIES_ID: device.device_id, "c#": record["c#"], "s#": record["s#"], "ff": record["ff"], @@ -273,7 +273,7 @@ async def test_id_missing(hass, controller): discovery_info = get_device_discovery_info(device) # Remove id from device - del discovery_info["properties"]["id"] + del discovery_info[zeroconf.ATTR_PROPERTIES][zeroconf.ATTR_PROPERTIES_ID] # Device is discovered result = await hass.config_entries.flow.async_init( @@ -289,7 +289,9 @@ async def test_discovery_ignored_model(hass, controller): """Already paired.""" device = setup_mock_accessory(controller) discovery_info = get_device_discovery_info(device) - discovery_info[zeroconf.ATTR_PROPERTIES]["id"] = "AA:BB:CC:DD:EE:FF" + discovery_info[zeroconf.ATTR_PROPERTIES][ + zeroconf.ATTR_PROPERTIES_ID + ] = "AA:BB:CC:DD:EE:FF" discovery_info[zeroconf.ATTR_PROPERTIES]["md"] = "HHKBridge1,1" # Device is discovered @@ -317,7 +319,9 @@ async def test_discovery_ignored_hk_bridge(hass, controller): connections={(device_registry.CONNECTION_NETWORK_MAC, formatted_mac)}, ) - discovery_info[zeroconf.ATTR_PROPERTIES]["id"] = "AA:BB:CC:DD:EE:FF" + discovery_info[zeroconf.ATTR_PROPERTIES][ + zeroconf.ATTR_PROPERTIES_ID + ] = "AA:BB:CC:DD:EE:FF" # Device is discovered result = await hass.config_entries.flow.async_init( @@ -344,7 +348,9 @@ async def test_discovery_does_not_ignore_non_homekit(hass, controller): connections={(device_registry.CONNECTION_NETWORK_MAC, formatted_mac)}, ) - discovery_info[zeroconf.ATTR_PROPERTIES]["id"] = "AA:BB:CC:DD:EE:FF" + discovery_info[zeroconf.ATTR_PROPERTIES][ + zeroconf.ATTR_PROPERTIES_ID + ] = "AA:BB:CC:DD:EE:FF" # Device is discovered result = await hass.config_entries.flow.async_init( @@ -486,7 +492,9 @@ async def test_discovery_already_configured_update_csharp(hass, controller): # Set device as already paired discovery_info[zeroconf.ATTR_PROPERTIES]["sf"] = 0x00 discovery_info[zeroconf.ATTR_PROPERTIES]["c#"] = 99999 - discovery_info[zeroconf.ATTR_PROPERTIES]["id"] = "AA:BB:CC:DD:EE:FF" + discovery_info[zeroconf.ATTR_PROPERTIES][ + zeroconf.ATTR_PROPERTIES_ID + ] = "AA:BB:CC:DD:EE:FF" # Device is discovered result = await hass.config_entries.flow.async_init(