Skip to content

Commit

Permalink
Add unique ID to TRADFRI (home-assistant#34745)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored Apr 26, 2020
1 parent de3f0c7 commit 13c8c2e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 23 deletions.
28 changes: 15 additions & 13 deletions homeassistant/components/tradfri/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Config flow for Tradfri."""
import asyncio
from collections import OrderedDict
from uuid import uuid4

import async_timeout
Expand Down Expand Up @@ -70,7 +69,7 @@ async def async_step_auth(self, user_input=None):
else:
user_input = {}

fields = OrderedDict()
fields = {}

if self._host is None:
fields[vol.Required(CONF_HOST, default=user_input.get(CONF_HOST))] = str
Expand All @@ -83,25 +82,28 @@ async def async_step_auth(self, user_input=None):
step_id="auth", data_schema=vol.Schema(fields), errors=errors
)

async def async_step_zeroconf(self, user_input):
"""Handle zeroconf discovery."""
async def async_step_homekit(self, user_input):
"""Handle homekit discovery."""
await self.async_set_unique_id(user_input["properties"]["id"])
self._abort_if_unique_id_configured({CONF_HOST: user_input["host"]})

host = user_input["host"]

# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
self.context["host"] = host
for entry in self._async_current_entries():
if entry.data[CONF_HOST] != host:
continue

if any(host == flow["context"]["host"] for flow in self._async_in_progress()):
return self.async_abort(reason="already_in_progress")
# Backwards compat, we update old entries
if not entry.unique_id:
self.hass.config_entries.async_update_entry(
entry, unique_id=user_input["properties"]["id"]
)

for entry in self._async_current_entries():
if entry.data[CONF_HOST] == host:
return self.async_abort(reason="already_configured")
return self.async_abort(reason="already_configured")

self._host = host
return await self.async_step_auth()

async_step_homekit = async_step_zeroconf

async def async_step_import(self, user_input):
"""Import a config entry."""
for entry in self._async_current_entries():
Expand Down
1 change: 0 additions & 1 deletion homeassistant/components/tradfri/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
"homekit": {
"models": ["TRADFRI"]
},
"zeroconf": ["_coap._udp.local."],
"codeowners": ["@ggravlingen"]
}
3 changes: 0 additions & 3 deletions homeassistant/generated/zeroconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
"axis",
"doorbird"
],
"_coap._udp.local.": [
"tradfri"
],
"_elg._tcp.local.": [
"elgato"
],
Expand Down
43 changes: 37 additions & 6 deletions tests/components/tradfri/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ async def test_discovery_connection(hass, mock_auth, mock_entry_setup):
mock_auth.side_effect = lambda hass, host, code: {"host": host, "gateway_id": "bla"}

flow = await hass.config_entries.flow.async_init(
"tradfri", context={"source": "zeroconf"}, data={"host": "123.123.123.123"}
"tradfri",
context={"source": "homekit"},
data={"host": "123.123.123.123", "properties": {"id": "homekit-id"}},
)

result = await hass.config_entries.flow.async_configure(
Expand All @@ -90,6 +92,7 @@ async def test_discovery_connection(hass, mock_auth, mock_entry_setup):
assert len(mock_entry_setup.mock_calls) == 1

assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["result"].unique_id == "homekit-id"
assert result["result"].data == {
"host": "123.123.123.123",
"gateway_id": "bla",
Expand Down Expand Up @@ -218,16 +221,23 @@ async def test_import_connection_legacy_no_groups(


async def test_discovery_duplicate_aborted(hass):
"""Test a duplicate discovery host is ignored."""
MockConfigEntry(domain="tradfri", data={"host": "some-host"}).add_to_hass(hass)
"""Test a duplicate discovery host aborts and updates existing entry."""
entry = MockConfigEntry(
domain="tradfri", data={"host": "some-host"}, unique_id="homekit-id"
)
entry.add_to_hass(hass)

flow = await hass.config_entries.flow.async_init(
"tradfri", context={"source": "zeroconf"}, data={"host": "some-host"}
"tradfri",
context={"source": "homekit"},
data={"host": "new-host", "properties": {"id": "homekit-id"}},
)

assert flow["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert flow["reason"] == "already_configured"

assert entry.data["host"] == "new-host"


async def test_import_duplicate_aborted(hass):
"""Test a duplicate import host is ignored."""
Expand All @@ -244,13 +254,34 @@ async def test_import_duplicate_aborted(hass):
async def test_duplicate_discovery(hass, mock_auth, mock_entry_setup):
"""Test a duplicate discovery in progress is ignored."""
result = await hass.config_entries.flow.async_init(
"tradfri", context={"source": "zeroconf"}, data={"host": "123.123.123.123"}
"tradfri",
context={"source": "homekit"},
data={"host": "123.123.123.123", "properties": {"id": "homekit-id"}},
)

assert result["type"] == data_entry_flow.RESULT_TYPE_FORM

result2 = await hass.config_entries.flow.async_init(
"tradfri", context={"source": "zeroconf"}, data={"host": "123.123.123.123"}
"tradfri",
context={"source": "homekit"},
data={"host": "123.123.123.123", "properties": {"id": "homekit-id"}},
)

assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT


async def test_discovery_updates_unique_id(hass):
"""Test a duplicate discovery host aborts and updates existing entry."""
entry = MockConfigEntry(domain="tradfri", data={"host": "some-host"},)
entry.add_to_hass(hass)

flow = await hass.config_entries.flow.async_init(
"tradfri",
context={"source": "homekit"},
data={"host": "some-host", "properties": {"id": "homekit-id"}},
)

assert flow["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert flow["reason"] == "already_configured"

assert entry.unique_id == "homekit-id"

0 comments on commit 13c8c2e

Please sign in to comment.