"""Adds config flow for tibber_prices.""" from __future__ import annotations import voluptuous as vol from homeassistant import config_entries from homeassistant.const import CONF_ACCESS_TOKEN from homeassistant.helpers import selector from homeassistant.helpers.aiohttp_client import async_create_clientsession from slugify import slugify from .api import ( TibberPricesApiClient, TibberPricesApiClientAuthenticationError, TibberPricesApiClientCommunicationError, TibberPricesApiClientError, ) from .const import DOMAIN, LOGGER class TibberPricesFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Config flow for tibber_prices.""" 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: name = await self._test_credentials( access_token=user_input[CONF_ACCESS_TOKEN] ) except TibberPricesApiClientAuthenticationError as exception: LOGGER.warning(exception) _errors["base"] = "auth" except TibberPricesApiClientCommunicationError as exception: LOGGER.error(exception) _errors["base"] = "connection" except TibberPricesApiClientError as exception: LOGGER.exception(exception) _errors["base"] = "unknown" else: await self.async_set_unique_id(unique_id=slugify(name)) self._abort_if_unique_id_configured() return self.async_create_entry( title=name, data=user_input, ) return self.async_show_form( step_id="user", data_schema=vol.Schema( { vol.Required( CONF_ACCESS_TOKEN, default=(user_input or {}).get( CONF_ACCESS_TOKEN, vol.UNDEFINED ), ): selector.TextSelector( selector.TextSelectorConfig( type=selector.TextSelectorType.TEXT, ), ), }, ), errors=_errors, ) async def _test_credentials(self, access_token: str) -> str: """Validate credentials and return the user's name.""" client = TibberPricesApiClient( access_token=access_token, session=async_create_clientsession(self.hass), ) result = await client.async_test_connection() return result["viewer"]["name"]