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-12-19 17:28:03 +00:00
|
|
|
from urllib.parse import urlparse
|
2019-04-03 02:23:33 +00:00
|
|
|
|
2019-03-24 18:27:32 +00:00
|
|
|
import async_timeout
|
2019-12-05 05:17:18 +00:00
|
|
|
from pydeconz.errors import RequestError, ResponseError
|
2020-01-03 10:50:53 +00:00
|
|
|
from pydeconz.utils import (
|
|
|
|
async_discovery,
|
|
|
|
async_get_api_key,
|
|
|
|
async_get_bridge_id,
|
|
|
|
normalize_bridge_id,
|
|
|
|
)
|
2019-12-05 05:17:18 +00:00
|
|
|
import voluptuous as vol
|
2019-05-27 04:56:00 +00:00
|
|
|
|
2018-09-17 08:12:46 +00:00
|
|
|
from homeassistant import config_entries
|
2019-12-19 17:28:03 +00:00
|
|
|
from homeassistant.components import ssdp
|
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-08-19 21:42:21 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_ALLOW_CLIP_SENSOR,
|
|
|
|
CONF_ALLOW_DECONZ_GROUPS,
|
|
|
|
CONF_BRIDGEID,
|
2019-09-05 23:38:00 +00:00
|
|
|
DEFAULT_ALLOW_CLIP_SENSOR,
|
|
|
|
DEFAULT_ALLOW_DECONZ_GROUPS,
|
2019-08-19 21:42:21 +00:00
|
|
|
DEFAULT_PORT,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
2018-04-18 14:27:44 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DECONZ_MANUFACTURERURL = "http://www.dresden-elektronik.de"
|
|
|
|
CONF_SERIAL = "serial"
|
2019-04-15 04:50:01 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-09-01 11:15:48 +00:00
|
|
|
class DeconzFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
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
|
|
|
|
|
2019-08-19 21:42:21 +00:00
|
|
|
@staticmethod
|
|
|
|
@callback
|
|
|
|
def async_get_options_flow(config_entry):
|
|
|
|
"""Get the options flow for this handler."""
|
|
|
|
return DeconzOptionsFlowHandler(config_entry)
|
|
|
|
|
2018-04-18 14:27:44 +00:00
|
|
|
def __init__(self):
|
|
|
|
"""Initialize the deCONZ config flow."""
|
2020-01-03 10:50:53 +00:00
|
|
|
self.bridge_id = None
|
2018-04-18 14:27:44 +00:00
|
|
|
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]:
|
2020-01-03 10:50:53 +00:00
|
|
|
self.bridge_id = bridge[CONF_BRIDGEID]
|
|
|
|
self.deconz_config = {
|
|
|
|
CONF_HOST: bridge[CONF_HOST],
|
|
|
|
CONF_PORT: bridge[CONF_PORT],
|
|
|
|
}
|
2018-04-18 14:27:44 +00:00
|
|
|
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)
|
|
|
|
|
2019-09-25 19:50:14 +00:00
|
|
|
except (asyncio.TimeoutError, ResponseError):
|
2019-03-24 18:27:32 +00:00
|
|
|
self.bridges = []
|
2018-04-18 14:27:44 +00:00
|
|
|
|
|
|
|
if len(self.bridges) == 1:
|
2020-01-03 10:50:53 +00:00
|
|
|
return await self.async_step_user(self.bridges[0])
|
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(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="init",
|
|
|
|
data_schema=vol.Schema({vol.Required(CONF_HOST): vol.In(hosts)}),
|
2018-04-18 14:27:44 +00:00
|
|
|
)
|
|
|
|
|
2018-11-06 09:34:24 +00:00
|
|
|
return self.async_show_form(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="init",
|
|
|
|
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):
|
2019-07-31 19:25:30 +00:00
|
|
|
api_key = await async_get_api_key(session, **self.deconz_config)
|
2019-03-24 18:27:32 +00:00
|
|
|
|
|
|
|
except (ResponseError, RequestError, asyncio.TimeoutError):
|
2019-07-31 19:25:30 +00:00
|
|
|
errors["base"] = "no_key"
|
2019-03-24 18:27:32 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_show_form(step_id="link", errors=errors)
|
2018-04-18 14:27:44 +00:00
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
async def _create_entry(self):
|
|
|
|
"""Create entry for gateway."""
|
2020-01-03 10:50:53 +00:00
|
|
|
if not self.bridge_id:
|
2019-04-05 00:48:24 +00:00
|
|
|
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):
|
2020-01-03 10:50:53 +00:00
|
|
|
self.bridge_id = await async_get_bridge_id(
|
2019-07-31 19:25:30 +00:00
|
|
|
session, **self.deconz_config
|
|
|
|
)
|
2020-01-03 10:50:53 +00:00
|
|
|
await self.async_set_unique_id(self.bridge_id)
|
|
|
|
self._abort_if_unique_id_configured()
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
except asyncio.TimeoutError:
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_abort(reason="no_bridges")
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2020-01-03 10:50:53 +00:00
|
|
|
return self.async_create_entry(title=self.bridge_id, data=self.deconz_config)
|
2018-05-29 14:09:53 +00:00
|
|
|
|
2019-12-20 09:29:18 +00:00
|
|
|
def _update_entry(self, entry, host, port, api_key=None):
|
2019-04-15 04:50:01 +00:00
|
|
|
"""Update existing entry."""
|
2019-12-22 21:24:18 +00:00
|
|
|
if (
|
|
|
|
entry.data[CONF_HOST] == host
|
|
|
|
and entry.data[CONF_PORT] == port
|
|
|
|
and (api_key is None or entry.data[CONF_API_KEY] == api_key)
|
|
|
|
):
|
2019-08-31 13:56:43 +00:00
|
|
|
return self.async_abort(reason="already_configured")
|
|
|
|
|
2019-04-15 04:50:01 +00:00
|
|
|
entry.data[CONF_HOST] = host
|
2019-12-20 09:29:18 +00:00
|
|
|
entry.data[CONF_PORT] = port
|
|
|
|
|
|
|
|
if api_key is not None:
|
|
|
|
entry.data[CONF_API_KEY] = api_key
|
|
|
|
|
2019-04-15 04:50:01 +00:00
|
|
|
self.hass.config_entries.async_update_entry(entry)
|
2019-08-31 13:56:43 +00:00
|
|
|
return self.async_abort(reason="updated_instance")
|
2019-04-15 04:50:01 +00:00
|
|
|
|
2019-06-03 16:26:01 +00:00
|
|
|
async def async_step_ssdp(self, discovery_info):
|
|
|
|
"""Handle a discovered deCONZ bridge."""
|
2019-12-19 17:28:03 +00:00
|
|
|
if discovery_info[ssdp.ATTR_UPNP_MANUFACTURER_URL] != DECONZ_MANUFACTURERURL:
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_abort(reason="not_deconz_bridge")
|
2018-04-18 14:27:44 +00:00
|
|
|
|
2020-01-03 10:50:53 +00:00
|
|
|
self.bridge_id = normalize_bridge_id(discovery_info[ssdp.ATTR_UPNP_SERIAL])
|
2019-12-19 17:28:03 +00:00
|
|
|
parsed_url = urlparse(discovery_info[ssdp.ATTR_SSDP_LOCATION])
|
|
|
|
|
2019-09-25 16:56:31 +00:00
|
|
|
for entry in self.hass.config_entries.async_entries(DOMAIN):
|
2020-01-03 10:50:53 +00:00
|
|
|
if self.bridge_id == entry.unique_id:
|
2019-12-22 21:24:18 +00:00
|
|
|
if entry.source == "hassio":
|
|
|
|
return self.async_abort(reason="already_configured")
|
2019-12-20 09:29:18 +00:00
|
|
|
return self._update_entry(entry, parsed_url.hostname, parsed_url.port)
|
2019-04-15 04:50:01 +00:00
|
|
|
|
2020-01-03 10:50:53 +00:00
|
|
|
await self.async_set_unique_id(self.bridge_id)
|
2019-10-07 15:17:39 +00:00
|
|
|
# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
|
2019-12-19 17:28:03 +00:00
|
|
|
self.context["title_placeholders"] = {"host": parsed_url.hostname}
|
2019-06-08 06:43:18 +00:00
|
|
|
|
2019-09-14 20:53:59 +00:00
|
|
|
self.deconz_config = {
|
2019-12-19 17:28:03 +00:00
|
|
|
CONF_HOST: parsed_url.hostname,
|
|
|
|
CONF_PORT: parsed_url.port,
|
2019-04-05 00:48:24 +00:00
|
|
|
}
|
|
|
|
|
2019-09-14 20:53:59 +00:00
|
|
|
return await self.async_step_link()
|
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.
|
|
|
|
"""
|
2020-01-03 10:50:53 +00:00
|
|
|
self.bridge_id = normalize_bridge_id(user_input[CONF_SERIAL])
|
|
|
|
gateway = self.hass.data.get(DOMAIN, {}).get(self.bridge_id)
|
2019-04-15 04:50:01 +00:00
|
|
|
|
2020-01-03 10:50:53 +00:00
|
|
|
if gateway:
|
2019-12-20 09:29:18 +00:00
|
|
|
return self._update_entry(
|
2020-01-03 10:50:53 +00:00
|
|
|
gateway.config_entry,
|
2019-12-20 09:29:18 +00:00
|
|
|
user_input[CONF_HOST],
|
|
|
|
user_input[CONF_PORT],
|
|
|
|
user_input[CONF_API_KEY],
|
|
|
|
)
|
2019-04-03 02:23:33 +00:00
|
|
|
|
2020-01-03 10:50:53 +00:00
|
|
|
await self.async_set_unique_id(self.bridge_id)
|
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-07-31 19:25:30 +00:00
|
|
|
CONF_API_KEY: self._hassio_discovery[CONF_API_KEY],
|
2019-04-05 00:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return await self._create_entry()
|
2019-04-03 02:23:33 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="hassio_confirm",
|
|
|
|
description_placeholders={"addon": self._hassio_discovery["addon"]},
|
2019-04-03 02:23:33 +00:00
|
|
|
)
|
2019-08-19 21:42:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeconzOptionsFlowHandler(config_entries.OptionsFlow):
|
|
|
|
"""Handle deCONZ options."""
|
|
|
|
|
|
|
|
def __init__(self, config_entry):
|
|
|
|
"""Initialize deCONZ options flow."""
|
|
|
|
self.config_entry = config_entry
|
2019-09-05 23:38:00 +00:00
|
|
|
self.options = dict(config_entry.options)
|
2019-08-19 21:42:21 +00:00
|
|
|
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
"""Manage the deCONZ options."""
|
|
|
|
return await self.async_step_deconz_devices()
|
|
|
|
|
|
|
|
async def async_step_deconz_devices(self, user_input=None):
|
|
|
|
"""Manage the deconz devices options."""
|
|
|
|
if user_input is not None:
|
|
|
|
self.options[CONF_ALLOW_CLIP_SENSOR] = user_input[CONF_ALLOW_CLIP_SENSOR]
|
|
|
|
self.options[CONF_ALLOW_DECONZ_GROUPS] = user_input[
|
|
|
|
CONF_ALLOW_DECONZ_GROUPS
|
|
|
|
]
|
|
|
|
return self.async_create_entry(title="", data=self.options)
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="deconz_devices",
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(
|
|
|
|
CONF_ALLOW_CLIP_SENSOR,
|
2019-09-05 23:38:00 +00:00
|
|
|
default=self.config_entry.options.get(
|
|
|
|
CONF_ALLOW_CLIP_SENSOR, DEFAULT_ALLOW_CLIP_SENSOR
|
|
|
|
),
|
2019-08-19 21:42:21 +00:00
|
|
|
): bool,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_ALLOW_DECONZ_GROUPS,
|
2019-09-05 23:38:00 +00:00
|
|
|
default=self.config_entry.options.get(
|
|
|
|
CONF_ALLOW_DECONZ_GROUPS, DEFAULT_ALLOW_DECONZ_GROUPS
|
|
|
|
),
|
2019-08-19 21:42:21 +00:00
|
|
|
): bool,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|