From 937e453b6953b5f72e1c0d2deb923c4b119c8f59 Mon Sep 17 00:00:00 2001 From: ludeeus Date: Sun, 12 Feb 2023 09:34:09 +0000 Subject: [PATCH] Use selectors --- .../integration_blueprint/config_flow.py | 92 +++++++++++-------- 1 file changed, 52 insertions(+), 40 deletions(-) diff --git a/custom_components/integration_blueprint/config_flow.py b/custom_components/integration_blueprint/config_flow.py index 2cea985..d637a05 100644 --- a/custom_components/integration_blueprint/config_flow.py +++ b/custom_components/integration_blueprint/config_flow.py @@ -4,10 +4,16 @@ import voluptuous as vol from homeassistant import config_entries from homeassistant.const import CONF_PASSWORD, CONF_USERNAME +from homeassistant.helpers import selector from homeassistant.helpers.aiohttp_client import async_create_clientsession -from .api import IntegrationBlueprintApiClient -from .const import DOMAIN +from .api import ( + IntegrationBlueprintApiClient, + IntegrationBlueprintApiClientAuthenticationError, + IntegrationBlueprintApiClientCommunicationError, + IntegrationBlueprintApiClientError, +) +from .const import DOMAIN, LOGGER class BlueprintFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): @@ -16,54 +22,60 @@ class BlueprintFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): VERSION = 1 CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL - def __init__(self): - """Initialize.""" - self._errors = {} - - async def async_step_user(self, user_input=None): + async def async_step_user( + self, + user_input: dict | None = None, + ) -> config_entries.FlowResult: """Handle a flow initialized by the user.""" - self._errors = {} - + _errors = {} if user_input is not None: - valid = await self._test_credentials( - user_input[CONF_USERNAME], user_input[CONF_PASSWORD] - ) - if valid: - return self.async_create_entry( - title=user_input[CONF_USERNAME], data=user_input + try: + await self._test_credentials( + username=user_input[CONF_USERNAME], + password=user_input[CONF_PASSWORD], ) + except IntegrationBlueprintApiClientAuthenticationError as exception: + LOGGER.warning(exception) + _errors["base"] = "auth" + except IntegrationBlueprintApiClientCommunicationError as exception: + LOGGER.error(exception) + _errors["base"] = "connection" + except IntegrationBlueprintApiClientError as exception: + LOGGER.exception(exception) + _errors["base"] = "unknown" else: - self._errors["base"] = "auth" - - return await self._show_config_form(user_input) - - user_input = {} - # Provide defaults for form - user_input[CONF_USERNAME] = "" - user_input[CONF_PASSWORD] = "" - - return await self._show_config_form(user_input) + return self.async_create_entry( + title=user_input[CONF_USERNAME], + data=user_input, + ) - async def _show_config_form(self, user_input): # pylint: disable=unused-argument - """Show the configuration form to edit location data.""" return self.async_show_form( step_id="user", data_schema=vol.Schema( { - vol.Required(CONF_USERNAME, default=user_input[CONF_USERNAME]): str, - vol.Required(CONF_PASSWORD, default=user_input[CONF_PASSWORD]): str, + vol.Required( + CONF_USERNAME, + default=(user_input or {}).get(CONF_USERNAME), + ): selector.TextSelector( + selector.TextSelectorConfig( + type=selector.TextSelectorType.TEXT + ), + ), + vol.Required(CONF_PASSWORD): selector.TextSelector( + selector.TextSelectorConfig( + type=selector.TextSelectorType.PASSWORD + ), + ), } ), - errors=self._errors, + errors=_errors, ) - async def _test_credentials(self, username, password): - """Return true if credentials is valid.""" - try: - session = async_create_clientsession(self.hass) - client = IntegrationBlueprintApiClient(username, password, session) - await client.async_get_data() - return True - except Exception: # pylint: disable=broad-except - pass - return False + async def _test_credentials(self, username: str, password: str) -> None: + """Validate credentials.""" + client = IntegrationBlueprintApiClient( + username=username, + password=password, + session=async_create_clientsession(self.hass), + ) + await client.async_get_data()