Skip to content

Commit

Permalink
Config flow updates to accept an email
Browse files Browse the repository at this point in the history
  • Loading branch information
NLthijs48 committed Apr 5, 2024
1 parent b24c02f commit cd30649
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
5 changes: 2 additions & 3 deletions custom_components/crisp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from __future__ import annotations

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.const import CONF_EMAIL, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession

Expand All @@ -28,8 +28,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN][entry.entry_id] = coordinator = BlueprintDataUpdateCoordinator(
hass=hass,
client=IntegrationBlueprintApiClient(
username=entry.data[CONF_USERNAME],
password=entry.data[CONF_PASSWORD],
email=entry.data[CONF_EMAIL],
session=async_get_clientsession(hass),
),
)
Expand Down
6 changes: 2 additions & 4 deletions custom_components/crisp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ class IntegrationBlueprintApiClient:

def __init__(
self,
username: str,
password: str,
email: str,
session: aiohttp.ClientSession,
) -> None:
"""Sample API Client."""
self._username = username
self._password = password
self._email = email
self._session = session

async def async_get_data(self) -> any:
Expand Down
43 changes: 23 additions & 20 deletions custom_components/crisp/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.const import CONF_EMAIL
from homeassistant.helpers import selector
from homeassistant.helpers.aiohttp_client import async_create_clientsession

Expand All @@ -17,21 +17,30 @@


class BlueprintFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for Blueprint."""
"""Config flow for Crisp."""

VERSION = 1
MINOR_VERSION = 1

async def async_step_user(
self,
user_input: dict | None = None,
info: dict | None = None,
) -> config_entries.FlowResult:
"""Handle a flow initialized by the user."""
"""Handle a flow initialized by the user: initial setup"""
_errors = {}
if user_input is not None:
if info is not None:
# TODO: validate email? is there some function for that?

# TODO: switch to the Crisp userid which is more stable? Or at least make email lowercase
# Set unique id of this config flow to the entered email
await self.async_set_unique_id(info[CONF_EMAIL])
# Ensure config flow can only be done once for this email
self._abort_if_unique_id_configured()

try:
# TODO: call Crisp api to request code, handle response
await self._test_credentials(
username=user_input[CONF_USERNAME],
password=user_input[CONF_PASSWORD],
email=info[CONF_EMAIL],
)
except IntegrationBlueprintApiClientAuthenticationError as exception:
LOGGER.warning(exception)
Expand All @@ -44,37 +53,31 @@ async def async_step_user(
_errors["base"] = "unknown"
else:
return self.async_create_entry(
title=user_input[CONF_USERNAME],
data=user_input,
title=info[CONF_EMAIL],
data=info,
)

return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(
CONF_USERNAME,
default=(user_input or {}).get(CONF_USERNAME),
CONF_EMAIL,
default=(info or {}).get(CONF_EMAIL),
): selector.TextSelector(
selector.TextSelectorConfig(
type=selector.TextSelectorType.TEXT
),
),
vol.Required(CONF_PASSWORD): selector.TextSelector(
selector.TextSelectorConfig(
type=selector.TextSelectorType.PASSWORD
type=selector.TextSelectorType.EMAIL
),
),
}
),
errors=_errors,
)

async def _test_credentials(self, username: str, password: str) -> None:
async def _test_credentials(self, email: str) -> None:
"""Validate credentials."""
client = IntegrationBlueprintApiClient(
username=username,
password=password,
email=email,
session=async_create_clientsession(self.hass),
)
await client.async_get_data()
8 changes: 5 additions & 3 deletions custom_components/crisp/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
"config": {
"step": {
"user": {
"description": "If you need help with the configuration have a look here: https://github.com/NLthijs48/home-assistant-crisp",
"description": "Enter your email adress used for your Crisp account.",
"data": {
"username": "Username",
"password": "Password"
"email": "Email"
}
}
},
"abort": {
"already_configured": "Crisp account with that email is already configured."
},
"error": {
"auth": "Username/Password is wrong.",
"connection": "Unable to connect to the server.",
Expand Down

0 comments on commit cd30649

Please sign in to comment.