Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Adds config flow for GIOS."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 from typing import Any
7 
8 from aiohttp.client_exceptions import ClientConnectorError
9 from gios import ApiError, Gios, InvalidSensorsDataError, NoStationError
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_NAME
14 from homeassistant.helpers.aiohttp_client import async_get_clientsession
15 
16 from .const import API_TIMEOUT, CONF_STATION_ID, DOMAIN
17 
18 
19 class GiosFlowHandler(ConfigFlow, domain=DOMAIN):
20  """Config flow for GIOS."""
21 
22  VERSION = 1
23 
24  async def async_step_user(
25  self, user_input: dict[str, Any] | None = None
26  ) -> ConfigFlowResult:
27  """Handle a flow initialized by the user."""
28  errors = {}
29 
30  if user_input is not None:
31  try:
32  await self.async_set_unique_idasync_set_unique_id(
33  str(user_input[CONF_STATION_ID]), raise_on_progress=False
34  )
35  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
36 
37  websession = async_get_clientsession(self.hass)
38 
39  async with asyncio.timeout(API_TIMEOUT):
40  gios = Gios(user_input[CONF_STATION_ID], websession)
41  await gios.async_update()
42 
43  assert gios.station_name is not None
44  return self.async_create_entryasync_create_entryasync_create_entry(
45  title=gios.station_name,
46  data=user_input,
47  )
48  except (ApiError, ClientConnectorError, TimeoutError):
49  errors["base"] = "cannot_connect"
50  except NoStationError:
51  errors[CONF_STATION_ID] = "wrong_station_id"
52  except InvalidSensorsDataError:
53  errors[CONF_STATION_ID] = "invalid_sensors_data"
54 
55  return self.async_show_formasync_show_formasync_show_form(
56  step_id="user",
57  data_schema=vol.Schema(
58  {
59  vol.Required(CONF_STATION_ID): int,
60  vol.Optional(
61  CONF_NAME, default=self.hass.config.location_name
62  ): str,
63  }
64  ),
65  errors=errors,
66  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:26
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
str
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)