2018-04-18 14:27:44 +00:00
|
|
|
"""Config flow to configure deCONZ component."""
|
2019-03-24 18:27:32 +00:00
|
|
|
import asyncio
|
2019-04-03 02:23:33 +00:00
|
|
|
|
2019-03-24 18:27:32 +00:00
|
|
|
import async_timeout
|
2018-04-18 14:27:44 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-05-27 04:56:00 +00:00
|
|
|
from pydeconz.errors import ResponseError, RequestError
|
|
|
|
from pydeconz.utils import (
|
|
|
|
async_discovery, async_get_api_key, async_get_bridgeid)
|
|
|
|
|
2018-09-17 08:12:46 +00:00
|
|
|
from homeassistant import config_entries
|
2018-04-18 14:27:44 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT
|
2019-04-03 02:23:33 +00:00
|
|
|
from homeassistant.core import callback
|
2018-04-18 14:27:44 +00:00
|
|
|
from homeassistant.helpers import aiohttp_client
|
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
from .const import CONF_BRIDGEID, DEFAULT_PORT, DOMAIN
|
2018-04-18 14:27:44 +00:00
|
|
|
|
2019-06-03 16:26:01 +00:00
|
|
|
DECONZ_MANUFACTURERURL = 'http://www.dresden-elektronik.de'
|
2019-04-15 04:50:01 +00:00
|
|
|
CONF_SERIAL = 'serial'
|
2019-06-08 06:43:18 +00:00
|
|
|
ATTR_UUID = 'udn'
|
2019-04-15 04:50:01 +00:00
|
|
|
|
2018-04-18 14:27:44 +00:00
|
|
|
|
|
|
|
@callback
|
2019-04-15 04:50:01 +00:00
|
|
|
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)}
|
2018-04-18 14:27:44 +00:00
|
|
|
|
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
@callback
|
|
|
|
def get_master_gateway(hass):
|
2019-04-15 04:50:01 +00:00
|
|
|
"""Return the gateway which is marked as master."""
|
2019-04-05 00:48:24 +00:00
|
|
|
for gateway in hass.data[DOMAIN].values():
|
|
|
|
if gateway.master:
|
|
|
|
return gateway
|
|
|
|
|
|
|
|
|
2018-04-18 14:27:44 +00:00
|
|
|
@config_entries.HANDLERS.register(DOMAIN)
|
2018-09-17 08:12:46 +00:00
|
|
|
class DeconzFlowHandler(config_entries.ConfigFlow):
|
2018-04-18 14:27:44 +00:00
|
|
|
"""Handle a deCONZ config flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
2018-09-17 08:12:46 +00:00
|
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_PUSH
|
2018-04-18 14:27:44 +00:00
|
|
|
|
2019-04-03 02:23:33 +00:00
|
|
|
_hassio_discovery = None
|
|
|
|
|
2018-04-18 14:27:44 +00:00
|
|
|
def __init__(self):
|
|
|
|
"""Initialize the deCONZ config flow."""
|
|
|
|
self.bridges = []
|
|
|
|
self.deconz_config = {}
|
|
|
|
|
2019-04-15 04:50:01 +00:00
|
|
|
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)
|
|
|
|
|
2018-08-09 11:24:14 +00:00
|
|
|
async def async_step_user(self, user_input=None):
|
2018-05-29 14:09:53 +00:00
|
|
|
"""Handle a deCONZ config flow start.
|
|
|
|
|
|
|
|
If only one bridge is found go to link step.
|
|
|
|
If more than one bridge is found let user choose bridge to link.
|
2019-03-24 18:27:32 +00:00
|
|
|
If no bridge is found allow user to manually input configuration.
|
2018-05-29 14:09:53 +00:00
|
|
|
"""
|
2018-04-18 14:27:44 +00:00
|
|
|
if user_input is not None:
|
|
|
|
for bridge in self.bridges:
|
|
|
|
if bridge[CONF_HOST] == user_input[CONF_HOST]:
|
|
|
|
self.deconz_config = bridge
|
|
|
|
return await self.async_step_link()
|
2019-03-24 18:27:32 +00:00
|
|
|
|
2018-11-06 09:34:24 +00:00
|
|
|
self.deconz_config = user_input
|
|
|
|
return await self.async_step_link()
|
2018-04-18 14:27:44 +00:00
|
|
|
|
|
|
|
session = aiohttp_client.async_get_clientsession(self.hass)
|
2019-03-24 18:27:32 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
with async_timeout.timeout(10):
|
|
|
|
self.bridges = await async_discovery(session)
|
|
|
|
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
self.bridges = []
|
2018-04-18 14:27:44 +00:00
|
|
|
|
|
|
|
if len(self.bridges) == 1:
|
|
|
|
self.deconz_config = self.bridges[0]
|
|
|
|
return await self.async_step_link()
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2018-07-23 08:16:05 +00:00
|
|
|
if len(self.bridges) > 1:
|
2018-04-18 14:27:44 +00:00
|
|
|
hosts = []
|
2019-03-24 18:27:32 +00:00
|
|
|
|
2018-04-18 14:27:44 +00:00
|
|
|
for bridge in self.bridges:
|
|
|
|
hosts.append(bridge[CONF_HOST])
|
2019-03-24 18:27:32 +00:00
|
|
|
|
2018-04-18 14:27:44 +00:00
|
|
|
return self.async_show_form(
|
2018-11-06 09:34:24 +00:00
|
|
|
step_id='init',
|
2018-04-18 14:27:44 +00:00
|
|
|
data_schema=vol.Schema({
|
|
|
|
vol.Required(CONF_HOST): vol.In(hosts)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2018-11-06 09:34:24 +00:00
|
|
|
return self.async_show_form(
|
2019-03-24 18:27:32 +00:00
|
|
|
step_id='init',
|
2018-11-06 09:34:24 +00:00
|
|
|
data_schema=vol.Schema({
|
|
|
|
vol.Required(CONF_HOST): str,
|
|
|
|
vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
|
|
|
|
}),
|
2018-04-18 14:27:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def async_step_link(self, user_input=None):
|
|
|
|
"""Attempt to link with the deCONZ bridge."""
|
|
|
|
errors = {}
|
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
session = aiohttp_client.async_get_clientsession(self.hass)
|
2019-03-24 18:27:32 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
with async_timeout.timeout(10):
|
|
|
|
api_key = await async_get_api_key(
|
|
|
|
session, **self.deconz_config)
|
|
|
|
|
|
|
|
except (ResponseError, RequestError, asyncio.TimeoutError):
|
|
|
|
errors['base'] = 'no_key'
|
|
|
|
|
|
|
|
else:
|
2018-04-18 14:27:44 +00:00
|
|
|
self.deconz_config[CONF_API_KEY] = api_key
|
2019-04-05 00:48:24 +00:00
|
|
|
return await self._create_entry()
|
2018-04-18 14:27:44 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id='link',
|
|
|
|
errors=errors,
|
|
|
|
)
|
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
async def _create_entry(self):
|
|
|
|
"""Create entry for gateway."""
|
|
|
|
if CONF_BRIDGEID not in self.deconz_config:
|
|
|
|
session = aiohttp_client.async_get_clientsession(self.hass)
|
2018-05-29 14:09:53 +00:00
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
try:
|
|
|
|
with async_timeout.timeout(10):
|
|
|
|
self.deconz_config[CONF_BRIDGEID] = \
|
|
|
|
await async_get_bridgeid(
|
|
|
|
session, **self.deconz_config)
|
|
|
|
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
return self.async_abort(reason='no_bridges')
|
|
|
|
|
|
|
|
return self.async_create_entry(
|
|
|
|
title='deCONZ-' + self.deconz_config[CONF_BRIDGEID],
|
|
|
|
data=self.deconz_config
|
2018-05-29 14:09:53 +00:00
|
|
|
)
|
|
|
|
|
2019-04-15 04:50:01 +00:00
|
|
|
async def _update_entry(self, entry, host):
|
|
|
|
"""Update existing entry."""
|
|
|
|
entry.data[CONF_HOST] = host
|
|
|
|
self.hass.config_entries.async_update_entry(entry)
|
|
|
|
|
2019-06-03 16:26:01 +00:00
|
|
|
async def async_step_ssdp(self, discovery_info):
|
|
|
|
"""Handle a discovered deCONZ bridge."""
|
2019-06-10 16:11:07 +00:00
|
|
|
from homeassistant.components.ssdp import (
|
|
|
|
ATTR_MANUFACTURERURL, ATTR_SERIAL)
|
|
|
|
|
2019-06-03 16:26:01 +00:00
|
|
|
if discovery_info[ATTR_MANUFACTURERURL] != DECONZ_MANUFACTURERURL:
|
|
|
|
return self.async_abort(reason='not_deconz_bridge')
|
2018-04-18 14:27:44 +00:00
|
|
|
|
2019-06-08 06:43:18 +00:00
|
|
|
uuid = discovery_info[ATTR_UUID].replace('uuid:', '')
|
|
|
|
gateways = {
|
|
|
|
gateway.api.config.uuid: gateway
|
|
|
|
for gateway in self.hass.data.get(DOMAIN, {}).values()
|
|
|
|
}
|
2019-04-15 04:50:01 +00:00
|
|
|
|
2019-06-08 06:43:18 +00:00
|
|
|
if uuid in gateways:
|
|
|
|
entry = gateways[uuid].config_entry
|
2019-04-15 04:50:01 +00:00
|
|
|
await self._update_entry(entry, discovery_info[CONF_HOST])
|
|
|
|
return self.async_abort(reason='updated_instance')
|
|
|
|
|
2019-06-08 06:43:18 +00:00
|
|
|
bridgeid = discovery_info[ATTR_SERIAL]
|
|
|
|
if any(bridgeid == flow['context'][CONF_BRIDGEID]
|
2019-06-03 16:26:01 +00:00
|
|
|
for flow in self._async_in_progress()):
|
|
|
|
return self.async_abort(reason='already_in_progress')
|
|
|
|
|
2019-06-08 06:43:18 +00:00
|
|
|
# pylint: disable=unsupported-assignment-operation
|
|
|
|
self.context[CONF_BRIDGEID] = bridgeid
|
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
deconz_config = {
|
|
|
|
CONF_HOST: discovery_info[CONF_HOST],
|
|
|
|
CONF_PORT: discovery_info[CONF_PORT],
|
|
|
|
}
|
|
|
|
|
2018-04-18 14:27:44 +00:00
|
|
|
return await self.async_step_import(deconz_config)
|
|
|
|
|
|
|
|
async def async_step_import(self, import_config):
|
|
|
|
"""Import a deCONZ bridge as a config entry.
|
|
|
|
|
|
|
|
This flow is triggered by `async_setup` for configured bridges.
|
|
|
|
This flow is also triggered by `async_step_discovery`.
|
|
|
|
|
|
|
|
This will execute for any bridge that does not have a
|
|
|
|
config entry yet (based on host).
|
|
|
|
|
|
|
|
If an API key is provided, we will create an entry.
|
|
|
|
Otherwise we will delegate to `link` step which
|
|
|
|
will ask user to link the bridge.
|
|
|
|
"""
|
2018-05-29 14:09:53 +00:00
|
|
|
self.deconz_config = import_config
|
|
|
|
if CONF_API_KEY not in import_config:
|
2018-04-18 14:27:44 +00:00
|
|
|
return await self.async_step_link()
|
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
return await self._create_entry()
|
2019-04-03 02:23:33 +00:00
|
|
|
|
|
|
|
async def async_step_hassio(self, user_input=None):
|
|
|
|
"""Prepare configuration for a Hass.io deCONZ bridge.
|
|
|
|
|
|
|
|
This flow is triggered by the discovery component.
|
|
|
|
"""
|
2019-04-15 04:50:01 +00:00
|
|
|
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')
|
2019-04-03 02:23:33 +00:00
|
|
|
|
|
|
|
self._hassio_discovery = user_input
|
|
|
|
|
|
|
|
return await self.async_step_hassio_confirm()
|
|
|
|
|
|
|
|
async def async_step_hassio_confirm(self, user_input=None):
|
|
|
|
"""Confirm a Hass.io discovery."""
|
|
|
|
if user_input is not None:
|
2019-04-05 00:48:24 +00:00
|
|
|
self.deconz_config = {
|
|
|
|
CONF_HOST: self._hassio_discovery[CONF_HOST],
|
|
|
|
CONF_PORT: self._hassio_discovery[CONF_PORT],
|
2019-04-15 04:50:01 +00:00
|
|
|
CONF_BRIDGEID: self._hassio_discovery[CONF_SERIAL],
|
2019-04-05 00:48:24 +00:00
|
|
|
CONF_API_KEY: self._hassio_discovery[CONF_API_KEY]
|
|
|
|
}
|
|
|
|
|
|
|
|
return await self._create_entry()
|
2019-04-03 02:23:33 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id='hassio_confirm',
|
|
|
|
description_placeholders={
|
|
|
|
'addon': self._hassio_discovery['addon']
|
2019-04-05 00:48:24 +00:00
|
|
|
}
|
2019-04-03 02:23:33 +00:00
|
|
|
)
|