Skip to content

Commit

Permalink
Fix issue when RT data only supplies stop_sequence (not stop_id)
Browse files Browse the repository at this point in the history
  • Loading branch information
vingerha committed Dec 31, 2024
1 parent 798b820 commit 9c8afc5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions custom_components/gtfs2/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ async def _async_update_data(self) -> dict[str, str]:
_LOGGER.debug("GTFS RT: no route_id in sensor data, using route_id from config_entry")
self._route_id = data["route"].split(": ")[0]
self._stop_id = self._data["next_departure"]["origin_stop_id"].split(": ")[0]
self._stop_sequence = self._data["next_departure"]["origin_stop_sequence"]
self._destination_id = data["destination"].split(": ")[0]
self._trip_id = self._data.get('next_departure', {}).get('trip_id', None)
self._direction = data["direction"]
Expand Down
10 changes: 6 additions & 4 deletions custom_components/gtfs2/gtfs_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ def get_next_departure(self):
"first": item["first"],
"last": item["last"],
"origin_stop_id": item["origin_stop_id"],
"origin_stop_sequence": item["origin_stop_sequence"],
"origin_stop_name": item["origin_stop_name"],
"departure_time": depart_time,
"arrival_time": arrival_time,
Expand Down Expand Up @@ -900,7 +901,7 @@ def get_local_stops_next_departures(self):
_LOGGER.debug("Query params: Latitude %s - Longitude %s - Timerange %s - Timerange_history %s - Radius %s - Now: %s", latitude, longitude, time_range, time_range_history, radius, now)
sql_query = f"""
SELECT * FROM (
SELECT stop.stop_id, stop.stop_name,stop.stop_lat as latitude, stop.stop_lon as longitude, stop.stop_timezone as stop_timezone, agency.agency_timezone as agency_timezone, trip.trip_id, trip.trip_headsign, trip.direction_id, time(st.departure_time) as departure_time,
SELECT stop.stop_id, stop.stop_name,stop.stop_lat as latitude, stop.stop_lon as longitude, stop.stop_timezone as stop_timezone, agency.agency_timezone as agency_timezone, trip.trip_id, trip.trip_headsign, trip.direction_id, time(st.departure_time) as departure_time,st.stop_sequence as stop_sequence,
route.route_long_name,route.route_short_name,route.route_type,
calendar.{now.strftime("%A").lower()} AS today,
{tomorrow_select}
Expand Down Expand Up @@ -929,7 +930,7 @@ def get_local_stops_next_departures(self):
)
UNION ALL
SELECT * FROM (
SELECT stop.stop_id, stop.stop_name,stop.stop_lat as latitude, stop.stop_lon as longitude, stop.stop_timezone as stop_timezone, agency.agency_timezone as agency_timezone, trip.trip_id, trip.trip_headsign, trip.direction_id, time(st.departure_time) as departure_time,
SELECT stop.stop_id, stop.stop_name,stop.stop_lat as latitude, stop.stop_lon as longitude, stop.stop_timezone as stop_timezone, agency.agency_timezone as agency_timezone, trip.trip_id, trip.trip_headsign, trip.direction_id, time(st.departure_time) as departure_time,st.stop_sequence as stop_sequence,
route.route_long_name,route.route_short_name,route.route_type,
'0' AS today,
{tomorrow_select2}
Expand Down Expand Up @@ -1029,7 +1030,7 @@ def get_local_stops_next_departures(self):
local_stops_list.append(prev_entry)
timetable = []

entry = {"stop_id": row['stop_id'], "stop_name": row['stop_name'], "latitude": row['latitude'], "longitude": row['longitude'], "departure": timetable, "offset": offset}
entry = {"stop_id": row['stop_id'], "stop_name": row['stop_name'], "stop_sequence": row['stop_sequence'], "latitude": row['latitude'], "longitude": row['longitude'], "departure": timetable, "offset": offset}
self._icon = ICONS.get(row['route_type'], ICON)

if row["today"] == 1 or (row["today_cd"] == 1 and row["start_date"] == row["calendar_date"]):
Expand All @@ -1042,6 +1043,7 @@ def get_local_stops_next_departures(self):
self._route = row['route_id']
self._route_id = row['route_id']
self._stop_id = row['stop_id']
self._stop_sequence = row['stop_sequence']
_LOGGER.debug("Row departure_time: %s", row["departure_time"])
# collect departure time from row, using agency timezone as basis, then transforming it to the stop-specific timezone (based on Amtrak)
self._departure_datetime = datetime.datetime.strptime(now_date + " " + row["departure_time"], "%Y-%m-%d %H:%M:%S").replace(tzinfo=timezone_agency).astimezone(tz=timezone_stop)
Expand All @@ -1056,7 +1058,7 @@ def get_local_stops_next_departures(self):
# Find RT if configured
if self._realtime:
self._get_next_service = {}
_LOGGER.debug("Find rt for local stop route: %s - direction: %s - stop: %s", self._route , self._direction, self._stop_id)
_LOGGER.debug("Find rt for local stop route: %s - direction: %s - stop: %s - stop_sequence: %s", self._route , self._direction, self._stop_id, self._stop_sequence)
next_service = get_rt_route_trip_statuses(self)
_LOGGER.debug("Next service: %s", next_service)
if next_service:
Expand Down
8 changes: 7 additions & 1 deletion custom_components/gtfs2/gtfs_rt_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,14 @@ def get_rt_route_trip_statuses(self):

for stop in entity["trip_update"]["stop_time_update"]:
stop_id = stop["stop_id"]
if stop_id == self._stop_id:
stop_sequence = stop["stop_sequence"]
if stop_id == self._stop_id or (stop_id == "" and stop_sequence == self._stop_sequence):
_LOGGER.debug("Stop found: %s", stop)
# if the data does not contain a stop_id but only a stop_sequence, assume stop_id being the correct stop based on sequence
# this does not have to be always correct but best-guess
if stop_id == "":
stop_id = self._stop_id

if route_id not in departure_times:
departure_times[route_id] = {}

Expand Down
3 changes: 2 additions & 1 deletion custom_components/gtfs2/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ def _update_attrs(self): # noqa: C901 PLR0911
else:
self._attributes["origin_station_stop_name"] = self._departure.get("origin_stop_name", None)
self._attributes["origin_station_stop_id"] = self._departure.get("origin_stop_id", None)
self._attributes["origin_station_stop_sequence"] = self._departure.get("origin_stop_sequence", None)

key = "destination_station_stop_id"
# exclude check if route_type =2 (trains) as no ID is used
Expand All @@ -330,7 +331,7 @@ def _update_attrs(self): # noqa: C901 PLR0911
)
else:
self._attributes["destination_station_stop_name"] = self._departure.get("destination_stop_name", None)
self._attributes["destination_station_stop_id"] = self._departure.get("destination_stop_id", None)
self._attributes["destination_station_stop_id"] = self._departure.get("destination_stop_id", None)

# Manage Route metadata
key = "route_id"
Expand Down

0 comments on commit 9c8afc5

Please sign in to comment.