mirror of
https://github.com/jpawlowski/hass.tibber_prices.git
synced 2026-03-29 21:03:40 +00:00
82 lines
3 KiB
Python
82 lines
3 KiB
Python
"""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:
|
|
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(
|
|
## 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_ACCESS_TOKEN])
|
|
)
|
|
self._abort_if_unique_id_configured()
|
|
return self.async_create_entry(
|
|
title=user_input[CONF_ACCESS_TOKEN],
|
|
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) -> None:
|
|
"""Validate credentials."""
|
|
client = TibberPricesApiClient(
|
|
access_token=access_token,
|
|
session=async_create_clientsession(self.hass),
|
|
)
|
|
await client.async_test_connection()
|