Skip to content

Commit

Permalink
Use selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus committed Feb 12, 2023
1 parent 6aab126 commit 937e453
Showing 1 changed file with 52 additions and 40 deletions.
92 changes: 52 additions & 40 deletions custom_components/integration_blueprint/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()

0 comments on commit 937e453

Please sign in to comment.