hass-core/homeassistant/components/nyt_games/config_flow.py
Joost Lekkerkerker 1d94e66b9c
Add NYT Games integration (#126449)
* Add NYT Games integration

* Add NYT Games integration

* Add NYT Games integration

* Add NYT Games integration

* Add test
2024-09-23 17:40:19 +02:00

42 lines
1.5 KiB
Python

"""Config flow for NYT Games."""
from typing import Any
from nyt_games import NYTGamesAuthenticationError, NYTGamesClient, NYTGamesError
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_TOKEN
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DOMAIN
class NYTGamesConfigFlow(ConfigFlow, domain=DOMAIN):
"""NYT Games config flow."""
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle a flow initialized by the user."""
errors: dict[str, str] = {}
if user_input:
session = async_get_clientsession(self.hass)
client = NYTGamesClient(user_input[CONF_TOKEN], session=session)
try:
latest_stats = await client.get_latest_stats()
except NYTGamesAuthenticationError:
errors["base"] = "invalid_auth"
except NYTGamesError:
errors["base"] = "cannot_connect"
except Exception: # noqa: BLE001
errors["base"] = "unknown"
else:
await self.async_set_unique_id(str(latest_stats.user_id))
self._abort_if_unique_id_configured()
return self.async_create_entry(title="NYT Games", data=user_input)
return self.async_show_form(
step_id="user",
data_schema=vol.Schema({vol.Required(CONF_TOKEN): str}),
errors=errors,
)