129 lines
4.3 KiB
Python
129 lines
4.3 KiB
Python
"""Config flow for Open Exchange Rates integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from collections.abc import Mapping
|
|
from typing import Any
|
|
|
|
from aioopenexchangerates import (
|
|
Client,
|
|
OpenExchangeRatesAuthError,
|
|
OpenExchangeRatesClientError,
|
|
)
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
|
|
from homeassistant.const import CONF_API_KEY, CONF_BASE
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import AbortFlow
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from .const import CLIENT_TIMEOUT, DEFAULT_BASE, DOMAIN, LOGGER
|
|
|
|
|
|
def get_data_schema(
|
|
currencies: dict[str, str], existing_data: Mapping[str, str]
|
|
) -> vol.Schema:
|
|
"""Return a form schema."""
|
|
return vol.Schema(
|
|
{
|
|
vol.Required(CONF_API_KEY): str,
|
|
vol.Optional(
|
|
CONF_BASE, default=existing_data.get(CONF_BASE) or DEFAULT_BASE
|
|
): vol.In(currencies),
|
|
}
|
|
)
|
|
|
|
|
|
async def validate_input(hass: HomeAssistant, data: dict[str, str]) -> dict[str, str]:
|
|
"""Validate the user input allows us to connect."""
|
|
client = Client(data[CONF_API_KEY], async_get_clientsession(hass))
|
|
|
|
async with asyncio.timeout(CLIENT_TIMEOUT):
|
|
await client.get_latest(base=data[CONF_BASE])
|
|
|
|
return {"title": data[CONF_BASE]}
|
|
|
|
|
|
class OpenExchangeRatesConfigFlow(ConfigFlow, domain=DOMAIN):
|
|
"""Handle a config flow for Open Exchange Rates."""
|
|
|
|
VERSION = 1
|
|
|
|
def __init__(self) -> None:
|
|
"""Initialize the config flow."""
|
|
self.currencies: dict[str, str] = {}
|
|
|
|
async def async_step_user(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> ConfigFlowResult:
|
|
"""Handle the initial step."""
|
|
currencies = await self.async_get_currencies()
|
|
|
|
if user_input is None:
|
|
existing_data: Mapping[str, Any] = {}
|
|
if self.source == SOURCE_REAUTH:
|
|
existing_data = self._get_reauth_entry().data
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=get_data_schema(currencies, existing_data),
|
|
description_placeholders={
|
|
"signup": "https://openexchangerates.org/signup"
|
|
},
|
|
)
|
|
|
|
errors = {}
|
|
|
|
try:
|
|
info = await validate_input(self.hass, user_input)
|
|
except OpenExchangeRatesAuthError:
|
|
errors["base"] = "invalid_auth"
|
|
except OpenExchangeRatesClientError:
|
|
errors["base"] = "cannot_connect"
|
|
except TimeoutError:
|
|
errors["base"] = "timeout_connect"
|
|
except Exception: # noqa: BLE001
|
|
LOGGER.exception("Unexpected exception")
|
|
errors["base"] = "unknown"
|
|
else:
|
|
self._async_abort_entries_match(
|
|
{
|
|
CONF_API_KEY: user_input[CONF_API_KEY],
|
|
CONF_BASE: user_input[CONF_BASE],
|
|
}
|
|
)
|
|
|
|
if self.source == SOURCE_REAUTH:
|
|
return self.async_update_reload_and_abort(
|
|
self._get_reauth_entry(), data_updates=user_input
|
|
)
|
|
|
|
return self.async_create_entry(title=info["title"], data=user_input)
|
|
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=get_data_schema(currencies, user_input),
|
|
description_placeholders={"signup": "https://openexchangerates.org/signup"},
|
|
errors=errors,
|
|
)
|
|
|
|
async def async_step_reauth(
|
|
self, entry_data: Mapping[str, Any]
|
|
) -> ConfigFlowResult:
|
|
"""Handle reauth."""
|
|
return await self.async_step_user()
|
|
|
|
async def async_get_currencies(self) -> dict[str, str]:
|
|
"""Get the available currencies."""
|
|
if not self.currencies:
|
|
client = Client("dummy-api-key", async_get_clientsession(self.hass))
|
|
try:
|
|
async with asyncio.timeout(CLIENT_TIMEOUT):
|
|
self.currencies = await client.get_currencies()
|
|
except OpenExchangeRatesClientError as err:
|
|
raise AbortFlow("cannot_connect") from err
|
|
except TimeoutError as err:
|
|
raise AbortFlow("timeout_connect") from err
|
|
return self.currencies
|