1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-08 09:38:58 +01:00
Files
core/homeassistant/components/gios/config_flow.py
T
Kamil Breguła e49767d37a GIOS quality scale fixes to platinum (#162510)
Co-authored-by: mik-laj <12058428+mik-laj@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-16 18:59:45 +01:00

95 lines
3.1 KiB
Python

"""Adds config flow for GIOS."""
from __future__ import annotations
import asyncio
from typing import TYPE_CHECKING, Any
from aiohttp.client_exceptions import ClientConnectorError
from gios import ApiError, Gios, InvalidSensorsDataError, NoStationError
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_NAME
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.selector import (
SelectOptionDict,
SelectSelector,
SelectSelectorConfig,
SelectSelectorMode,
)
from .const import API_TIMEOUT, CONF_STATION_ID, DOMAIN
class GiosFlowHandler(ConfigFlow, domain=DOMAIN):
"""Config flow for GIOS."""
VERSION = 1
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle a flow initialized by the user."""
errors = {}
websession = async_get_clientsession(self.hass)
if user_input is not None:
station_id = user_input[CONF_STATION_ID]
await self.async_set_unique_id(station_id, raise_on_progress=False)
self._abort_if_unique_id_configured()
try:
async with asyncio.timeout(API_TIMEOUT):
gios = await Gios.create(websession, int(station_id))
await gios.async_update()
except ApiError, ClientConnectorError, TimeoutError:
errors["base"] = "cannot_connect"
except InvalidSensorsDataError:
errors[CONF_STATION_ID] = "invalid_sensors_data"
else:
# GIOS treats station ID as int
user_input[CONF_STATION_ID] = int(station_id)
if TYPE_CHECKING:
assert gios.station_name is not None
return self.async_create_entry(
title=gios.station_name,
# CONF_NAME is still used, but its value is preserved
# primarily for backward compatibility. This allows older
# versions of the software to read the entry data without
# raising errors.
data={**user_input, CONF_NAME: gios.station_name},
)
try:
gios = await Gios.create(websession)
except ApiError, ClientConnectorError, NoStationError:
return self.async_abort(reason="cannot_connect")
options: list[SelectOptionDict] = [
SelectOptionDict(value=str(station.id), label=station.name)
for station in gios.measurement_stations.values()
]
schema: vol.Schema = vol.Schema(
{
vol.Required(CONF_STATION_ID): SelectSelector(
SelectSelectorConfig(
options=options,
sort=True,
mode=SelectSelectorMode.DROPDOWN,
),
)
}
)
return self.async_show_form(
step_id="user",
data_schema=schema,
errors=errors,
)