diff --git a/custom_components/crisp/__init__.py b/custom_components/crisp/__init__.py index 643995f..7410427 100644 --- a/custom_components/crisp/__init__.py +++ b/custom_components/crisp/__init__.py @@ -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 @@ -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), ), ) diff --git a/custom_components/crisp/api.py b/custom_components/crisp/api.py index a738040..9d40ce7 100644 --- a/custom_components/crisp/api.py +++ b/custom_components/crisp/api.py @@ -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: diff --git a/custom_components/crisp/config_flow.py b/custom_components/crisp/config_flow.py index a474163..f7fd0d8 100644 --- a/custom_components/crisp/config_flow.py +++ b/custom_components/crisp/config_flow.py @@ -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 @@ -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) @@ -44,8 +53,8 @@ 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( @@ -53,16 +62,11 @@ async def async_step_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 ), ), } @@ -70,11 +74,10 @@ async def async_step_user( 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() diff --git a/custom_components/crisp/translations/en.json b/custom_components/crisp/translations/en.json index 71cdb6d..999050b 100644 --- a/custom_components/crisp/translations/en.json +++ b/custom_components/crisp/translations/en.json @@ -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.",