"""Config flow for PerfectDraft integration.""" from __future__ import annotations import logging from typing import Any import voluptuous as vol from homeassistant import config_entries from homeassistant.const import CONF_EMAIL, CONF_PASSWORD from homeassistant.core import callback from homeassistant.helpers.aiohttp_client import async_get_clientsession from .api import PerfectDraftApiClient from .const import ( CONF_ACCESS_TOKEN, CONF_ID_TOKEN, CONF_MACHINE_ID, CONF_REFRESH_TOKEN, CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL, DOMAIN, MIN_SCAN_INTERVAL, ) from .exceptions import ( AuthenticationError, PerfectDraftApiError, PerfectDraftConnectionError, ) _LOGGER = logging.getLogger(__name__) CONF_RECAPTCHA_TOKEN = "recaptcha_token" _AUTH_ERRORS = { "credentials": "invalid_credentials", "token": "token_rejected", "other": "invalid_auth", } class PerfectDraftConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Handle a config flow for PerfectDraft.""" VERSION = 1 def __init__(self) -> None: self._email: str | None = None self._password: str | None = None # Fills {reason} in the error strings; every form must pass it. self._auth_reason = "" async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> config_entries.ConfigFlowResult: """Step 1: collect email and password.""" if user_input is not None: self._email = user_input[CONF_EMAIL] self._password = user_input[CONF_PASSWORD] return await self.async_step_token() return self._show_user_form({}) def _show_user_form( self, errors: dict[str, str] ) -> config_entries.ConfigFlowResult: return self.async_show_form( step_id="user", data_schema=vol.Schema( { vol.Required(CONF_EMAIL): str, vol.Required(CONF_PASSWORD): str, } ), errors=errors, description_placeholders={"reason": self._auth_reason}, ) async def async_step_token( self, user_input: dict[str, Any] | None = None ) -> config_entries.ConfigFlowResult: """Step 2: collect reCAPTCHA token from user.""" errors: dict[str, str] = {} if user_input is not None: recaptcha_token = user_input[CONF_RECAPTCHA_TOKEN].strip() session = async_get_clientsession(self.hass) client = PerfectDraftApiClient(session) try: await client.authenticate( self._email, self._password, recaptcha_token ) except AuthenticationError as err: _LOGGER.error("Authentication failed: %s", err) self._auth_reason = err.display_reason error = _AUTH_ERRORS[err.kind] if error == "invalid_credentials": # A fresh token cannot fix a wrong password, so ask for # the credentials again instead of looping on this step. if self.source == config_entries.SOURCE_REAUTH: return self._show_reauth_confirm_form({"base": error}) return self._show_user_form({"base": error}) errors["base"] = error except PerfectDraftConnectionError as err: _LOGGER.error("Connection failed: %s", err) errors["base"] = "cannot_connect" except PerfectDraftApiError as err: _LOGGER.error("API error during auth: %s", err) errors["base"] = "unknown" else: reauth_entry = ( self._get_reauth_entry() if self.source == config_entries.SOURCE_REAUTH else None ) machine_id = ( reauth_entry.data.get(CONF_MACHINE_ID) if reauth_entry else None ) if machine_id: return await self._async_persist( client, reauth_entry, machine_id ) try: profile = await client.get_user_profile() except (PerfectDraftApiError, PerfectDraftConnectionError): errors["base"] = "cannot_connect" else: return await self._async_persist( client, reauth_entry, _extract_machine_id(profile) ) return self.async_show_form( step_id="token", data_schema=vol.Schema( { vol.Required(CONF_RECAPTCHA_TOKEN): str, } ), errors=errors, description_placeholders={"reason": self._auth_reason}, ) async def _async_persist( self, client: PerfectDraftApiClient, reauth_entry: config_entries.ConfigEntry | None, machine_id: str | None, ) -> config_entries.ConfigFlowResult: """Store the freshly issued tokens. Reauth must update the entry it was started for. Creating a new one would change the entry_id and orphan every entity's unique_id, and `_abort_if_unique_id_configured` cannot be used here at all: during reauth the unique ID belongs to that very entry, so the duplicate guard would abort the flow and discard the new tokens. """ data = { CONF_ACCESS_TOKEN: client.access_token, CONF_ID_TOKEN: client.id_token, CONF_REFRESH_TOKEN: client.refresh_token, CONF_MACHINE_ID: machine_id, } await self.async_set_unique_id(self._email.lower()) if reauth_entry is not None: self._abort_if_unique_id_mismatch(reason="reauth_account_mismatch") return self.async_update_reload_and_abort( reauth_entry, data_updates=data ) self._abort_if_unique_id_configured() return self.async_create_entry( title=f"PerfectDraft ({self._email})", data={CONF_EMAIL: self._email, **data}, ) async def async_step_reauth( self, entry_data: dict[str, Any] ) -> config_entries.ConfigFlowResult: """Handle re-authentication when the refresh token expires.""" self._email = entry_data.get(CONF_EMAIL) return await self.async_step_reauth_confirm() async def async_step_reauth_confirm( self, user_input: dict[str, Any] | None = None ) -> config_entries.ConfigFlowResult: """Ask user to re-enter credentials for reauth.""" if user_input is not None: self._email = user_input[CONF_EMAIL] self._password = user_input[CONF_PASSWORD] return await self.async_step_token() return self._show_reauth_confirm_form({}) def _show_reauth_confirm_form( self, errors: dict[str, str] ) -> config_entries.ConfigFlowResult: return self.async_show_form( step_id="reauth_confirm", data_schema=vol.Schema( { vol.Required(CONF_EMAIL, default=self._email): str, vol.Required(CONF_PASSWORD): str, } ), errors=errors, description_placeholders={"reason": self._auth_reason}, ) @staticmethod @callback def async_get_options_flow( config_entry: config_entries.ConfigEntry, ) -> PerfectDraftOptionsFlow: return PerfectDraftOptionsFlow(config_entry) class PerfectDraftOptionsFlow(config_entries.OptionsFlow): """Handle options for PerfectDraft (polling interval).""" def __init__(self, config_entry: config_entries.ConfigEntry) -> None: self._config_entry = config_entry async def async_step_init( self, user_input: dict[str, Any] | None = None ) -> config_entries.ConfigFlowResult: if user_input is not None: interval = max(user_input[CONF_SCAN_INTERVAL], MIN_SCAN_INTERVAL) return self.async_create_entry( title="", data={CONF_SCAN_INTERVAL: interval} ) current = self._config_entry.options.get( CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL ) return self.async_show_form( step_id="init", data_schema=vol.Schema( { vol.Required(CONF_SCAN_INTERVAL, default=current): vol.All( int, vol.Range(min=MIN_SCAN_INTERVAL) ), } ), ) def _extract_machine_id(profile: dict[str, Any]) -> str | None: """Extract the first machine ID from the /api/me response.""" machines = profile.get("perfectdraftMachines") if isinstance(machines, list) and machines: return str(machines[0].get("id", "")) return None