Skip to content

Commit

Permalink
Fix incorrect and duplicate timetable data
Browse files Browse the repository at this point in the history
  • Loading branch information
warrior25 committed Apr 6, 2024
1 parent c0ea891 commit e6185ce
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
3 changes: 0 additions & 3 deletions custom_components/nysse/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
DEFAULT_ICON = "mdi:bus-clock"
TRAM_LINES = ["1", "3"]

AIMED_DEPARTURE_TIME = "aimedDepartureTime"
EXPECTED_DEPARTURE_TIME = "expectedDepartureTime"

STOP_URL = "https://data.itsfactory.fi/journeys/api/1/stop-monitoring?stops={0}"
SERVICE_ALERTS_URL = (
"https://data.itsfactory.fi/journeys/api/1/gtfs-rt/service-alerts/json"
Expand Down
5 changes: 3 additions & 2 deletions custom_components/nysse/fetch_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,12 @@ async def get_stop_times(stop_id, route_ids, amount, from_time):
WHERE stop_id = ?
AND trips.route_id IN ({','.join(['?']*len(route_ids))})
AND calendar.{weekday} = '1'
AND calendar.start_date < ?
AND calendar.start_date <= ?
AND calendar.end_date >= ?
AND departure_time > ?
LIMIT ?
""",
[stop_id, *route_ids, today, from_time.strftime("%H:%M:%S"), amount],
[stop_id, *route_ids, today, today, from_time.strftime("%H:%M:%S"), amount],
)
stop_times += cursor.fetchall()
if len(stop_times) >= amount:
Expand Down
22 changes: 7 additions & 15 deletions custom_components/nysse/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
import homeassistant.util.dt as dt_util

from .const import (
AIMED_DEPARTURE_TIME,
DEFAULT_ICON,
DEFAULT_MAX,
DEFAULT_TIMELIMIT,
DOMAIN,
EXPECTED_DEPARTURE_TIME,
PLATFORM_NAME,
SERVICE_ALERTS_URL,
STOP_URL,
Expand Down Expand Up @@ -136,8 +134,8 @@ def _format_departures(self, departures):
"trip_headsign": self._get_stop_name(
departure["destinationShortName"]
),
"departure_time": departure["call"][EXPECTED_DEPARTURE_TIME],
"aimed_departure_time": departure["call"][AIMED_DEPARTURE_TIME],
"departure_time": departure["call"]["expectedDepartureTime"],
"aimed_departure_time": departure["call"]["aimedDepartureTime"],
"realtime": True,
}
if (
Expand Down Expand Up @@ -173,21 +171,15 @@ async def async_update(self) -> None:
departures = await self._fetch_departures()
departures = self._remove_unwanted_departures(departures)
if len(departures) < self._max_items:
from_time = self._last_update_time + timedelta(minutes=self._timelimit)
if len(departures) > 0:
from_time = parser.parse(departures[-1]["aimed_departure_time"])
self._journeys = await get_stop_times(
self._stop_code,
self._lines,
self._max_items - len(departures),
self._last_update_time + timedelta(minutes=self._timelimit),
from_time,
)
for journey in self._journeys[:]:
for departure in departures:
departure_time = parser.parse(departure["aimed_departure_time"])
journey_time = parser.parse(journey["departure_time"])
if (
journey_time == departure_time
and journey["route_id"] == departure["route_id"]
):
self._journeys.remove(journey)
else:
self._journeys.clear()

Expand All @@ -214,7 +206,7 @@ def _data_to_display_format(self, data):
"realtime": item["realtime"] if "realtime" in item else False,
}
formatted_data.append(departure)
return formatted_data
return sorted(formatted_data, key=lambda x: x["time_to_station"])

def _get_line_icon(self, line_no):
if line_no in TRAM_LINES:
Expand Down

0 comments on commit e6185ce

Please sign in to comment.