Use ZeroconfServiceInfo in tradfri (#60112)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-11-22 11:49:37 +01:00 committed by GitHub
parent 5a40322cda
commit 70f43a1415
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 9 deletions

View file

@ -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")

View file

@ -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