diff --git a/homeassistant/components/tradfri/config_flow.py b/homeassistant/components/tradfri/config_flow.py index f0f4016ba9bff4f7c3e2f9c412f153bb9d72f7c5..af3ed00e9742a00c4c4b2aa00320eb08f1d9e2e6 100644 --- a/homeassistant/components/tradfri/config_flow.py +++ b/homeassistant/components/tradfri/config_flow.py @@ -96,10 +96,14 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN): self, discovery_info: zeroconf.ZeroconfServiceInfo ) -> FlowResult: """Handle homekit discovery.""" - await self.async_set_unique_id(discovery_info["properties"]["id"]) - self._abort_if_unique_id_configured({CONF_HOST: discovery_info["host"]}) + await self.async_set_unique_id( + discovery_info[zeroconf.ATTR_PROPERTIES][zeroconf.ATTR_PROPERTIES_ID] + ) + self._abort_if_unique_id_configured( + {CONF_HOST: discovery_info[zeroconf.ATTR_HOST]} + ) - host = discovery_info["host"] + host = discovery_info[zeroconf.ATTR_HOST] for entry in self._async_current_entries(): if entry.data.get(CONF_HOST) != host: @@ -108,7 +112,10 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN): # Backwards compat, we update old entries if not entry.unique_id: self.hass.config_entries.async_update_entry( - entry, unique_id=discovery_info["properties"]["id"] + entry, + unique_id=discovery_info[zeroconf.ATTR_PROPERTIES][ + zeroconf.ATTR_PROPERTIES_ID + ], ) return self.async_abort(reason="already_configured") diff --git a/tests/components/tradfri/test_config_flow.py b/tests/components/tradfri/test_config_flow.py index 60f3043f4f54be356b7b19d7d7f87a742af6b3f1..b4d8034323830dd6be41679abc66838ad192e361 100644 --- a/tests/components/tradfri/test_config_flow.py +++ b/tests/components/tradfri/test_config_flow.py @@ -4,6 +4,7 @@ from unittest.mock import AsyncMock, patch import pytest from homeassistant import config_entries, data_entry_flow +from homeassistant.components import zeroconf from homeassistant.components.tradfri import config_flow from . import TRADFRI_PATH @@ -103,7 +104,10 @@ async def test_discovery_connection(hass, mock_auth, mock_entry_setup): flow = await hass.config_entries.flow.async_init( "tradfri", context={"source": config_entries.SOURCE_HOMEKIT}, - data={"host": "123.123.123.123", "properties": {"id": "homekit-id"}}, + data=zeroconf.ZeroconfServiceInfo( + host="123.123.123.123", + properties={zeroconf.ATTR_PROPERTIES_ID: "homekit-id"}, + ), ) result = await hass.config_entries.flow.async_configure( @@ -251,7 +255,9 @@ async def test_discovery_duplicate_aborted(hass): flow = await hass.config_entries.flow.async_init( "tradfri", context={"source": config_entries.SOURCE_HOMEKIT}, - data={"host": "new-host", "properties": {"id": "homekit-id"}}, + data=zeroconf.ZeroconfServiceInfo( + host="new-host", properties={zeroconf.ATTR_PROPERTIES_ID: "homekit-id"} + ), ) assert flow["type"] == data_entry_flow.RESULT_TYPE_ABORT @@ -279,7 +285,10 @@ async def test_duplicate_discovery(hass, mock_auth, mock_entry_setup): result = await hass.config_entries.flow.async_init( "tradfri", context={"source": config_entries.SOURCE_HOMEKIT}, - data={"host": "123.123.123.123", "properties": {"id": "homekit-id"}}, + data=zeroconf.ZeroconfServiceInfo( + host="123.123.123.123", + properties={zeroconf.ATTR_PROPERTIES_ID: "homekit-id"}, + ), ) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM @@ -287,7 +296,10 @@ async def test_duplicate_discovery(hass, mock_auth, mock_entry_setup): result2 = await hass.config_entries.flow.async_init( "tradfri", context={"source": config_entries.SOURCE_HOMEKIT}, - data={"host": "123.123.123.123", "properties": {"id": "homekit-id"}}, + data=zeroconf.ZeroconfServiceInfo( + host="123.123.123.123", + properties={zeroconf.ATTR_PROPERTIES_ID: "homekit-id"}, + ), ) assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT @@ -304,7 +316,9 @@ async def test_discovery_updates_unique_id(hass): flow = await hass.config_entries.flow.async_init( "tradfri", context={"source": config_entries.SOURCE_HOMEKIT}, - data={"host": "some-host", "properties": {"id": "homekit-id"}}, + data=zeroconf.ZeroconfServiceInfo( + host="some-host", properties={zeroconf.ATTR_PROPERTIES_ID: "homekit-id"} + ), ) assert flow["type"] == data_entry_flow.RESULT_TYPE_ABORT