diff --git a/custom_components/gtfs2/config_flow.py b/custom_components/gtfs2/config_flow.py index fbe9b09..54d9018 100644 --- a/custom_components/gtfs2/config_flow.py +++ b/custom_components/gtfs2/config_flow.py @@ -207,6 +207,7 @@ async def _check_config(self, data): "name": data["name"], "next_departure": None, } + try: self._data["next_departure"] = await self.hass.async_add_executor_job( get_next_departure, self._data diff --git a/custom_components/gtfs2/gtfs_helper.py b/custom_components/gtfs2/gtfs_helper.py index 972ae84..d8148c5 100644 --- a/custom_components/gtfs2/gtfs_helper.py +++ b/custom_components/gtfs2/gtfs_helper.py @@ -34,6 +34,7 @@ def get_next_departure(data): limit = 24 * 60 * 60 * 2 tomorrow_select = tomorrow_where = tomorrow_order = "" if include_tomorrow: + _LOGGER.debug("Include Tomorrow") limit = int(limit / 2 * 3) tomorrow_name = tomorrow.strftime("%A").lower() tomorrow_select = f"calendar.{tomorrow_name} AS tomorrow," @@ -63,7 +64,8 @@ def get_next_departure(data): calendar.{now.strftime("%A").lower()} AS today, {tomorrow_select} calendar.start_date AS start_date, - calendar.end_date AS end_date + calendar.end_date AS end_date, + 0 as today_cd FROM trips trip INNER JOIN calendar calendar ON trip.service_id = calendar.service_id @@ -76,21 +78,59 @@ def get_next_departure(data): INNER JOIN stops end_station ON destination_stop_time.stop_id = end_station.stop_id INNER JOIN routes route - ON route.route_id = trip.route_id - WHERE (calendar.{yesterday.strftime("%A").lower()} = 1 - OR calendar.{now.strftime("%A").lower()} = 1 - {tomorrow_where} - ) - AND start_station.stop_id = :origin_station_id + ON route.route_id = trip.route_id + LEFT OUTER JOIN calendar_dates calendar_date_today + on trip.service_id = calendar_date_today.service_id + WHERE start_station.stop_id = :origin_station_id AND end_station.stop_id = :end_station_id AND origin_stop_sequence < dest_stop_sequence AND calendar.start_date <= :today AND calendar.end_date >= :today - ORDER BY calendar.{yesterday.strftime("%A").lower()} DESC, - calendar.{now.strftime("%A").lower()} DESC, - {tomorrow_order} - origin_stop_time.departure_time - LIMIT :limit + UNION ALL + SELECT trip.trip_id, trip.route_id,route.route_long_name, + time(origin_stop_time.arrival_time) AS origin_arrival_time, + time(origin_stop_time.departure_time) AS origin_depart_time, + date(origin_stop_time.departure_time) AS origin_depart_date, + origin_stop_time.drop_off_type AS origin_drop_off_type, + origin_stop_time.pickup_type AS origin_pickup_type, + origin_stop_time.shape_dist_traveled AS origin_dist_traveled, + origin_stop_time.stop_headsign AS origin_stop_headsign, + origin_stop_time.stop_sequence AS origin_stop_sequence, + origin_stop_time.timepoint AS origin_stop_timepoint, + time(destination_stop_time.arrival_time) AS dest_arrival_time, + time(destination_stop_time.departure_time) AS dest_depart_time, + destination_stop_time.drop_off_type AS dest_drop_off_type, + destination_stop_time.pickup_type AS dest_pickup_type, + destination_stop_time.shape_dist_traveled AS dest_dist_traveled, + destination_stop_time.stop_headsign AS dest_stop_headsign, + destination_stop_time.stop_sequence AS dest_stop_sequence, + destination_stop_time.timepoint AS dest_stop_timepoint, + calendar.{yesterday.strftime("%A").lower()} AS yesterday, + calendar.{now.strftime("%A").lower()} AS today, + {tomorrow_select} + calendar.start_date AS start_date, + calendar.end_date AS end_date, + calendar_date_today.exception_type as today_cd + FROM trips trip + INNER JOIN calendar calendar + ON trip.service_id = calendar.service_id + INNER JOIN stop_times origin_stop_time + ON trip.trip_id = origin_stop_time.trip_id + INNER JOIN stops start_station + ON origin_stop_time.stop_id = start_station.stop_id + INNER JOIN stop_times destination_stop_time + ON trip.trip_id = destination_stop_time.trip_id + INNER JOIN stops end_station + ON destination_stop_time.stop_id = end_station.stop_id + INNER JOIN routes route + ON route.route_id = trip.route_id + INNER JOIN calendar_dates calendar_date_today + ON trip.service_id = calendar_date_today.service_id + WHERE start_station.stop_id = :origin_station_id + AND end_station.stop_id = :end_station_id + AND origin_stop_sequence < dest_stop_sequence + AND (calendar_date_today.date = :today or calendar_date_today.date = :tomorrow) + ORDER BY today_cd, origin_depart_time """ # noqa: S608 result = schedule.engine.connect().execute( text(sql_query), @@ -98,17 +138,16 @@ def get_next_departure(data): "origin_station_id": start_station_id, "end_station_id": end_station_id, "today": now_date, + "tomorrow": tomorrow_date, "limit": limit, }, ) - # Create lookup timetable for today and possibly tomorrow, taking into # account any departures from yesterday scheduled after midnight, # as long as all departures are within the calendar date range. timetable = {} yesterday_start = today_start = tomorrow_start = None yesterday_last = today_last = "" - for row_cursor in result: row = row_cursor._asdict() if row["yesterday"] == 1 and yesterday_date >= row["start_date"]: @@ -119,8 +158,7 @@ def get_next_departure(data): idx = f"{now_date} {row['origin_depart_time']}" timetable[idx] = {**row, **extras} yesterday_last = idx - - if row["today"] == 1: + if row["today"] == 1 or row["today_cd"] == 1: extras = {"day": "today", "first": False, "last": False} if today_start is None: today_start = row["origin_depart_date"] @@ -132,7 +170,6 @@ def get_next_departure(data): idx = f"{idx_prefix} {row['origin_depart_time']}" timetable[idx] = {**row, **extras} today_last = idx - if ( "tomorrow" in row and row["tomorrow"] == 1 @@ -145,11 +182,9 @@ def get_next_departure(data): if tomorrow_start == row["origin_depart_date"]: idx = f"{tomorrow_date} {row['origin_depart_time']}" timetable[idx] = {**row, **extras} - # Flag last departures. for idx in filter(None, [yesterday_last, today_last]): timetable[idx]["last"] = True - item = {} for key in sorted(timetable.keys()): if datetime.datetime.strptime(key, "%Y-%m-%d %H:%M:%S") > now: diff --git a/custom_components/gtfs2/translations/fr.json b/custom_components/gtfs2/translations/fr.json index 6694db1..4fb1911 100644 --- a/custom_components/gtfs2/translations/fr.json +++ b/custom_components/gtfs2/translations/fr.json @@ -3,14 +3,14 @@ "step": { "user": { "data": { - "file": "Sélectionner la source" + "file": "Sélectionnez la source" }, "description": "Sélectionnez parmi les sources existantes dans votre dossier gtfs2" }, "source": { "data": { - "file": "Nouveau nom de source de données", - "url": "URL externe vers le fichier (zip) de données GTFS" + "file": "Nom de la nouvelle source de données", + "url": "URL externe vers le fichier (zip) des données GTFS" }, "description": "REMARQUE : avec une nouvelle URL, cela peut prendre du temps après la soumission, selon la taille du fichier et performance du serveur" }, @@ -22,12 +22,12 @@ }, "stops": { "data": { - "origin": "Stop d'origine", - "destination": "Stop de destination", - "name": "Nom de la route", + "origin": "Arrêt d'origine", + "destination": "Arrêt de destination", + "name": "Nom de la ligne", "offset": "Décalage en minutes", "refresh_interval": "Intervalle d'actualisation en minutes", - "include_tomorrow": "Inclure demain?" + "include_tomorrow": "Inclure le lendemain?" } } }, @@ -35,14 +35,14 @@ "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", "unknown": "[%key:common::config_flow::error::unknown%]", - "stop_incorrect": "Destination de départ et/ou de fin incorrecte, éventuellement pas de transport « aujourd'hui » ou pas dans la même direction, vérifiez les journaux", - "generic_failure": "Échec global, vérifiez les journaux", + "stop_incorrect": "Arrêt de départ et/ou de fin incorrecte, éventuellement pas de transport « aujourd'hui » ou pas dans la même direction, vérifiez les logs d'érreur", + "generic_failure": "Échec global, vérifiez les logs d'érreur", "no_data_file": "Problème de collecte de données : URL incorrecte ou nom de fichier ne se trouvant pas dans le bon dossier" }, "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", "files_deleted": "Source de données supprimée, cela peut affecter les itinéraires existants", - "stop_incorrect": "Destination de départ et/ou de fin incorrecte, éventuellement pas de transport « aujourd'hui » ou pas dans la même direction, vérifiez les journaux" + "stop_incorrect": "Arrêt de départ et/ou de fin incorrecte, éventuellement pas de transport « aujourd'hui » ou pas dans la même direction, vérifiez logs d'érreur" } } -} +} \ No newline at end of file