Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config Flow for the zamg integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 from zamg import ZamgData
9 from zamg.exceptions import ZamgApiError, ZamgNoDataError
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.helpers.aiohttp_client import async_get_clientsession
13 
14 from .const import CONF_STATION_ID, DOMAIN, LOGGER
15 
16 
17 class ZamgConfigFlow(ConfigFlow, domain=DOMAIN):
18  """Config flow for zamg integration."""
19 
20  VERSION = 1
21 
22  _client: ZamgData | None = None
23 
24  async def async_step_user(
25  self, user_input: dict[str, Any] | None = None
26  ) -> ConfigFlowResult:
27  """Handle a flow initiated by the user."""
28  if self._client_client is None:
29  self._client_client = ZamgData()
30  self._client_client.session = async_get_clientsession(self.hass)
31 
32  if user_input is None:
33  try:
34  stations = await self._client_client.zamg_stations()
35  closest_station_id = await self._client_client.closest_station(
36  self.hass.config.latitude,
37  self.hass.config.longitude,
38  )
39  except (ZamgApiError, ZamgNoDataError) as err:
40  LOGGER.error("Config_flow: Received error from ZAMG: %s", err)
41  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
42  LOGGER.debug("config_flow: closest station = %s", closest_station_id)
43  user_input = {}
44 
45  schema = vol.Schema(
46  {
47  vol.Required(CONF_STATION_ID, default=closest_station_id): vol.In(
48  {
49  station: f"{stations[station][2]} ({station})"
50  for station in stations
51  }
52  )
53  }
54  )
55  return self.async_show_formasync_show_formasync_show_form(step_id="user", data_schema=schema)
56 
57  station_id = user_input[CONF_STATION_ID]
58 
59  # Check if already configured
60  await self.async_set_unique_idasync_set_unique_id(station_id)
61  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
62 
63  try:
64  self._client_client.set_default_station(station_id)
65  await self._client_client.update()
66  except (ZamgApiError, ZamgNoDataError) as err:
67  LOGGER.error("Config_flow: Received error from ZAMG: %s", err)
68  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
69 
70  return self.async_create_entryasync_create_entryasync_create_entry(
71  title=self._client_client.get_station_name,
72  data={CONF_STATION_ID: station_id},
73  )
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_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
_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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
IssData update(pyiss.ISS iss)
Definition: __init__.py:33
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)