1 """Config flow for HERE Travel Time integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
9 from here_routing
import (
12 HERERoutingUnauthorizedError,
16 from here_transit
import HERETransitError
17 import voluptuous
as vol
45 CONF_DESTINATION_ENTITY_ID,
46 CONF_DESTINATION_LATITUDE,
47 CONF_DESTINATION_LONGITUDE,
49 CONF_ORIGIN_ENTITY_ID,
51 CONF_ORIGIN_LONGITUDE,
62 _LOGGER = logging.getLogger(__name__)
65 CONF_ROUTE_MODE: ROUTE_MODE_FASTEST,
66 CONF_ARRIVAL_TIME:
None,
67 CONF_DEPARTURE_TIME:
None,
72 """Validate the user input allows us to connect."""
73 known_working_origin = Place(latitude=38.9, longitude=-77.04833)
74 known_working_destination = Place(latitude=39.0, longitude=-77.1)
76 await HERERoutingApi(api_key).route(
77 origin=known_working_origin,
78 destination=known_working_destination,
79 transport_mode=TransportMode.CAR,
84 """Get a populated schema or default."""
85 travel_mode = data.get(CONF_MODE, TRAVEL_MODE_CAR)
86 if travel_mode ==
"publicTransportTimeTable":
87 travel_mode = TRAVEL_MODE_PUBLIC
91 CONF_NAME, default=data.get(CONF_NAME, DEFAULT_NAME)
93 vol.Required(CONF_API_KEY, default=data.get(CONF_API_KEY)): cv.string,
95 CONF_MODE, default=data.get(CONF_MODE, TRAVEL_MODE_CAR)
96 ): vol.In(TRAVEL_MODES),
102 """Handle a config flow for HERE Travel Time."""
107 """Init Config Flow."""
108 self._config: dict[str, Any] = {}
113 config_entry: ConfigEntry,
114 ) -> HERETravelTimeOptionsFlow:
115 """Get the options flow."""
119 self, user_input: dict[str, Any] |
None =
None
120 ) -> ConfigFlowResult:
121 """Handle the initial step."""
123 user_input = user_input
or {}
127 except HERERoutingUnauthorizedError:
128 errors[
"base"] =
"invalid_auth"
129 except (HERERoutingError, HERETransitError):
130 _LOGGER.exception(
"Unexpected exception")
131 errors[
"base"] =
"unknown"
133 self._config[CONF_NAME] = user_input[CONF_NAME]
134 self._config[CONF_API_KEY] = user_input[CONF_API_KEY]
135 self._config[CONF_MODE] = user_input[CONF_MODE]
142 self, user_input: dict[str, Any] |
None =
None
143 ) -> ConfigFlowResult:
144 """Handle reconfiguration."""
151 """Show the origin menu."""
153 step_id=
"origin_menu",
154 menu_options=[
"origin_coordinates",
"origin_entity"],
158 self, user_input: dict[str, Any] |
None =
None
159 ) -> ConfigFlowResult:
160 """Configure origin by using gps coordinates."""
161 if user_input
is not None:
162 self._config[CONF_ORIGIN_LATITUDE] = user_input[CONF_ORIGIN][CONF_LATITUDE]
163 self._config[CONF_ORIGIN_LONGITUDE] = user_input[CONF_ORIGIN][
167 self._config.pop(CONF_ORIGIN_ENTITY_ID,
None)
179 CONF_LATITUDE: self._config.
get(CONF_ORIGIN_LATITUDE)
180 or self.hass.config.latitude,
181 CONF_LONGITUDE: self._config.
get(CONF_ORIGIN_LONGITUDE)
182 or self.hass.config.longitude,
189 self, user_input: dict[str, Any] |
None =
None
190 ) -> ConfigFlowResult:
191 """Configure origin by using an entity."""
192 if user_input
is not None:
193 self._config[CONF_ORIGIN_ENTITY_ID] = user_input[CONF_ORIGIN_ENTITY_ID]
195 self._config.pop(CONF_ORIGIN_LATITUDE,
None)
196 self._config.pop(CONF_ORIGIN_LONGITUDE,
None)
202 CONF_ORIGIN_ENTITY_ID,
206 {CONF_ORIGIN_ENTITY_ID: self._config.
get(CONF_ORIGIN_ENTITY_ID)},
211 """Show the destination menu."""
213 step_id=
"destination_menu",
214 menu_options=[
"destination_coordinates",
"destination_entity"],
219 user_input: dict[str, Any] |
None =
None,
220 ) -> ConfigFlowResult:
221 """Configure destination by using gps coordinates."""
222 if user_input
is not None:
223 self._config[CONF_DESTINATION_LATITUDE] = user_input[CONF_DESTINATION][
226 self._config[CONF_DESTINATION_LONGITUDE] = user_input[CONF_DESTINATION][
230 self._config.pop(CONF_DESTINATION_ENTITY_ID,
None)
234 title=self._config[CONF_NAME],
238 title=self._config[CONF_NAME],
240 options=DEFAULT_OPTIONS,
252 CONF_LATITUDE: self._config.
get(CONF_DESTINATION_LATITUDE)
253 or self.hass.config.latitude,
254 CONF_LONGITUDE: self._config.
get(CONF_DESTINATION_LONGITUDE)
255 or self.hass.config.longitude,
260 step_id=
"destination_coordinates", data_schema=schema
265 user_input: dict[str, Any] |
None =
None,
266 ) -> ConfigFlowResult:
267 """Configure destination by using an entity."""
268 if user_input
is not None:
269 self._config[CONF_DESTINATION_ENTITY_ID] = user_input[
270 CONF_DESTINATION_ENTITY_ID
273 self._config.pop(CONF_DESTINATION_LATITUDE,
None)
274 self._config.pop(CONF_DESTINATION_LONGITUDE,
None)
280 title=self._config[CONF_NAME],
282 options=DEFAULT_OPTIONS,
288 CONF_DESTINATION_ENTITY_ID,
292 {CONF_DESTINATION_ENTITY_ID: self._config.
get(CONF_DESTINATION_ENTITY_ID)},
298 """Handle HERE Travel Time options."""
301 """Initialize HERE Travel Time options flow."""
302 self.
_config_config: dict[str, Any] = {}
305 self, user_input: dict[str, Any] |
None =
None
306 ) -> ConfigFlowResult:
307 """Manage the HERE Travel Time options."""
308 if user_input
is not None:
318 CONF_ROUTE_MODE, DEFAULT_OPTIONS[CONF_ROUTE_MODE]
320 ): vol.In(ROUTE_MODES),
325 CONF_ROUTE_MODE, DEFAULT_OPTIONS[CONF_ROUTE_MODE]
330 return self.
async_show_formasync_show_form(step_id=
"init", data_schema=schema)
333 """Show the time menu."""
336 menu_options=[
"departure_time",
"arrival_time",
"no_time"],
340 self, user_input: dict[str, Any] |
None =
None
341 ) -> ConfigFlowResult:
342 """Create Options Entry."""
346 self, user_input: dict[str, Any] |
None =
None
347 ) -> ConfigFlowResult:
348 """Configure arrival time."""
349 if user_input
is not None:
350 self.
_config_config[CONF_ARRIVAL_TIME] = user_input[CONF_ARRIVAL_TIME]
355 {vol.Required(CONF_ARRIVAL_TIME, default=
"00:00:00"):
TimeSelector()}
357 {CONF_ARRIVAL_TIME:
"00:00:00"},
360 return self.
async_show_formasync_show_form(step_id=
"arrival_time", data_schema=schema)
363 self, user_input: dict[str, Any] |
None =
None
364 ) -> ConfigFlowResult:
365 """Configure departure time."""
366 if user_input
is not None:
367 self.
_config_config[CONF_DEPARTURE_TIME] = user_input[CONF_DEPARTURE_TIME]
372 {vol.Required(CONF_DEPARTURE_TIME, default=
"00:00:00"):
TimeSelector()}
374 {CONF_DEPARTURE_TIME:
"00:00:00"},
377 return self.
async_show_formasync_show_form(step_id=
"departure_time", data_schema=schema)
ConfigFlowResult async_step_destination_coordinates(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_origin_menu(self, None _=None)
HERETravelTimeOptionsFlow async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_origin_coordinates(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_origin_entity(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_destination_menu(self, None _=None)
ConfigFlowResult async_step_destination_entity(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_time_menu(self, None _=None)
ConfigFlowResult async_step_departure_time(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_arrival_time(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_no_time(self, dict[str, Any]|None user_input=None)
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)
ConfigEntry _get_reconfigure_entry(self)
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)
ConfigEntry config_entry(self)
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_show_menu(self, *str|None step_id=None, Container[str] menu_options, Mapping[str, str]|None description_placeholders=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)
web.Response get(self, web.Request request, str config_key)
vol.Schema get_user_step_schema(Mapping[str, Any] data)
None async_validate_api_key(str api_key)