Initial commit
This commit is contained in:
89
custom_components/integration_blueprint/config_flow.py
Normal file
89
custom_components/integration_blueprint/config_flow.py
Normal file
@@ -0,0 +1,89 @@
|
||||
"""Adds config flow for Blueprint."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
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 slugify import slugify
|
||||
|
||||
from .api import (
|
||||
IntegrationBlueprintApiClient,
|
||||
IntegrationBlueprintApiClientAuthenticationError,
|
||||
IntegrationBlueprintApiClientCommunicationError,
|
||||
IntegrationBlueprintApiClientError,
|
||||
)
|
||||
from .const import DOMAIN, LOGGER
|
||||
|
||||
|
||||
class BlueprintFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Config flow for Blueprint."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(
|
||||
self,
|
||||
user_input: dict | None = None,
|
||||
) -> config_entries.ConfigFlowResult:
|
||||
"""Handle a flow initialized by the user."""
|
||||
_errors = {}
|
||||
if user_input is not None:
|
||||
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:
|
||||
await self.async_set_unique_id(
|
||||
## Do NOT use this in production code
|
||||
## The unique_id should never be something that can change
|
||||
## https://developers.home-assistant.io/docs/config_entries_config_flow_handler#unique-ids
|
||||
unique_id=slugify(user_input[CONF_USERNAME])
|
||||
)
|
||||
self._abort_if_unique_id_configured()
|
||||
return self.async_create_entry(
|
||||
title=user_input[CONF_USERNAME],
|
||||
data=user_input,
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=vol.Schema(
|
||||
{
|
||||
vol.Required(
|
||||
CONF_USERNAME,
|
||||
default=(user_input or {}).get(CONF_USERNAME, vol.UNDEFINED),
|
||||
): selector.TextSelector(
|
||||
selector.TextSelectorConfig(
|
||||
type=selector.TextSelectorType.TEXT,
|
||||
),
|
||||
),
|
||||
vol.Required(CONF_PASSWORD): selector.TextSelector(
|
||||
selector.TextSelectorConfig(
|
||||
type=selector.TextSelectorType.PASSWORD,
|
||||
),
|
||||
),
|
||||
},
|
||||
),
|
||||
errors=_errors,
|
||||
)
|
||||
|
||||
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()
|
||||
Reference in New Issue
Block a user