1 """Config flow to configure the Nextbus integration."""
3 from collections
import Counter
6 from py_nextbus
import NextBusClient
7 import voluptuous
as vol
18 from .const
import CONF_AGENCY, CONF_ROUTE, DOMAIN
19 from .util
import listify
21 _LOGGER = logging.getLogger(__name__)
30 for key, value
in options.items()
32 key=
lambda o: o[
"label"],
34 mode=SelectSelectorMode.DROPDOWN,
40 return {a[
"id"]: a[
"name"]
for a
in client.agencies()}
44 return {a[
"id"]: a[
"title"]
for a
in client.routes(agency_tag)}
48 client: NextBusClient, agency_tag: str, route_tag: str
50 route_config = client.route_details(route_tag, agency_tag)
51 stop_ids = {a[
"id"]: a[
"name"]
for a
in route_config[
"stops"]}
52 title_counts = Counter(stop_ids.values())
54 stop_directions: dict[str, str] = {}
55 for direction
in listify(route_config[
"directions"]):
56 if not direction[
"useForUi"]:
58 for stop
in direction[
"stops"]:
59 stop_directions[stop] = direction[
"name"]
62 for stop_id, title
in stop_ids.items():
63 if title_counts[title] > 1:
64 stop_ids[stop_id] = f
"{title} ({stop_directions.get(stop_id, stop_id)})"
70 return f
"{data[CONF_AGENCY]}_{data[CONF_ROUTE]}_{data[CONF_STOP]}"
74 """Handle Nextbus configuration."""
78 _agency_tags: dict[str, str]
79 _route_tags: dict[str, str]
80 _stop_tags: dict[str, str]
83 """Initialize NextBus config flow."""
84 self.data: dict[str, str] = {}
89 user_input: dict[str, str] |
None =
None,
90 ) -> ConfigFlowResult:
91 """Handle a flow initiated by the user."""
96 user_input: dict[str, str] |
None =
None,
97 ) -> ConfigFlowResult:
99 if user_input
is not None:
100 self.data[CONF_AGENCY] = user_input[CONF_AGENCY]
104 self.
_agency_tags_agency_tags = await self.hass.async_add_executor_job(
105 _get_agency_tags, self.
_client_client
110 data_schema=vol.Schema(
121 user_input: dict[str, str] |
None =
None,
122 ) -> ConfigFlowResult:
124 if user_input
is not None:
125 self.data[CONF_ROUTE] = user_input[CONF_ROUTE]
129 self.
_route_tags_route_tags = await self.hass.async_add_executor_job(
130 _get_route_tags, self.
_client_client, self.data[CONF_AGENCY]
135 data_schema=vol.Schema(
146 user_input: dict[str, str] |
None =
None,
147 ) -> ConfigFlowResult:
150 if user_input
is not None:
151 self.data[CONF_STOP] = user_input[CONF_STOP]
156 agency_tag = self.data[CONF_AGENCY]
157 route_tag = self.data[CONF_ROUTE]
158 stop_tag = self.data[CONF_STOP]
161 route_name = self.
_route_tags_route_tags[route_tag]
162 stop_name = self.
_stop_tags_stop_tags[stop_tag]
165 title=f
"{agency_name} {route_name} {stop_name}",
169 self.
_stop_tags_stop_tags = await self.hass.async_add_executor_job(
172 self.data[CONF_AGENCY],
173 self.data[CONF_ROUTE],
178 data_schema=vol.Schema(
ConfigFlowResult async_step_stop(self, dict[str, str]|None user_input=None)
ConfigFlowResult async_step_user(self, dict[str, str]|None user_input=None)
ConfigFlowResult async_step_agency(self, dict[str, str]|None user_input=None)
ConfigFlowResult async_step_route(self, dict[str, str]|None user_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)
_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] _get_route_tags(NextBusClient client, str agency_tag)
SelectSelector _dict_to_select_selector(dict[str, str] options)
str _unique_id_from_data(dict[str, str] data)
dict[str, str] _get_stop_tags(NextBusClient client, str agency_tag, str route_tag)
dict[str, str] _get_agency_tags(NextBusClient client)
list[Any] listify(Any maybe_list)