Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Waze Travel Time integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 
10  SOURCE_RECONFIGURE,
11  ConfigEntry,
12  ConfigFlow,
13  ConfigFlowResult,
14  OptionsFlow,
15 )
16 from homeassistant.const import CONF_NAME, CONF_REGION
17 from homeassistant.core import HomeAssistant, callback
19  BooleanSelector,
20  SelectSelector,
21  SelectSelectorConfig,
22  SelectSelectorMode,
23  TextSelector,
24  TextSelectorConfig,
25  TextSelectorType,
26 )
27 from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
28 
29 from .const import (
30  CONF_AVOID_FERRIES,
31  CONF_AVOID_SUBSCRIPTION_ROADS,
32  CONF_AVOID_TOLL_ROADS,
33  CONF_DESTINATION,
34  CONF_EXCL_FILTER,
35  CONF_INCL_FILTER,
36  CONF_ORIGIN,
37  CONF_REALTIME,
38  CONF_UNITS,
39  CONF_VEHICLE_TYPE,
40  DEFAULT_FILTER,
41  DEFAULT_NAME,
42  DEFAULT_OPTIONS,
43  DOMAIN,
44  IMPERIAL_UNITS,
45  REGIONS,
46  UNITS,
47  VEHICLE_TYPES,
48 )
49 from .helpers import is_valid_config_entry
50 
51 OPTIONS_SCHEMA = vol.Schema(
52  {
53  vol.Optional(CONF_INCL_FILTER): TextSelector(
55  type=TextSelectorType.TEXT,
56  multiple=True,
57  ),
58  ),
59  vol.Optional(CONF_EXCL_FILTER): TextSelector(
61  type=TextSelectorType.TEXT,
62  multiple=True,
63  ),
64  ),
65  vol.Optional(CONF_REALTIME): BooleanSelector(),
66  vol.Required(CONF_VEHICLE_TYPE): SelectSelector(
68  options=VEHICLE_TYPES,
69  mode=SelectSelectorMode.DROPDOWN,
70  translation_key=CONF_VEHICLE_TYPE,
71  sort=True,
72  )
73  ),
74  vol.Required(CONF_UNITS): SelectSelector(
76  options=UNITS,
77  mode=SelectSelectorMode.DROPDOWN,
78  translation_key=CONF_UNITS,
79  sort=True,
80  )
81  ),
82  vol.Optional(CONF_AVOID_TOLL_ROADS): BooleanSelector(),
83  vol.Optional(CONF_AVOID_SUBSCRIPTION_ROADS): BooleanSelector(),
84  vol.Optional(CONF_AVOID_FERRIES): BooleanSelector(),
85  }
86 )
87 
88 CONFIG_SCHEMA = vol.Schema(
89  {
90  vol.Required(CONF_NAME, default=DEFAULT_NAME): TextSelector(),
91  vol.Required(CONF_ORIGIN): TextSelector(),
92  vol.Required(CONF_DESTINATION): TextSelector(),
93  vol.Required(CONF_REGION): SelectSelector(
95  options=REGIONS,
96  mode=SelectSelectorMode.DROPDOWN,
97  translation_key=CONF_REGION,
98  sort=True,
99  )
100  ),
101  }
102 )
103 
104 
105 def default_options(hass: HomeAssistant) -> dict[str, str | bool | list[str]]:
106  """Get the default options."""
107  defaults = DEFAULT_OPTIONS.copy()
108  if hass.config.units is US_CUSTOMARY_SYSTEM:
109  defaults[CONF_UNITS] = IMPERIAL_UNITS
110  return defaults
111 
112 
114  """Handle an options flow for Waze Travel Time."""
115 
116  async def async_step_init(self, user_input=None) -> ConfigFlowResult:
117  """Handle the initial step."""
118  if user_input is not None:
119  if user_input.get(CONF_INCL_FILTER) is None:
120  user_input[CONF_INCL_FILTER] = DEFAULT_FILTER
121  if user_input.get(CONF_EXCL_FILTER) is None:
122  user_input[CONF_EXCL_FILTER] = DEFAULT_FILTER
123  return self.async_create_entryasync_create_entry(
124  title="",
125  data=user_input,
126  )
127 
128  return self.async_show_formasync_show_form(
129  step_id="init",
130  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
131  OPTIONS_SCHEMA, self.config_entryconfig_entryconfig_entry.options
132  ),
133  )
134 
135 
136 class WazeConfigFlow(ConfigFlow, domain=DOMAIN):
137  """Handle a config flow for Waze Travel Time."""
138 
139  VERSION = 2
140 
141  @staticmethod
142  @callback
144  config_entry: ConfigEntry,
145  ) -> WazeOptionsFlow:
146  """Get the options flow for this handler."""
147  return WazeOptionsFlow()
148 
149  async def async_step_user(
150  self, user_input: dict[str, Any] | None = None
151  ) -> ConfigFlowResult:
152  """Handle the initial step."""
153  errors = {}
154  user_input = user_input or {}
155 
156  if user_input:
157  user_input[CONF_REGION] = user_input[CONF_REGION].upper()
158  if await is_valid_config_entry(
159  self.hass,
160  user_input[CONF_ORIGIN],
161  user_input[CONF_DESTINATION],
162  user_input[CONF_REGION],
163  ):
164  if self.sourcesourcesourcesource == SOURCE_RECONFIGURE:
165  return self.async_update_reload_and_abortasync_update_reload_and_abort(
166  self._get_reconfigure_entry_get_reconfigure_entry(),
167  title=user_input[CONF_NAME],
168  data=user_input,
169  )
170  return self.async_create_entryasync_create_entryasync_create_entry(
171  title=user_input.get(CONF_NAME, DEFAULT_NAME),
172  data=user_input,
173  options=default_options(self.hass),
174  )
175 
176  # If we get here, it's because we couldn't connect
177  errors["base"] = "cannot_connect"
178  user_input[CONF_REGION] = user_input[CONF_REGION].lower()
179 
180  return self.async_show_formasync_show_formasync_show_form(
181  step_id="user",
182  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(CONFIG_SCHEMA, user_input),
183  errors=errors,
184  )
185 
187  self, user_input: dict[str, Any] | None = None
188  ) -> ConfigFlowResult:
189  """Handle reconfiguration."""
190  data = self._get_reconfigure_entry_get_reconfigure_entry().data.copy()
191  data[CONF_REGION] = data[CONF_REGION].lower()
192 
193  return self.async_show_formasync_show_formasync_show_form(
194  step_id="user",
195  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(CONFIG_SCHEMA, data),
196  )
WazeOptionsFlow async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:145
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:151
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:188
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)
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)
str|None source(self)
dict[str, str|bool|list[str]] default_options(HomeAssistant hass)
Definition: config_flow.py:105
bool is_valid_config_entry(HomeAssistant hass, str origin, str destination, str region)
Definition: helpers.py:16