2020-05-13 13:50:29 +00:00
|
|
|
"""Config flow to configure Blink."""
|
|
|
|
import logging
|
|
|
|
|
2020-08-05 10:21:14 +00:00
|
|
|
from blinkpy.auth import Auth, LoginError, TokenRefreshFailed
|
|
|
|
from blinkpy.blinkpy import Blink, BlinkSetupError
|
2020-05-13 13:50:29 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries, core, exceptions
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PIN,
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
CONF_USERNAME,
|
|
|
|
)
|
2020-06-10 16:38:17 +00:00
|
|
|
from homeassistant.core import callback
|
2020-05-13 13:50:29 +00:00
|
|
|
|
2021-12-13 19:03:01 +00:00
|
|
|
from .const import DEFAULT_SCAN_INTERVAL, DEVICE_ID, DOMAIN
|
|
|
|
|
2020-05-13 13:50:29 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-08-05 10:21:14 +00:00
|
|
|
def validate_input(hass: core.HomeAssistant, auth):
|
2020-05-13 13:50:29 +00:00
|
|
|
"""Validate the user input allows us to connect."""
|
2020-08-05 10:21:14 +00:00
|
|
|
try:
|
|
|
|
auth.startup()
|
2020-08-28 11:50:32 +00:00
|
|
|
except (LoginError, TokenRefreshFailed) as err:
|
|
|
|
raise InvalidAuth from err
|
2020-08-05 10:21:14 +00:00
|
|
|
if auth.check_key_required():
|
2020-05-13 13:50:29 +00:00
|
|
|
raise Require2FA
|
|
|
|
|
2020-08-05 10:21:14 +00:00
|
|
|
|
|
|
|
def _send_blink_2fa_pin(auth, pin):
|
|
|
|
"""Send 2FA pin to blink servers."""
|
|
|
|
blink = Blink()
|
|
|
|
blink.auth = auth
|
2021-01-04 11:33:34 +00:00
|
|
|
blink.setup_login_ids()
|
2020-08-05 10:21:14 +00:00
|
|
|
blink.setup_urls()
|
|
|
|
return auth.send_auth_key(blink, pin)
|
2020-05-13 13:50:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BlinkConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a Blink config flow."""
|
|
|
|
|
2021-03-23 08:24:42 +00:00
|
|
|
VERSION = 3
|
2020-05-13 13:50:29 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Initialize the blink flow."""
|
2020-08-05 10:21:14 +00:00
|
|
|
self.auth = None
|
2020-05-13 13:50:29 +00:00
|
|
|
|
2020-06-10 16:38:17 +00:00
|
|
|
@staticmethod
|
|
|
|
@callback
|
|
|
|
def async_get_options_flow(config_entry):
|
|
|
|
"""Get options flow for this handler."""
|
|
|
|
return BlinkOptionsFlowHandler(config_entry)
|
|
|
|
|
2020-05-13 13:50:29 +00:00
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Handle a flow initiated by the user."""
|
|
|
|
errors = {}
|
2020-08-05 10:21:14 +00:00
|
|
|
data = {CONF_USERNAME: "", CONF_PASSWORD: "", "device_id": DEVICE_ID}
|
2020-05-13 13:50:29 +00:00
|
|
|
if user_input is not None:
|
2020-08-05 10:21:14 +00:00
|
|
|
data[CONF_USERNAME] = user_input["username"]
|
|
|
|
data[CONF_PASSWORD] = user_input["password"]
|
2020-05-13 13:50:29 +00:00
|
|
|
|
2020-08-05 10:21:14 +00:00
|
|
|
self.auth = Auth(data, no_prompt=True)
|
|
|
|
await self.async_set_unique_id(data[CONF_USERNAME])
|
2020-05-13 13:50:29 +00:00
|
|
|
|
|
|
|
try:
|
2020-08-05 10:21:14 +00:00
|
|
|
await self.hass.async_add_executor_job(
|
|
|
|
validate_input, self.hass, self.auth
|
|
|
|
)
|
|
|
|
return self._async_finish_flow()
|
2020-05-13 13:50:29 +00:00
|
|
|
except Require2FA:
|
|
|
|
return await self.async_step_2fa()
|
|
|
|
except InvalidAuth:
|
|
|
|
errors["base"] = "invalid_auth"
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
_LOGGER.exception("Unexpected exception")
|
|
|
|
errors["base"] = "unknown"
|
|
|
|
|
|
|
|
data_schema = {
|
|
|
|
vol.Required("username"): str,
|
|
|
|
vol.Required("password"): str,
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.async_show_form(
|
2020-08-27 11:56:20 +00:00
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema(data_schema),
|
|
|
|
errors=errors,
|
2020-05-13 13:50:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def async_step_2fa(self, user_input=None):
|
|
|
|
"""Handle 2FA step."""
|
2020-08-05 10:21:14 +00:00
|
|
|
errors = {}
|
2020-05-13 13:50:29 +00:00
|
|
|
if user_input is not None:
|
|
|
|
pin = user_input.get(CONF_PIN)
|
2020-08-05 10:21:14 +00:00
|
|
|
try:
|
|
|
|
valid_token = await self.hass.async_add_executor_job(
|
|
|
|
_send_blink_2fa_pin, self.auth, pin
|
|
|
|
)
|
|
|
|
except BlinkSetupError:
|
|
|
|
errors["base"] = "cannot_connect"
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
_LOGGER.exception("Unexpected exception")
|
|
|
|
errors["base"] = "unknown"
|
|
|
|
|
|
|
|
else:
|
|
|
|
if valid_token:
|
|
|
|
return self._async_finish_flow()
|
|
|
|
errors["base"] = "invalid_access_token"
|
2020-05-13 13:50:29 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="2fa",
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{vol.Optional("pin"): vol.All(str, vol.Length(min=1))}
|
|
|
|
),
|
2020-08-05 10:21:14 +00:00
|
|
|
errors=errors,
|
2020-05-13 13:50:29 +00:00
|
|
|
)
|
|
|
|
|
2020-08-05 10:21:14 +00:00
|
|
|
async def async_step_reauth(self, entry_data):
|
|
|
|
"""Perform reauth upon migration of old entries."""
|
|
|
|
return await self.async_step_user(entry_data)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_finish_flow(self):
|
|
|
|
"""Finish with setup."""
|
|
|
|
return self.async_create_entry(title=DOMAIN, data=self.auth.login_attributes)
|
2020-05-13 13:50:29 +00:00
|
|
|
|
|
|
|
|
2020-06-10 16:38:17 +00:00
|
|
|
class BlinkOptionsFlowHandler(config_entries.OptionsFlow):
|
|
|
|
"""Handle Blink options."""
|
|
|
|
|
|
|
|
def __init__(self, config_entry):
|
|
|
|
"""Initialize Blink options flow."""
|
|
|
|
self.config_entry = config_entry
|
|
|
|
self.options = dict(config_entry.options)
|
|
|
|
self.blink = None
|
|
|
|
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
"""Manage the Blink options."""
|
|
|
|
self.blink = self.hass.data[DOMAIN][self.config_entry.entry_id]
|
|
|
|
self.options[CONF_SCAN_INTERVAL] = self.blink.refresh_rate
|
|
|
|
|
|
|
|
return await self.async_step_simple_options()
|
|
|
|
|
|
|
|
async def async_step_simple_options(self, user_input=None):
|
|
|
|
"""For simple options."""
|
|
|
|
if user_input is not None:
|
|
|
|
self.options.update(user_input)
|
|
|
|
self.blink.refresh_rate = user_input[CONF_SCAN_INTERVAL]
|
|
|
|
return self.async_create_entry(title="", data=self.options)
|
|
|
|
|
|
|
|
options = self.config_entry.options
|
|
|
|
scan_interval = options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="simple_options",
|
|
|
|
data_schema=vol.Schema(
|
2020-08-27 11:56:20 +00:00
|
|
|
{
|
|
|
|
vol.Optional(
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
default=scan_interval,
|
|
|
|
): int
|
|
|
|
}
|
2020-06-10 16:38:17 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-05-13 13:50:29 +00:00
|
|
|
class Require2FA(exceptions.HomeAssistantError):
|
|
|
|
"""Error to indicate we require 2FA."""
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidAuth(exceptions.HomeAssistantError):
|
|
|
|
"""Error to indicate there is invalid auth."""
|