Clean up Freebox config flow (#97347)

Co-authored-by: Robert Resch <robert@resch.dev>
pull/98871/head
Joost Lekkerkerker 2023-08-23 11:52:28 +02:00 committed by GitHub
parent a2b0149677
commit 5b3c60527a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 36 deletions

View File

@ -1,5 +1,6 @@
"""Config flow to configure the Freebox integration."""
import logging
from typing import Any
from freebox_api.exceptions import AuthorizationError, HttpRequestError
import voluptuous as vol
@ -20,45 +21,35 @@ class FreeboxFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1
def __init__(self) -> None:
"""Initialize Freebox config flow."""
self._host: str
self._port = None
_data: dict[str, Any] = {}
def _show_setup_form(self, user_input=None, errors=None):
"""Show the setup form to the user."""
if user_input is None:
user_input = {}
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_HOST, default=user_input.get(CONF_HOST, "")): str,
vol.Required(CONF_PORT, default=user_input.get(CONF_PORT, "")): int,
}
),
errors=errors or {},
)
async def async_step_user(self, user_input=None) -> FlowResult:
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle a flow initiated by the user."""
errors: dict[str, str] = {}
if user_input is None:
return self._show_setup_form(user_input, errors)
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_HOST): str,
vol.Required(CONF_PORT): int,
}
),
errors={},
)
self._host = user_input[CONF_HOST]
self._port = user_input[CONF_PORT]
self._data = user_input
# Check if already configured
await self.async_set_unique_id(self._host)
await self.async_set_unique_id(self._data[CONF_HOST])
self._abort_if_unique_id_configured()
return await self.async_step_link()
async def async_step_link(self, user_input=None) -> FlowResult:
async def async_step_link(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Attempt to link with the Freebox router.
Given a configured host, will ask the user to press the button
@ -69,10 +60,10 @@ class FreeboxFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
errors = {}
fbx = await get_api(self.hass, self._host)
fbx = await get_api(self.hass, self._data[CONF_HOST])
try:
# Open connection and check authentication
await fbx.open(self._host, self._port)
await fbx.open(self._data[CONF_HOST], self._data[CONF_PORT])
# Check permissions
await fbx.system.get_config()
@ -82,8 +73,8 @@ class FreeboxFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
await fbx.close()
return self.async_create_entry(
title=self._host,
data={CONF_HOST: self._host, CONF_PORT: self._port},
title=self._data[CONF_HOST],
data=self._data,
)
except AuthorizationError as error:
@ -91,18 +82,23 @@ class FreeboxFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
errors["base"] = "register_failed"
except HttpRequestError:
_LOGGER.error("Error connecting to the Freebox router at %s", self._host)
_LOGGER.error(
"Error connecting to the Freebox router at %s", self._data[CONF_HOST]
)
errors["base"] = "cannot_connect"
except Exception: # pylint: disable=broad-except
_LOGGER.exception(
"Unknown error connecting with Freebox router at %s", self._host
"Unknown error connecting with Freebox router at %s",
self._data[CONF_HOST],
)
errors["base"] = "unknown"
return self.async_show_form(step_id="link", errors=errors)
async def async_step_import(self, user_input=None) -> FlowResult:
async def async_step_import(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Import a config entry."""
return await self.async_step_user(user_input)