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 authored and Paulus Schoutsen committed 2019-04-14 21:50:01 -07:00
1 parent 96f689a70f
commit 23cb579f9f
4 files changed
+217 -116

No files matched your search

+33 -12
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]
}
@@ -101,8 +101,22 @@ class DeconzGateway:
self.api.start()
self.config_entry.add_update_listener(self.async_new_address_callback)
return True
@staticmethod
async def async_new_address_callback(hass, entry):
"""Handle signals of gateway getting new address.
This is a static method because a class method (bound method),
can not be used with weak references.
"""
gateway = hass.data[DOMAIN][entry.data[CONF_BRIDGEID]]
gateway.api.close()
gateway.api.host = entry.data[CONF_HOST]
gateway.api.start()
@property
def event_reachable(self):
"""Gateway specific event to signal a change in connection status."""
@@ -35,6 +35,7 @@
"abort": {
"already_configured": "Bridge is already configured",
"no_bridges": "No deCONZ bridges discovered",
"updated_instance": "Updated deCONZ instance with new host address",
"one_instance_only": "Component only supports one deCONZ instance"
}
}