2023-12-29 13:55:41 +00:00
|
|
|
"""Config flow for Tedee integration."""
|
2024-01-02 12:28:15 +00:00
|
|
|
from collections.abc import Mapping
|
2023-12-29 13:55:41 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from pytedee_async import (
|
|
|
|
TedeeAuthException,
|
|
|
|
TedeeClient,
|
|
|
|
TedeeClientException,
|
|
|
|
TedeeLocalAuthException,
|
|
|
|
)
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2024-01-02 12:28:15 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry, ConfigFlow
|
2023-12-29 13:55:41 +00:00
|
|
|
from homeassistant.const import CONF_HOST
|
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2024-01-04 20:42:38 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2023-12-29 13:55:41 +00:00
|
|
|
|
|
|
|
from .const import CONF_LOCAL_ACCESS_TOKEN, DOMAIN, NAME
|
|
|
|
|
|
|
|
|
2024-01-02 12:28:15 +00:00
|
|
|
class TedeeConfigFlow(ConfigFlow, domain=DOMAIN):
|
2023-12-29 13:55:41 +00:00
|
|
|
"""Handle a config flow for Tedee."""
|
|
|
|
|
2024-01-02 12:28:15 +00:00
|
|
|
reauth_entry: ConfigEntry | None = None
|
|
|
|
|
2023-12-29 13:55:41 +00:00
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
|
|
|
"""Handle the initial step."""
|
|
|
|
errors: dict[str, str] = {}
|
|
|
|
|
|
|
|
if user_input is not None:
|
2024-01-02 12:28:15 +00:00
|
|
|
if self.reauth_entry:
|
|
|
|
host = self.reauth_entry.data[CONF_HOST]
|
|
|
|
else:
|
|
|
|
host = user_input[CONF_HOST]
|
2023-12-29 13:55:41 +00:00
|
|
|
local_access_token = user_input[CONF_LOCAL_ACCESS_TOKEN]
|
2024-01-04 20:42:38 +00:00
|
|
|
tedee_client = TedeeClient(
|
|
|
|
local_token=local_access_token,
|
|
|
|
local_ip=host,
|
|
|
|
session=async_get_clientsession(self.hass),
|
|
|
|
)
|
2023-12-29 13:55:41 +00:00
|
|
|
try:
|
|
|
|
local_bridge = await tedee_client.get_local_bridge()
|
|
|
|
except (TedeeAuthException, TedeeLocalAuthException):
|
|
|
|
errors[CONF_LOCAL_ACCESS_TOKEN] = "invalid_api_key"
|
|
|
|
except TedeeClientException:
|
|
|
|
errors[CONF_HOST] = "invalid_host"
|
|
|
|
else:
|
2024-01-02 12:28:15 +00:00
|
|
|
if self.reauth_entry:
|
|
|
|
self.hass.config_entries.async_update_entry(
|
|
|
|
self.reauth_entry,
|
|
|
|
data={**self.reauth_entry.data, **user_input},
|
|
|
|
)
|
|
|
|
await self.hass.config_entries.async_reload(
|
|
|
|
self.context["entry_id"]
|
|
|
|
)
|
|
|
|
return self.async_abort(reason="reauth_successful")
|
2023-12-29 13:55:41 +00:00
|
|
|
await self.async_set_unique_id(local_bridge.serial)
|
|
|
|
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(
|
|
|
|
{
|
2024-01-02 12:28:15 +00:00
|
|
|
vol.Required(
|
|
|
|
CONF_HOST,
|
|
|
|
): str,
|
|
|
|
vol.Required(
|
|
|
|
CONF_LOCAL_ACCESS_TOKEN,
|
|
|
|
): str,
|
2023-12-29 13:55:41 +00:00
|
|
|
}
|
|
|
|
),
|
|
|
|
errors=errors,
|
|
|
|
)
|
2024-01-02 12:28:15 +00:00
|
|
|
|
|
|
|
async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
|
|
|
|
"""Perform reauth upon an API authentication error."""
|
|
|
|
self.reauth_entry = self.hass.config_entries.async_get_entry(
|
|
|
|
self.context["entry_id"]
|
|
|
|
)
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(
|
|
|
|
CONF_LOCAL_ACCESS_TOKEN,
|
|
|
|
default=entry_data[CONF_LOCAL_ACCESS_TOKEN],
|
|
|
|
): str,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|