Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for israel rail."""
2 
3 import logging
4 from typing import Any
5 
6 from israelrailapi import TrainSchedule
7 from israelrailapi.stations import STATIONS
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 
12 from .const import CONF_DESTINATION, CONF_START, DOMAIN
13 
14 STATIONS_NAMES = [station["Heb"] for station in STATIONS.values()]
15 STATIONS_NAMES.sort()
16 
17 DATA_SCHEMA = vol.Schema(
18  {
19  vol.Required(CONF_START): vol.In(STATIONS_NAMES),
20  vol.Required(CONF_DESTINATION): vol.In(STATIONS_NAMES),
21  }
22 )
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 
27 class IsraelRailConfigFlow(ConfigFlow, domain=DOMAIN):
28  """Israel rail config flow."""
29 
30  VERSION = 1
31 
32  async def async_step_user(
33  self, user_input: dict[str, Any] | None = None
34  ) -> ConfigFlowResult:
35  """Async user step to set up the connection."""
36  errors = {}
37  if user_input:
38  train_schedule = TrainSchedule()
39  try:
40  await self.hass.async_add_executor_job(
41  train_schedule.query,
42  user_input[CONF_START],
43  user_input[CONF_DESTINATION],
44  )
45  except Exception:
46  _LOGGER.exception("Unknown error")
47  errors["base"] = "unknown"
48  if not errors:
49  unique_id = f"{user_input[CONF_START]} {user_input[CONF_DESTINATION]}"
50  await self.async_set_unique_idasync_set_unique_id(unique_id)
51  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
52 
53  return self.async_create_entryasync_create_entryasync_create_entry(
54  title=unique_id,
55  data=user_input,
56  )
57 
58  return self.async_show_formasync_show_formasync_show_form(
59  step_id="user",
60  data_schema=DATA_SCHEMA,
61  errors=errors,
62  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:34
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)
_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)