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,
|
|
|
|
)
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2020-08-01 16:18:40 +00:00
|
|
|
from .const import CONF_BOND_ID
|
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__)
|
|
|
|
|
2020-08-01 16:18:40 +00:00
|
|
|
DATA_SCHEMA_USER = vol.Schema(
|
2020-07-06 01:17:53 +00:00
|
|
|
{vol.Required(CONF_HOST): str, vol.Required(CONF_ACCESS_TOKEN): str}
|
|
|
|
)
|
2020-08-01 16:18:40 +00:00
|
|
|
DATA_SCHEMA_DISCOVERY = vol.Schema({vol.Required(CONF_ACCESS_TOKEN): str})
|
2020-07-06 01:17:53 +00:00
|
|
|
|
|
|
|
|
2021-02-20 07:06:43 +00:00
|
|
|
async def _validate_input(data: Dict[str, Any]) -> Tuple[str, Optional[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
|
|
|
|
2020-08-01 16:18:40 +00:00
|
|
|
_discovered: dict = None
|
|
|
|
|
|
|
|
async def async_step_zeroconf(
|
|
|
|
self, discovery_info: Optional[Dict[str, Any]] = None
|
|
|
|
) -> Dict[str, Any]:
|
|
|
|
"""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})
|
|
|
|
|
|
|
|
self._discovered = {
|
|
|
|
CONF_HOST: host,
|
|
|
|
CONF_BOND_ID: bond_id,
|
|
|
|
}
|
|
|
|
self.context.update({"title_placeholders": self._discovered})
|
|
|
|
|
|
|
|
return await self.async_step_confirm()
|
|
|
|
|
|
|
|
async def async_step_confirm(
|
|
|
|
self, user_input: Dict[str, Any] = None
|
|
|
|
) -> 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:
|
2020-08-01 16:18:40 +00:00
|
|
|
data = user_input.copy()
|
|
|
|
data[CONF_HOST] = self._discovered[CONF_HOST]
|
2020-07-06 01:17:53 +00:00
|
|
|
try:
|
2020-08-01 16:18:40 +00:00
|
|
|
return await self._try_create_entry(data)
|
|
|
|
except InputValidationError as error:
|
|
|
|
errors["base"] = error.base
|
2020-07-06 01:17:53 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
2020-08-01 16:18:40 +00:00
|
|
|
step_id="confirm",
|
|
|
|
data_schema=DATA_SCHEMA_DISCOVERY,
|
|
|
|
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(
|
|
|
|
self, user_input: Dict[str, Any] = None
|
|
|
|
) -> Dict[str, Any]:
|
|
|
|
"""Handle a flow initialized by the user."""
|
|
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
|
|
|
try:
|
|
|
|
return await self._try_create_entry(user_input)
|
|
|
|
except InputValidationError as error:
|
|
|
|
errors["base"] = error.base
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user", data_schema=DATA_SCHEMA_USER, errors=errors
|
|
|
|
)
|
|
|
|
|
|
|
|
async def _try_create_entry(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
2021-02-20 07:06:43 +00:00
|
|
|
bond_id, name = await _validate_input(data)
|
2020-08-01 16:18:40 +00:00
|
|
|
await self.async_set_unique_id(bond_id)
|
|
|
|
self._abort_if_unique_id_configured()
|
2021-02-20 07:06:43 +00:00
|
|
|
hub_name = name or bond_id
|
|
|
|
return self.async_create_entry(title=hub_name, data=data)
|
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
|