2020-07-06 01:17:53 +00:00
|
|
|
"""Config flow for Bond integration."""
|
|
|
|
import logging
|
2021-02-20 07:06:43 +00:00
|
|
|
from typing import Any, Dict, Optional, Tuple
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2020-07-23 01:22:25 +00:00
|
|
|
from aiohttp import ClientConnectionError, ClientResponseError
|
|
|
|
from bond_api import Bond
|
2020-07-06 01:17:53 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-07-23 01:22:25 +00:00
|
|
|
from homeassistant import config_entries, exceptions
|
2020-09-15 17:01:07 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_ACCESS_TOKEN,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_NAME,
|
|
|
|
HTTP_UNAUTHORIZED,
|
|
|
|
)
|
2021-03-01 02:16:30 +00:00
|
|
|
from homeassistant.helpers.typing import DiscoveryInfoType
|
2020-07-06 01:17:53 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN # pylint:disable=unused-import
|
2021-02-20 07:06:43 +00:00
|
|
|
from .utils import BondHub
|
2020-07-06 01:17:53 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-02-23 21:42:56 +00:00
|
|
|
|
|
|
|
USER_SCHEMA = vol.Schema(
|
2020-07-06 01:17:53 +00:00
|
|
|
{vol.Required(CONF_HOST): str, vol.Required(CONF_ACCESS_TOKEN): str}
|
|
|
|
)
|
2021-02-23 21:42:56 +00:00
|
|
|
DISCOVERY_SCHEMA = vol.Schema({vol.Required(CONF_ACCESS_TOKEN): str})
|
|
|
|
TOKEN_SCHEMA = vol.Schema({})
|
2020-07-06 01:17:53 +00:00
|
|
|
|
|
|
|
|
2021-03-01 02:16:30 +00:00
|
|
|
async def _validate_input(data: Dict[str, Any]) -> Tuple[str, str]:
|
2020-07-06 01:17:53 +00:00
|
|
|
"""Validate the user input allows us to connect."""
|
|
|
|
|
2021-02-20 07:06:43 +00:00
|
|
|
bond = Bond(data[CONF_HOST], data[CONF_ACCESS_TOKEN])
|
2020-07-23 01:22:25 +00:00
|
|
|
try:
|
2021-02-20 07:06:43 +00:00
|
|
|
hub = BondHub(bond)
|
|
|
|
await hub.setup(max_devices=1)
|
2020-08-28 11:50:32 +00:00
|
|
|
except ClientConnectionError as error:
|
|
|
|
raise InputValidationError("cannot_connect") from error
|
2020-07-23 01:22:25 +00:00
|
|
|
except ClientResponseError as error:
|
2020-09-15 17:01:07 +00:00
|
|
|
if error.status == HTTP_UNAUTHORIZED:
|
2020-08-28 11:50:32 +00:00
|
|
|
raise InputValidationError("invalid_auth") from error
|
|
|
|
raise InputValidationError("unknown") from error
|
|
|
|
except Exception as error:
|
2020-08-01 16:18:40 +00:00
|
|
|
_LOGGER.exception("Unexpected exception")
|
2020-08-28 11:50:32 +00:00
|
|
|
raise InputValidationError("unknown") from error
|
2020-07-23 01:22:25 +00:00
|
|
|
|
2020-07-30 23:00:58 +00:00
|
|
|
# Return unique ID from the hub to be stored in the config entry.
|
2021-02-20 07:06:43 +00:00
|
|
|
if not hub.bond_id:
|
2020-08-08 02:22:13 +00:00
|
|
|
raise InputValidationError("old_firmware")
|
|
|
|
|
2021-02-20 07:06:43 +00:00
|
|
|
return hub.bond_id, hub.name
|
2020-07-06 01:17:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a config flow for Bond."""
|
|
|
|
|
|
|
|
VERSION = 1
|
2021-02-09 08:43:38 +00:00
|
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_PUSH
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2021-03-01 02:16:30 +00:00
|
|
|
def __init__(self) -> None:
|
2021-02-23 21:42:56 +00:00
|
|
|
"""Initialize config flow."""
|
2021-03-01 02:16:30 +00:00
|
|
|
self._discovered: Dict[str, str] = {}
|
2021-02-23 21:42:56 +00:00
|
|
|
|
2021-03-01 02:16:30 +00:00
|
|
|
async def _async_try_automatic_configure(self) -> None:
|
2021-02-23 21:42:56 +00:00
|
|
|
"""Try to auto configure the device.
|
|
|
|
|
|
|
|
Failure is acceptable here since the device may have been
|
|
|
|
online longer then the allowed setup period, and we will
|
|
|
|
instead ask them to manually enter the token.
|
|
|
|
"""
|
|
|
|
bond = Bond(self._discovered[CONF_HOST], "")
|
|
|
|
try:
|
|
|
|
response = await bond.token()
|
|
|
|
except ClientConnectionError:
|
|
|
|
return
|
|
|
|
|
|
|
|
token = response.get("token")
|
|
|
|
if token is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
self._discovered[CONF_ACCESS_TOKEN] = token
|
|
|
|
_, hub_name = await _validate_input(self._discovered)
|
|
|
|
self._discovered[CONF_NAME] = hub_name
|
2020-08-01 16:18:40 +00:00
|
|
|
|
2021-03-01 02:16:30 +00:00
|
|
|
async def async_step_zeroconf(self, discovery_info: DiscoveryInfoType) -> Dict[str, Any]: # type: ignore
|
2020-08-01 16:18:40 +00:00
|
|
|
"""Handle a flow initialized by zeroconf discovery."""
|
|
|
|
name: str = discovery_info[CONF_NAME]
|
|
|
|
host: str = discovery_info[CONF_HOST]
|
|
|
|
bond_id = name.partition(".")[0]
|
|
|
|
await self.async_set_unique_id(bond_id)
|
|
|
|
self._abort_if_unique_id_configured({CONF_HOST: host})
|
|
|
|
|
2021-02-23 21:42:56 +00:00
|
|
|
self._discovered = {CONF_HOST: host, CONF_NAME: bond_id}
|
|
|
|
await self._async_try_automatic_configure()
|
|
|
|
|
|
|
|
self.context.update(
|
|
|
|
{
|
|
|
|
"title_placeholders": {
|
|
|
|
CONF_HOST: self._discovered[CONF_HOST],
|
|
|
|
CONF_NAME: self._discovered[CONF_NAME],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2020-08-01 16:18:40 +00:00
|
|
|
|
|
|
|
return await self.async_step_confirm()
|
|
|
|
|
|
|
|
async def async_step_confirm(
|
2021-03-01 02:16:30 +00:00
|
|
|
self, user_input: Optional[Dict[str, Any]] = None
|
2020-08-01 16:18:40 +00:00
|
|
|
) -> Dict[str, Any]:
|
|
|
|
"""Handle confirmation flow for discovered bond hub."""
|
2020-07-06 01:17:53 +00:00
|
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
2021-02-23 21:42:56 +00:00
|
|
|
if CONF_ACCESS_TOKEN in self._discovered:
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=self._discovered[CONF_NAME],
|
|
|
|
data={
|
|
|
|
CONF_ACCESS_TOKEN: self._discovered[CONF_ACCESS_TOKEN],
|
|
|
|
CONF_HOST: self._discovered[CONF_HOST],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
data = {
|
|
|
|
CONF_ACCESS_TOKEN: user_input[CONF_ACCESS_TOKEN],
|
|
|
|
CONF_HOST: self._discovered[CONF_HOST],
|
|
|
|
}
|
2020-07-06 01:17:53 +00:00
|
|
|
try:
|
2021-02-23 21:42:56 +00:00
|
|
|
_, hub_name = await _validate_input(data)
|
2020-08-01 16:18:40 +00:00
|
|
|
except InputValidationError as error:
|
|
|
|
errors["base"] = error.base
|
2021-02-23 21:42:56 +00:00
|
|
|
else:
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=hub_name,
|
|
|
|
data=data,
|
|
|
|
)
|
|
|
|
|
|
|
|
if CONF_ACCESS_TOKEN in self._discovered:
|
|
|
|
data_schema = TOKEN_SCHEMA
|
|
|
|
else:
|
|
|
|
data_schema = DISCOVERY_SCHEMA
|
2020-07-06 01:17:53 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
2020-08-01 16:18:40 +00:00
|
|
|
step_id="confirm",
|
2021-02-23 21:42:56 +00:00
|
|
|
data_schema=data_schema,
|
2020-08-01 16:18:40 +00:00
|
|
|
errors=errors,
|
|
|
|
description_placeholders=self._discovered,
|
2020-07-06 01:17:53 +00:00
|
|
|
)
|
|
|
|
|
2020-08-01 16:18:40 +00:00
|
|
|
async def async_step_user(
|
2021-03-01 02:16:30 +00:00
|
|
|
self, user_input: Optional[Dict[str, Any]] = None
|
2020-08-01 16:18:40 +00:00
|
|
|
) -> Dict[str, Any]:
|
|
|
|
"""Handle a flow initialized by the user."""
|
|
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
|
|
|
try:
|
2021-02-23 21:42:56 +00:00
|
|
|
bond_id, hub_name = await _validate_input(user_input)
|
2020-08-01 16:18:40 +00:00
|
|
|
except InputValidationError as error:
|
|
|
|
errors["base"] = error.base
|
2021-02-23 21:42:56 +00:00
|
|
|
else:
|
|
|
|
await self.async_set_unique_id(bond_id)
|
|
|
|
self._abort_if_unique_id_configured()
|
|
|
|
return self.async_create_entry(title=hub_name, data=user_input)
|
2020-08-01 16:18:40 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
2021-02-23 21:42:56 +00:00
|
|
|
step_id="user", data_schema=USER_SCHEMA, errors=errors
|
2020-08-01 16:18:40 +00:00
|
|
|
)
|
|
|
|
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2020-08-01 16:18:40 +00:00
|
|
|
class InputValidationError(exceptions.HomeAssistantError):
|
|
|
|
"""Error to indicate we cannot proceed due to invalid input."""
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2020-08-01 16:18:40 +00:00
|
|
|
def __init__(self, base: str):
|
|
|
|
"""Initialize with error base."""
|
|
|
|
super().__init__()
|
|
|
|
self.base = base
|