Skip to content

Commit

Permalink
Fix update port and api key on deconz discovery config entry u… (#30088)
Browse files Browse the repository at this point in the history
* Fix update port and api key on discovery config entry update

* Remove coroutine from _update_entry
  • Loading branch information
frenck authored and pvizeli committed Dec 20, 2019
1 parent c9de5b9 commit 939ca63
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
18 changes: 15 additions & 3 deletions homeassistant/components/deconz/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,17 @@ async def _create_entry(self):
title="deCONZ-" + self.deconz_config[CONF_BRIDGEID], data=self.deconz_config
)

async def _update_entry(self, entry, host):
def _update_entry(self, entry, host, port, api_key=None):
"""Update existing entry."""
if entry.data[CONF_HOST] == host:
return self.async_abort(reason="already_configured")

entry.data[CONF_HOST] = host
entry.data[CONF_PORT] = port

if api_key is not None:
entry.data[CONF_API_KEY] = api_key

self.hass.config_entries.async_update_entry(entry)
return self.async_abort(reason="updated_instance")

Expand All @@ -182,7 +187,9 @@ async def async_step_ssdp(self, discovery_info):

for entry in self.hass.config_entries.async_entries(DOMAIN):
if uuid == entry.data.get(CONF_UUID):
return await self._update_entry(entry, discovery_info[CONF_HOST])
return self._update_entry(
entry, discovery_info[CONF_HOST], discovery_info[CONF_PORT]
)

bridgeid = discovery_info[ATTR_SERIAL]
if any(
Expand Down Expand Up @@ -212,7 +219,12 @@ async def async_step_hassio(self, user_input=None):

if bridgeid in gateway_entries:
entry = gateway_entries[bridgeid]
return await self._update_entry(entry, user_input[CONF_HOST])
return self._update_entry(
entry,
user_input[CONF_HOST],
user_input[CONF_PORT],
user_input[CONF_API_KEY],
)

self._hassio_discovery = user_input

Expand Down
31 changes: 26 additions & 5 deletions tests/components/deconz/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,32 +323,53 @@ async def test_hassio_update_instance(hass):
"""Test we can update an existing config entry."""
entry = MockConfigEntry(
domain=config_flow.DOMAIN,
data={config_flow.CONF_BRIDGEID: "id", config_flow.CONF_HOST: "1.2.3.4"},
data={
config_flow.CONF_BRIDGEID: "id",
config_flow.CONF_HOST: "1.2.3.4",
config_flow.CONF_PORT: 40850,
config_flow.CONF_API_KEY: "secret",
},
)
entry.add_to_hass(hass)

result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN,
data={config_flow.CONF_HOST: "mock-deconz", config_flow.CONF_SERIAL: "id"},
data={
config_flow.CONF_HOST: "mock-deconz",
config_flow.CONF_PORT: 8080,
config_flow.CONF_API_KEY: "updated",
config_flow.CONF_SERIAL: "id",
},
context={"source": "hassio"},
)

assert result["type"] == "abort"
assert result["reason"] == "updated_instance"
assert entry.data[config_flow.CONF_HOST] == "mock-deconz"
assert entry.data[config_flow.CONF_PORT] == 8080
assert entry.data[config_flow.CONF_API_KEY] == "updated"


async def test_hassio_dont_update_instance(hass):
"""Test we can update an existing config entry."""
entry = MockConfigEntry(
domain=config_flow.DOMAIN,
data={config_flow.CONF_BRIDGEID: "id", config_flow.CONF_HOST: "1.2.3.4"},
data={
config_flow.CONF_BRIDGEID: "id",
config_flow.CONF_HOST: "1.2.3.4",
config_flow.CONF_PORT: 8080,
config_flow.CONF_API_KEY: "secret",
},
)
entry.add_to_hass(hass)

result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN,
data={config_flow.CONF_HOST: "1.2.3.4", config_flow.CONF_SERIAL: "id"},
data={
config_flow.CONF_HOST: "1.2.3.4",
config_flow.CONF_PORT: 8080,
config_flow.CONF_API_KEY: "secret",
config_flow.CONF_SERIAL: "id",
},
context={"source": "hassio"},
)

Expand Down

0 comments on commit 939ca63

Please sign in to comment.