Fix update port and api key on deconz discovery config entry u… (#30088)

* Fix update port and api key on discovery config entry update

* Remove coroutine from _update_entry
pull/30093/head
Franck Nijhof 2019-12-20 10:29:18 +01:00 committed by GitHub
parent 3a610edb78
commit eb0aed3653
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 8 deletions

View File

@ -159,12 +159,17 @@ class DeconzFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
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")
@ -181,7 +186,7 @@ class DeconzFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
for entry in self.hass.config_entries.async_entries(DOMAIN):
if uuid == entry.data.get(CONF_UUID):
return await self._update_entry(entry, parsed_url.hostname)
return self._update_entry(entry, parsed_url.hostname, parsed_url.port)
bridgeid = discovery_info[ssdp.ATTR_UPNP_SERIAL]
if any(
@ -211,7 +216,12 @@ class DeconzFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
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

View File

@ -322,32 +322,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"},
)