Add typing to Blink config flow (#98873)

This commit is contained in:
Joost Lekkerkerker 2023-08-30 11:50:47 +02:00 committed by GitHub
parent 2ce5b08fc3
commit fae50169d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,7 +9,7 @@ from blinkpy.auth import Auth, LoginError, TokenRefreshFailed
from blinkpy.blinkpy import Blink, BlinkSetupError from blinkpy.blinkpy import Blink, BlinkSetupError
import voluptuous as vol import voluptuous as vol
from homeassistant import config_entries, core, exceptions from homeassistant.config_entries import ConfigEntry, ConfigFlow
from homeassistant.const import ( from homeassistant.const import (
CONF_PASSWORD, CONF_PASSWORD,
CONF_PIN, CONF_PIN,
@ -18,6 +18,7 @@ from homeassistant.const import (
) )
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import selector from homeassistant.helpers import selector
from homeassistant.helpers.schema_config_entry_flow import ( from homeassistant.helpers.schema_config_entry_flow import (
SchemaFlowFormStep, SchemaFlowFormStep,
@ -48,7 +49,7 @@ OPTIONS_FLOW = {
} }
def validate_input(hass: core.HomeAssistant, auth): def validate_input(auth: Auth) -> None:
"""Validate the user input allows us to connect.""" """Validate the user input allows us to connect."""
try: try:
auth.startup() auth.startup()
@ -58,7 +59,7 @@ def validate_input(hass: core.HomeAssistant, auth):
raise Require2FA raise Require2FA
def _send_blink_2fa_pin(auth, pin): def _send_blink_2fa_pin(auth: Auth, pin: str) -> bool:
"""Send 2FA pin to blink servers.""" """Send 2FA pin to blink servers."""
blink = Blink() blink = Blink()
blink.auth = auth blink.auth = auth
@ -67,38 +68,34 @@ def _send_blink_2fa_pin(auth, pin):
return auth.send_auth_key(blink, pin) return auth.send_auth_key(blink, pin)
class BlinkConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): class BlinkConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a Blink config flow.""" """Handle a Blink config flow."""
VERSION = 3 VERSION = 3
def __init__(self) -> None: def __init__(self) -> None:
"""Initialize the blink flow.""" """Initialize the blink flow."""
self.auth = None self.auth: Auth | None = None
@staticmethod @staticmethod
@callback @callback
def async_get_options_flow( def async_get_options_flow(
config_entry: config_entries.ConfigEntry, config_entry: ConfigEntry,
) -> SchemaOptionsFlowHandler: ) -> SchemaOptionsFlowHandler:
"""Get options flow for this handler.""" """Get options flow for this handler."""
return SchemaOptionsFlowHandler(config_entry, OPTIONS_FLOW) return SchemaOptionsFlowHandler(config_entry, OPTIONS_FLOW)
async def async_step_user(self, user_input=None): async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle a flow initiated by the user.""" """Handle a flow initiated by the user."""
errors = {} errors = {}
data = {CONF_USERNAME: "", CONF_PASSWORD: "", "device_id": DEVICE_ID}
if user_input is not None: if user_input is not None:
data[CONF_USERNAME] = user_input["username"] self.auth = Auth({**user_input, "device_id": DEVICE_ID}, no_prompt=True)
data[CONF_PASSWORD] = user_input["password"] await self.async_set_unique_id(user_input[CONF_USERNAME])
self.auth = Auth(data, no_prompt=True)
await self.async_set_unique_id(data[CONF_USERNAME])
try: try:
await self.hass.async_add_executor_job( await self.hass.async_add_executor_job(validate_input, self.auth)
validate_input, self.hass, self.auth
)
return self._async_finish_flow() return self._async_finish_flow()
except Require2FA: except Require2FA:
return await self.async_step_2fa() return await self.async_step_2fa()
@ -108,18 +105,20 @@ class BlinkConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
_LOGGER.exception("Unexpected exception") _LOGGER.exception("Unexpected exception")
errors["base"] = "unknown" errors["base"] = "unknown"
data_schema = {
vol.Required("username"): str,
vol.Required("password"): str,
}
return self.async_show_form( return self.async_show_form(
step_id="user", step_id="user",
data_schema=vol.Schema(data_schema), data_schema=vol.Schema(
{
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
}
),
errors=errors, errors=errors,
) )
async def async_step_2fa(self, user_input=None): async def async_step_2fa(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle 2FA step.""" """Handle 2FA step."""
errors = {} errors = {}
if user_input is not None: if user_input is not None:
@ -142,7 +141,7 @@ class BlinkConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
return self.async_show_form( return self.async_show_form(
step_id="2fa", step_id="2fa",
data_schema=vol.Schema( data_schema=vol.Schema(
{vol.Optional("pin"): vol.All(str, vol.Length(min=1))} {vol.Optional(CONF_PIN): vol.All(str, vol.Length(min=1))}
), ),
errors=errors, errors=errors,
) )
@ -152,14 +151,15 @@ class BlinkConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
return await self.async_step_user(dict(entry_data)) return await self.async_step_user(dict(entry_data))
@callback @callback
def _async_finish_flow(self): def _async_finish_flow(self) -> FlowResult:
"""Finish with setup.""" """Finish with setup."""
assert self.auth
return self.async_create_entry(title=DOMAIN, data=self.auth.login_attributes) return self.async_create_entry(title=DOMAIN, data=self.auth.login_attributes)
class Require2FA(exceptions.HomeAssistantError): class Require2FA(HomeAssistantError):
"""Error to indicate we require 2FA.""" """Error to indicate we require 2FA."""
class InvalidAuth(exceptions.HomeAssistantError): class InvalidAuth(HomeAssistantError):
"""Error to indicate there is invalid auth.""" """Error to indicate there is invalid auth."""