Skip to content

Commit

Permalink
Route type handling for bus icon
Browse files Browse the repository at this point in the history
Store proper route-type after selecting, not the generic one, sadly this meant adding route_type to the CONF_ROUTE (future improvement)
From coordinator, use route_type to find appropriate icon and feed that to gtfs_rt for eh jsson containing vehicle locations.
Non-integrtaion steps: use customize_glob to pull the right entutypicture for geo_json that end with 'bus' (or any other route_type textual rep.)
  • Loading branch information
vingerha committed Jan 5, 2025
1 parent 9c8afc5 commit 916b430
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
5 changes: 3 additions & 2 deletions custom_components/gtfs2/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ async def async_step_route(self, user_input: dict | None = None) -> FlowResult:
),
errors=errors,
)
user_input[CONF_ROUTE_TYPE] = user_input.get(CONF_ROUTE,99).split('#')[1]
self._user_inputs.update(user_input)
_LOGGER.debug(f"UserInputs Route: {self._user_inputs}")
return await self.async_step_stops()
Expand All @@ -295,7 +296,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].split(":")[0],
self._user_inputs[CONF_DIRECTION],
)
last_stop = stops[-1:][0]
Expand Down Expand Up @@ -331,7 +332,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].split(":")[0],
self._user_inputs[CONF_DIRECTION],
)
last_stop = stops[-1:][0]
Expand Down
8 changes: 6 additions & 2 deletions custom_components/gtfs2/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
ATTR_DUE_IN,
ATTR_LATITUDE,
ATTR_LONGITUDE,
ATTR_RT_UPDATED_AT
ATTR_RT_UPDATED_AT,
ICON,
ICONS
)
from .gtfs_helper import get_gtfs, get_next_departure, check_datasource_index, create_trip_geojson, check_extracting, get_local_stops_next_departures
from .gtfs_rt_helper import get_next_services, get_rt_alerts
Expand Down Expand Up @@ -72,12 +74,13 @@ async def _async_update_data(self) -> dict[str, str]:
"name": data["name"],
"file": data["file"],
"route_type": data["route_type"],
"route": data["route"],
"extracting": False,
"next_departure": {},
"next_departure_realtime_attr": {},
"alert": {}
}

if check_extracting(self.hass, self._data['gtfs_dir'],self._data['file']):
_LOGGER.debug("Cannot update this sensor as still unpacking: %s", self._data["file"])
previous_data["extracting"] = True
Expand Down Expand Up @@ -122,6 +125,7 @@ async def _async_update_data(self) -> dict[str, str]:
self._headers = None
self._trip_update_url = options.get("trip_update_url", None)
self._vehicle_position_url = options.get("vehicle_position_url", None)
self._icon = ICONS.get(int(self._data["route_type"]), ICON)
self._alerts_url = options.get("alerts_url", None)
if options.get(CONF_API_KEY_LOCATION, None) == "query_string":
if options.get(CONF_API_KEY, None):
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_short_name, r.route_long_name, a.agency_name
SELECT r.route_id, r.route_type, 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])
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
2 changes: 1 addition & 1 deletion custom_components/gtfs2/gtfs_rt_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def get_rt_vehicle_positions(self):
geojson_element["geometry"]["coordinates"].append(vehicle["position"]["longitude"])
geojson_element["geometry"]["coordinates"].append(vehicle["position"]["latitude"])
geojson_element["properties"]["id"] = str(self._route_id) + "(" + str(vehicle["trip"]["direction_id"]) + ")" + str(binascii.crc32((vehicle["trip"]["trip_id"]).encode('utf8')))[-3:]
geojson_element["properties"]["title"] = str(self._route_id) + "(" + str(vehicle["trip"]["direction_id"]) + ")" + str(binascii.crc32((vehicle["trip"]["trip_id"]).encode('utf8')))[-3:]
geojson_element["properties"]["title"] = str(self._route_id) + "(" + str(vehicle["trip"]["direction_id"]) + ")" + str(binascii.crc32((vehicle["trip"]["trip_id"]).encode('utf8')))[-3:] + "_" + self._icon.split(':')[1]
geojson_element["properties"]["trip_id"] = vehicle["trip"]["trip_id"]
geojson_element["properties"]["route_id"] = str(self._route_id)
geojson_element["properties"]["direction_id"] = vehicle["trip"]["direction_id"]
Expand Down

0 comments on commit 916b430

Please sign in to comment.