Use ZeroconfServiceInfo in tradfri (#60112)
Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
parent
5a40322cda
commit
70f43a1415
2 changed files with 30 additions and 9 deletions
|
@ -96,10 +96,14 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
self, discovery_info: zeroconf.ZeroconfServiceInfo
|
self, discovery_info: zeroconf.ZeroconfServiceInfo
|
||||||
) -> FlowResult:
|
) -> FlowResult:
|
||||||
"""Handle homekit discovery."""
|
"""Handle homekit discovery."""
|
||||||
await self.async_set_unique_id(discovery_info["properties"]["id"])
|
await self.async_set_unique_id(
|
||||||
self._abort_if_unique_id_configured({CONF_HOST: discovery_info["host"]})
|
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():
|
for entry in self._async_current_entries():
|
||||||
if entry.data.get(CONF_HOST) != host:
|
if entry.data.get(CONF_HOST) != host:
|
||||||
|
@ -108,7 +112,10 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
# Backwards compat, we update old entries
|
# Backwards compat, we update old entries
|
||||||
if not entry.unique_id:
|
if not entry.unique_id:
|
||||||
self.hass.config_entries.async_update_entry(
|
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")
|
return self.async_abort(reason="already_configured")
|
||||||
|
|
|
@ -4,6 +4,7 @@ from unittest.mock import AsyncMock, patch
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant import config_entries, data_entry_flow
|
from homeassistant import config_entries, data_entry_flow
|
||||||
|
from homeassistant.components import zeroconf
|
||||||
from homeassistant.components.tradfri import config_flow
|
from homeassistant.components.tradfri import config_flow
|
||||||
|
|
||||||
from . import TRADFRI_PATH
|
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(
|
flow = await hass.config_entries.flow.async_init(
|
||||||
"tradfri",
|
"tradfri",
|
||||||
context={"source": config_entries.SOURCE_HOMEKIT},
|
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(
|
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(
|
flow = await hass.config_entries.flow.async_init(
|
||||||
"tradfri",
|
"tradfri",
|
||||||
context={"source": config_entries.SOURCE_HOMEKIT},
|
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
|
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(
|
result = await hass.config_entries.flow.async_init(
|
||||||
"tradfri",
|
"tradfri",
|
||||||
context={"source": config_entries.SOURCE_HOMEKIT},
|
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
|
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(
|
result2 = await hass.config_entries.flow.async_init(
|
||||||
"tradfri",
|
"tradfri",
|
||||||
context={"source": config_entries.SOURCE_HOMEKIT},
|
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
|
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(
|
flow = await hass.config_entries.flow.async_init(
|
||||||
"tradfri",
|
"tradfri",
|
||||||
context={"source": config_entries.SOURCE_HOMEKIT},
|
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
|
assert flow["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue