Skip to content

Commit

Permalink
Improve on selecting the route & route_type
Browse files Browse the repository at this point in the history
By using a different selector, allowing to use value and label and not show the route-type in the list of routes for the user
  • Loading branch information
vingerha committed Jan 6, 2025
1 parent 916b430 commit 4bf7cba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
19 changes: 12 additions & 7 deletions custom_components/gtfs2/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ async def async_step_agency(self, user_input: dict | None = None) -> FlowResult:
return await self.async_step_route_type()

async def async_step_route_type(self, user_input: dict | None = None) -> FlowResult:
"""Handle the route_type."""
"""Handle the dummy route_type to split between train (different mechanism) and rest."""
errors: dict[str, str] = {}
if user_input is None:
return self.async_show_form(
Expand All @@ -259,7 +259,7 @@ async def async_step_route_type(self, user_input: dict | None = None) -> FlowRes
return await self.async_step_route()

async def async_step_route(self, user_input: dict | None = None) -> FlowResult:
"""Handle the route."""
"""Handle the route and reset the route_type to the proper one."""
errors: dict[str, str] = {}
check_data = await self._check_data(self._user_inputs)
_LOGGER.debug("Source check data: %s", check_data)
Expand All @@ -272,19 +272,24 @@ async def async_step_route(self, user_input: dict | None = None) -> FlowResult:
self._user_inputs,
False,
)

if user_input is None:
route_list = [
selector.SelectOptionDict(value=r, label=r.split('#')[1])
for r in get_route_list(self._pygtfs, self._user_inputs)
]
return self.async_show_form(
step_id="route",
data_schema=vol.Schema(
{
vol.Required(CONF_ROUTE, default = ""): selector.SelectSelector(selector.SelectSelectorConfig(options = get_route_list(self._pygtfs, self._user_inputs), translation_key="route_type",custom_value=True)),
vol.Required(CONF_ROUTE, default = ""): selector.SelectSelector(selector.SelectSelectorConfig(options=route_list, translation_key="route",custom_value=True)),
vol.Required(CONF_DIRECTION): selector.SelectSelector(selector.SelectSelectorConfig(options=["0", "1"], translation_key="direction")),
},
),
errors=errors,
)
user_input[CONF_ROUTE_TYPE] = user_input.get(CONF_ROUTE,99).split('#')[1]
user_input[CONF_ROUTE_TYPE] = user_input.get(CONF_ROUTE).split('#')[0]
user_input[CONF_ROUTE] = user_input.get(CONF_ROUTE).split('#')[1].split(":")[0]

self._user_inputs.update(user_input)
_LOGGER.debug(f"UserInputs Route: {self._user_inputs}")
return await self.async_step_stops()
Expand All @@ -296,7 +301,7 @@ async def async_step_stops(self, user_input: dict | None = None) -> FlowResult:
try:
stops = get_stop_list(
self._pygtfs,
self._user_inputs[CONF_ROUTE].split(":")[0],
self._user_inputs[CONF_ROUTE],
self._user_inputs[CONF_DIRECTION],
)
last_stop = stops[-1:][0]
Expand Down Expand Up @@ -332,7 +337,7 @@ async def async_step_stops_retry(self, user_input: dict | None = None) -> FlowRe
try:
stops = get_stop_list(
self._pygtfs,
self._user_inputs[CONF_ROUTE].split(":")[0],
self._user_inputs[CONF_ROUTE],
self._user_inputs[CONF_DIRECTION],
)
last_stop = stops[-1:][0]
Expand Down
4 changes: 2 additions & 2 deletions custom_components/gtfs2/gtfs_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ def get_route_list(schedule, data):
if data["route_type"] != "99":
route_type_where = f"and route_type = {data['route_type']}"
sql_routes = f"""
SELECT r.route_id, r.route_type, r.route_short_name, r.route_long_name, a.agency_name
SELECT r.route_type, r.route_id, r.route_short_name, r.route_long_name, a.agency_name
from routes r
left join agency a on a.agency_id = r.agency_id
where 1=1
Expand All @@ -602,7 +602,7 @@ def get_route_list(schedule, data):
row = row_cursor._asdict()
routes_list.append(list(row_cursor))
for x in routes_list:
val = str(x[0]) + ":#" + str(x[1]) + "# (" + str(x[2]) + " - " + str(x[3]) + ") " + str(x[4])
val = str(x[0]) + "#" + str(x[1]) + ": (" + str(x[2]) + " - " + str(x[3]) + ") " + str(x[4])
routes.append(val)
_LOGGER.debug(f"routes: {routes}")
return routes
Expand Down

0 comments on commit 4bf7cba

Please sign in to comment.