1 """Config flow for swiss_public_transport."""
6 from opendata_transport
import OpendataTransport
7 from opendata_transport.exceptions
import (
8 OpendataTransportConnectionError,
9 OpendataTransportError,
11 import voluptuous
as vol
43 from .helper
import offset_opendata, unique_id_from_config
45 USER_DATA_SCHEMA = vol.Schema(
47 vol.Required(CONF_START): cv.string,
50 type=TextSelectorType.TEXT,
54 vol.Required(CONF_DESTINATION): cv.string,
55 vol.Optional(CONF_TIME_MODE, default=DEFAULT_TIME_MODE):
SelectSelector(
57 options=TIME_MODE_OPTIONS,
58 mode=SelectSelectorMode.DROPDOWN,
59 translation_key=
"time_mode",
62 vol.Optional(CONF_TIME_STATION, default=DEFAULT_TIME_STATION):
SelectSelector(
64 options=IS_ARRIVAL_OPTIONS,
65 mode=SelectSelectorMode.DROPDOWN,
66 translation_key=
"time_station",
71 ADVANCED_TIME_DATA_SCHEMA = {vol.Optional(CONF_TIME_FIXED):
TimeSelector()}
72 ADVANCED_TIME_OFFSET_DATA_SCHEMA = {vol.Optional(CONF_TIME_OFFSET):
DurationSelector()}
75 _LOGGER = logging.getLogger(__name__)
79 """Swiss public transport config flow."""
84 user_input: dict[str, Any]
87 self, user_input: dict[str, Any] |
None =
None
88 ) -> ConfigFlowResult:
89 """Async user step to set up the connection."""
90 errors: dict[str, str] = {}
91 if user_input
is not None:
92 if CONF_VIA
in user_input
and len(user_input[CONF_VIA]) > MAX_VIA:
93 errors[
"base"] =
"too_many_via_stations"
100 if user_input[CONF_TIME_MODE] ==
"fixed":
102 if user_input[CONF_TIME_MODE] ==
"offset":
116 data_schema=USER_DATA_SCHEMA,
117 suggested_values=user_input,
120 description_placeholders=PLACEHOLDERS,
124 self, time_input: dict[str, Any] |
None =
None
125 ) -> ConfigFlowResult:
126 """Async time step to set up the connection."""
128 CONF_TIME_FIXED, vol.Schema(ADVANCED_TIME_DATA_SCHEMA), time_input
132 self, time_offset_input: dict[str, Any] |
None =
None
133 ) -> ConfigFlowResult:
134 """Async time offset step to set up the connection."""
137 vol.Schema(ADVANCED_TIME_OFFSET_DATA_SCHEMA),
144 time_mode_schema: vol.Schema,
145 time_mode_input: dict[str, Any] |
None =
None,
146 ) -> ConfigFlowResult:
147 """Async time mode step to set up the connection."""
148 errors: dict[str, str] = {}
149 if time_mode_input
is not None:
155 {**self.
user_inputuser_input, **time_mode_input},
156 time_mode_input.get(CONF_TIME_OFFSET),
163 data={**self.
user_inputuser_input, **time_mode_input},
168 data_schema=time_mode_schema,
170 description_placeholders=PLACEHOLDERS,
174 self, input: dict[str, Any], time_offset: dict[str, int] |
None =
None
176 """Fetch the connections and advancedly return an error."""
179 opendata = OpendataTransport(
181 input[CONF_DESTINATION],
183 via=input.get(CONF_VIA),
184 time=input.get(CONF_TIME_FIXED),
188 await opendata.async_get_data()
189 except OpendataTransportConnectionError:
190 return "cannot_connect"
191 except OpendataTransportError:
194 _LOGGER.exception(
"Unknown error")
ConfigFlowResult async_step_time_offset(self, dict[str, Any]|None time_offset_input=None)
str|None fetch_connections(self, dict[str, Any] input, dict[str, int]|None time_offset=None)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_time_fixed(self, dict[str, Any]|None time_input=None)
ConfigFlowResult _async_step_time_mode(self, str step_id, vol.Schema time_mode_schema, dict[str, Any]|None time_mode_input=None)
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)
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)
None offset_opendata(OpendataTransport opendata, dict[str, int] offset)
str unique_id_from_config(MappingProxyType[str, Any]|dict[str, Any] config)
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)