Support updating deCONZ host address (#22784)

* Update config flow to support updating host address
Improve tests

* Update gateway to handle new address signal

* Improve description why whe need to keep step_init
This commit is contained in:
Robert Svensson 2019-04-15 06:50:01 +02:00 committed by Paulus Schoutsen
parent 96f689a70f
commit 23cb579f9f
4 changed files with 217 additions and 116 deletions

View file

@ -11,17 +11,19 @@ from homeassistant.helpers import aiohttp_client
from .const import CONF_BRIDGEID, DEFAULT_PORT, DOMAIN
CONF_SERIAL = 'serial'
@callback
def configured_hosts(hass):
"""Return a set of the configured hosts."""
return set(entry.data[CONF_HOST] for entry
in hass.config_entries.async_entries(DOMAIN))
def configured_gateways(hass):
"""Return a set of all configured gateways."""
return {entry.data[CONF_BRIDGEID]: entry for entry
in hass.config_entries.async_entries(DOMAIN)}
@callback
def get_master_gateway(hass):
"""Return a bool telling if this is the master gateway."""
"""Return the gateway which is marked as master."""
for gateway in hass.data[DOMAIN].values():
if gateway.master:
return gateway
@ -41,6 +43,10 @@ class DeconzFlowHandler(config_entries.ConfigFlow):
self.bridges = []
self.deconz_config = {}
async def async_step_init(self, user_input=None):
"""Needed in order to not require re-translation of strings."""
return await self.async_step_user(user_input)
async def async_step_user(self, user_input=None):
"""Handle a deCONZ config flow start.
@ -140,20 +146,30 @@ class DeconzFlowHandler(config_entries.ConfigFlow):
data=self.deconz_config
)
async def _update_entry(self, entry, host):
"""Update existing entry."""
entry.data[CONF_HOST] = host
self.hass.config_entries.async_update_entry(entry)
async def async_step_discovery(self, discovery_info):
"""Prepare configuration for a discovered deCONZ bridge.
This flow is triggered by the discovery component.
"""
bridgeid = discovery_info[CONF_SERIAL]
gateway_entries = configured_gateways(self.hass)
if bridgeid in gateway_entries:
entry = gateway_entries[bridgeid]
await self._update_entry(entry, discovery_info[CONF_HOST])
return self.async_abort(reason='updated_instance')
deconz_config = {
CONF_HOST: discovery_info[CONF_HOST],
CONF_PORT: discovery_info[CONF_PORT],
CONF_BRIDGEID: discovery_info['serial']
CONF_BRIDGEID: discovery_info[CONF_SERIAL]
}
if deconz_config[CONF_HOST] in configured_hosts(self.hass):
return self.async_abort(reason='one_instance_only')
return await self.async_step_import(deconz_config)
async def async_step_import(self, import_config):
@ -180,8 +196,13 @@ class DeconzFlowHandler(config_entries.ConfigFlow):
This flow is triggered by the discovery component.
"""
if configured_hosts(self.hass):
return self.async_abort(reason='one_instance_only')
bridgeid = user_input[CONF_SERIAL]
gateway_entries = configured_gateways(self.hass)
if bridgeid in gateway_entries:
entry = gateway_entries[bridgeid]
await self._update_entry(entry, user_input[CONF_HOST])
return self.async_abort(reason='updated_instance')
self._hassio_discovery = user_input
@ -193,7 +214,7 @@ class DeconzFlowHandler(config_entries.ConfigFlow):
self.deconz_config = {
CONF_HOST: self._hassio_discovery[CONF_HOST],
CONF_PORT: self._hassio_discovery[CONF_PORT],
CONF_BRIDGEID: self._hassio_discovery['serial'],
CONF_BRIDGEID: self._hassio_discovery[CONF_SERIAL],
CONF_API_KEY: self._hassio_discovery[CONF_API_KEY]
}