Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Adds config flow for Trafikverket Train integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from datetime import datetime
7 import logging
8 from typing import Any
9 
10 from pytrafikverket import TrafikverketTrain
11 from pytrafikverket.exceptions import (
12  InvalidAuthentication,
13  MultipleTrainStationsFound,
14  NoTrainAnnouncementFound,
15  NoTrainStationFound,
16  UnknownError,
17 )
18 import voluptuous as vol
19 
20 from homeassistant.config_entries import (
21  ConfigEntry,
22  ConfigFlow,
23  ConfigFlowResult,
24  OptionsFlow,
25 )
26 from homeassistant.const import CONF_API_KEY, CONF_NAME, CONF_WEEKDAY, WEEKDAYS
27 from homeassistant.core import HomeAssistant, callback
28 from homeassistant.helpers.aiohttp_client import async_get_clientsession
31  SelectSelector,
32  SelectSelectorConfig,
33  SelectSelectorMode,
34  TextSelector,
35  TimeSelector,
36 )
37 import homeassistant.util.dt as dt_util
38 
39 from .const import CONF_FILTER_PRODUCT, CONF_FROM, CONF_TIME, CONF_TO, DOMAIN
40 from .util import next_departuredate
41 
42 _LOGGER = logging.getLogger(__name__)
43 
44 OPTION_SCHEMA = {
45  vol.Optional(CONF_FILTER_PRODUCT, default=""): TextSelector(),
46 }
47 
48 DATA_SCHEMA = vol.Schema(
49  {
50  vol.Required(CONF_API_KEY): TextSelector(),
51  vol.Required(CONF_FROM): TextSelector(),
52  vol.Required(CONF_TO): TextSelector(),
53  vol.Optional(CONF_TIME): TimeSelector(),
54  vol.Required(CONF_WEEKDAY, default=WEEKDAYS): SelectSelector(
56  options=WEEKDAYS,
57  multiple=True,
58  mode=SelectSelectorMode.DROPDOWN,
59  translation_key=CONF_WEEKDAY,
60  )
61  ),
62  }
63 ).extend(OPTION_SCHEMA)
64 DATA_SCHEMA_REAUTH = vol.Schema(
65  {
66  vol.Required(CONF_API_KEY): cv.string,
67  }
68 )
69 
70 
71 async def validate_input(
72  hass: HomeAssistant,
73  api_key: str,
74  train_from: str,
75  train_to: str,
76  train_time: str | None,
77  weekdays: list[str],
78  product_filter: str | None,
79 ) -> dict[str, str]:
80  """Validate input from user input."""
81  errors: dict[str, str] = {}
82 
83  when = dt_util.now()
84  if train_time:
85  departure_day = next_departuredate(weekdays)
86  if _time := dt_util.parse_time(train_time):
87  when = datetime.combine(
88  departure_day,
89  _time,
90  dt_util.get_default_time_zone(),
91  )
92 
93  try:
94  web_session = async_get_clientsession(hass)
95  train_api = TrafikverketTrain(web_session, api_key)
96  from_station = await train_api.async_search_train_station(train_from)
97  to_station = await train_api.async_search_train_station(train_to)
98  if train_time:
99  await train_api.async_get_train_stop(
100  from_station, to_station, when, product_filter
101  )
102  else:
103  await train_api.async_get_next_train_stop(
104  from_station, to_station, when, product_filter
105  )
106  except InvalidAuthentication:
107  errors["base"] = "invalid_auth"
108  except NoTrainStationFound:
109  errors["base"] = "invalid_station"
110  except MultipleTrainStationsFound:
111  errors["base"] = "more_stations"
112  except NoTrainAnnouncementFound:
113  errors["base"] = "no_trains"
114  except UnknownError as error:
115  _LOGGER.error("Unknown error occurred during validation %s", str(error))
116  errors["base"] = "cannot_connect"
117  except Exception as error: # noqa: BLE001
118  _LOGGER.error("Unknown exception occurred during validation %s", str(error))
119  errors["base"] = "cannot_connect"
120 
121  return errors
122 
123 
124 class TVTrainConfigFlow(ConfigFlow, domain=DOMAIN):
125  """Handle a config flow for Trafikverket Train integration."""
126 
127  VERSION = 1
128  MINOR_VERSION = 2
129 
130  @staticmethod
131  @callback
133  config_entry: ConfigEntry,
134  ) -> TVTrainOptionsFlowHandler:
135  """Get the options flow for this handler."""
137 
138  async def async_step_reauth(
139  self, entry_data: Mapping[str, Any]
140  ) -> ConfigFlowResult:
141  """Handle re-authentication with Trafikverket."""
142  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
143 
145  self, user_input: dict[str, Any] | None = None
146  ) -> ConfigFlowResult:
147  """Confirm re-authentication with Trafikverket."""
148  errors: dict[str, str] = {}
149 
150  if user_input:
151  api_key = user_input[CONF_API_KEY]
152 
153  reauth_entry = self._get_reauth_entry_get_reauth_entry()
154  errors = await validate_input(
155  self.hass,
156  api_key,
157  reauth_entry.data[CONF_FROM],
158  reauth_entry.data[CONF_TO],
159  reauth_entry.data.get(CONF_TIME),
160  reauth_entry.data[CONF_WEEKDAY],
161  reauth_entry.options.get(CONF_FILTER_PRODUCT),
162  )
163  if not errors:
164  return self.async_update_reload_and_abortasync_update_reload_and_abort(
165  reauth_entry,
166  data_updates={CONF_API_KEY: api_key},
167  )
168 
169  return self.async_show_formasync_show_formasync_show_form(
170  step_id="reauth_confirm",
171  data_schema=DATA_SCHEMA_REAUTH,
172  errors=errors,
173  )
174 
175  async def async_step_user(
176  self, user_input: dict[str, Any] | None = None
177  ) -> ConfigFlowResult:
178  """Handle the user step."""
179  errors: dict[str, str] = {}
180 
181  if user_input is not None:
182  api_key: str = user_input[CONF_API_KEY]
183  train_from: str = user_input[CONF_FROM]
184  train_to: str = user_input[CONF_TO]
185  train_time: str | None = user_input.get(CONF_TIME)
186  train_days: list = user_input[CONF_WEEKDAY]
187  filter_product: str | None = user_input[CONF_FILTER_PRODUCT]
188 
189  if filter_product == "":
190  filter_product = None
191 
192  name = f"{train_from} to {train_to}"
193  if train_time:
194  name = f"{train_from} to {train_to} at {train_time}"
195 
196  errors = await validate_input(
197  self.hass,
198  api_key,
199  train_from,
200  train_to,
201  train_time,
202  train_days,
203  filter_product,
204  )
205  if not errors:
206  self._async_abort_entries_match_async_abort_entries_match(
207  {
208  CONF_API_KEY: api_key,
209  CONF_FROM: train_from,
210  CONF_TO: train_to,
211  CONF_TIME: train_time,
212  CONF_WEEKDAY: train_days,
213  CONF_FILTER_PRODUCT: filter_product,
214  }
215  )
216  return self.async_create_entryasync_create_entryasync_create_entry(
217  title=name,
218  data={
219  CONF_API_KEY: api_key,
220  CONF_NAME: name,
221  CONF_FROM: train_from,
222  CONF_TO: train_to,
223  CONF_TIME: train_time,
224  CONF_WEEKDAY: train_days,
225  },
226  options={CONF_FILTER_PRODUCT: filter_product},
227  )
228 
229  return self.async_show_formasync_show_formasync_show_form(
230  step_id="user",
231  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
232  DATA_SCHEMA, user_input or {}
233  ),
234  errors=errors,
235  )
236 
237 
239  """Handle Trafikverket Train options."""
240 
241  async def async_step_init(
242  self, user_input: dict[str, Any] | None = None
243  ) -> ConfigFlowResult:
244  """Manage Trafikverket Train options."""
245  errors: dict[str, Any] = {}
246 
247  if user_input:
248  if not (_filter := user_input.get(CONF_FILTER_PRODUCT)) or _filter == "":
249  user_input[CONF_FILTER_PRODUCT] = None
250  return self.async_create_entryasync_create_entry(data=user_input)
251 
252  return self.async_show_formasync_show_form(
253  step_id="init",
254  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
255  vol.Schema(OPTION_SCHEMA),
256  user_input or self.config_entryconfig_entryconfig_entry.options,
257  ),
258  errors=errors,
259  )
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:146
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:140
TVTrainOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:134
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:177
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:243
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_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)
None config_entry(self, ConfigEntry value)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_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)
dict[str, str] validate_input(HomeAssistant hass, str api_key, str train_from, str train_to, str|None train_time, list[str] weekdays, str|None product_filter)
Definition: config_flow.py:79
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)