Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
reduce route_type se;lection to 2, GTFS comes with dozens, sometimes used mostly not. Splitting out only 'trains'
Spin off the extracting process to not have a hanging pop-up
  • Loading branch information
vingerha committed Dec 14, 2023
1 parent 457d83c commit 51bf9de
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 41 deletions.
2 changes: 1 addition & 1 deletion custom_components/gtfs2/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ async def async_step_route_type(self, user_input: dict | None = None) -> FlowRes
step_id="route_type",
data_schema=vol.Schema(
{
vol.Required("route_type"): selector.SelectSelector(selector.SelectSelectorConfig(options=["0", "1", "2", "3", "4", "99"], translation_key="route_type")),
vol.Required("route_type"): selector.SelectSelector(selector.SelectSelectorConfig(options=["2", "99"], translation_key="route_type")),
},
),
errors=errors,
Expand Down
31 changes: 25 additions & 6 deletions custom_components/gtfs2/gtfs_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pygtfs
from sqlalchemy.sql import text
import multiprocessing
from multiprocessing import Process

import homeassistant.util.dt as dt_util
from homeassistant.core import HomeAssistant
Expand All @@ -32,12 +33,16 @@ def get_next_departure(self):
timezone=dt_util.get_time_zone(self.hass.config.time_zone)
schedule = self._data["schedule"]
route_type = self._data["route_type"]

# if type 2 (train) then filter on that and use name-like search
if route_type == "2":
route_type_where = f"route_type = :route_type"
start_station_id = str(self._data['origin'])+'%'
end_station_id = str(self._data['destination'])+'%'
start_station_where = f"AND start_station.stop_id in (select stop_id from stops where stop_name like :origin_station_id)"
end_station_where = f"AND end_station.stop_id in (select stop_id from stops where stop_name like :end_station_id)"
else:
route_type_where = "1=1"
start_station_id = self._data['origin'].split(': ')[0]
end_station_id = self._data['destination'].split(': ')[0]
start_station_where = f"AND start_station.stop_id = :origin_station_id"
Expand Down Expand Up @@ -109,7 +114,7 @@ def get_next_departure(self):
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 route_type = {route_type}
WHERE {route_type_where}
{start_station_where}
{end_station_where}
AND origin_stop_sequence < dest_stop_sequence
Expand Down Expand Up @@ -156,7 +161,7 @@ def get_next_departure(self):
ON route.route_id = trip.route_id
INNER JOIN calendar_dates calendar_date_today
ON trip.service_id = calendar_date_today.service_id
WHERE route_type = {route_type}
WHERE {route_type_where}
{start_station_where}
{end_station_where}
AND origin_stop_sequence < dest_stop_sequence
Expand Down Expand Up @@ -376,15 +381,29 @@ def get_gtfs(hass, path, data, update=False):

sqlite_file = f"{gtfs_root}.sqlite?check_same_thread=False"
joined_path = os.path.join(gtfs_dir, sqlite_file)
gtfs = pygtfs.Schedule(joined_path)
gtfs = pygtfs.Schedule(joined_path)
if not gtfs.feeds:
_LOGGER.info("Starting gtfs file unpacking: %s", joined_path)
pygtfs.append_feed(gtfs, os.path.join(gtfs_dir, file))
extract = Process(target=extract_from_zip(gtfs,gtfs_dir,file))
extract.start()
extract.join()
_LOGGER.info("Exiting main after start subprocess for unpacking: %s", file)
return "unpacking"
return gtfs



def extract_from_zip(gtfs,gtfs_dir,file):
_LOGGER.debug("Extracting gtfs file: %s", file)
if os.fork() != 0:
return
pygtfs.append_feed(gtfs, os.path.join(gtfs_dir, file))



def get_route_list(schedule, data):
_LOGGER.debug("Getting routes with data: %s", data)
route_type_where = ""
if data["route_type"] != 99:
if data["route_type"] != "99":
route_type_where = f"where route_type = {data['route_type']}"
sql_routes = f"""
SELECT route_id, route_short_name, route_long_name from routes
Expand Down
46 changes: 26 additions & 20 deletions custom_components/gtfs2/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,29 +287,35 @@ def _update_attrs(self): # noqa: C901 PLR0911

key = "origin_station_stop_id"
# exclude check if route_type =2 (trains) as no ID is used
if self._origin and key not in self._attributes and self._route_type != "2":
self.append_keys(self.dict_for_table(self._origin), "Origin Station")
self._attributes[ATTR_LOCATION_ORIGIN] = LOCATION_TYPE_OPTIONS.get(
self._origin.location_type, LOCATION_TYPE_DEFAULT
)
self._attributes[ATTR_WHEELCHAIR_ORIGIN] = WHEELCHAIR_BOARDING_OPTIONS.get(
self._origin.wheelchair_boarding, WHEELCHAIR_BOARDING_DEFAULT
)
if self._route_type != "2":
if self._origin and key not in self._attributes:
self.append_keys(self.dict_for_table(self._origin), "Origin Station")
self._attributes[ATTR_LOCATION_ORIGIN] = LOCATION_TYPE_OPTIONS.get(
self._origin.location_type, LOCATION_TYPE_DEFAULT
)
self._attributes[ATTR_WHEELCHAIR_ORIGIN] = WHEELCHAIR_BOARDING_OPTIONS.get(
self._origin.wheelchair_boarding, WHEELCHAIR_BOARDING_DEFAULT
)
else:
self._attributes["origin_station_stop_name"] = self._origin

key = "destination_station_stop_id"
# exclude check if route_type =2 (trains) as no ID is used
if self._destination and key not in self._attributes and self._route_type != "2":
self.append_keys(
self.dict_for_table(self._destination), "Destination Station"
)
self._attributes[ATTR_LOCATION_DESTINATION] = LOCATION_TYPE_OPTIONS.get(
self._destination.location_type, LOCATION_TYPE_DEFAULT
)
self._attributes[
ATTR_WHEELCHAIR_DESTINATION
] = WHEELCHAIR_BOARDING_OPTIONS.get(
self._destination.wheelchair_boarding, WHEELCHAIR_BOARDING_DEFAULT
)
if self._route_type != "2":
if self._destination and key not in self._attributes:
self.append_keys(
self.dict_for_table(self._destination), "Destination Station"
)
self._attributes[ATTR_LOCATION_DESTINATION] = LOCATION_TYPE_OPTIONS.get(
self._destination.location_type, LOCATION_TYPE_DEFAULT
)
self._attributes[
ATTR_WHEELCHAIR_DESTINATION
] = WHEELCHAIR_BOARDING_OPTIONS.get(
self._destination.wheelchair_boarding, WHEELCHAIR_BOARDING_DEFAULT
)
else:
self._attributes["destination_station_stop_name"] = self._destination

# Manage Route metadata
key = "route_id"
Expand Down
12 changes: 4 additions & 8 deletions custom_components/gtfs2/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"route_type": {
"data": {
"route_type": "Select the type of transport"
"route_type": "Train rides are setup between cities, not stops"
},
"description": "Select from the options below"
},
Expand Down Expand Up @@ -61,7 +61,7 @@
"stop_incorrect": "Start and/or End destination incorrect, possibly no transport 'today' or not in same direction, check logs",
"no_data_file": "Data collection issue: URL incorrect or filename not in the correct folder",
"no_zip_file": "Data collection issue: ZIP file not existing in the correct folder, note that it is capital-sensitive",
"extracting": "Extracting data, this can take a while"
"extracting": "(Still) extracting data, this will take time (read docu) "
}
},
"options": {
Expand Down Expand Up @@ -101,12 +101,8 @@
},
"route_type": {
"options": {
"0": "Tram, Streetcar, Light rail.",
"1": "Subway, Metro. Any underground rail system",
"2": "Rail, intercity or long-distance travel",
"3": "Bus, short- and long-distance bus routes",
"4": "Ferries, short- and long-distance boat service",
"99": "All, this includes also more obscure ones not in above"
"99": "All, excluding trains",
"2": "Trains, intercity or long-distance travel"
}
}
},
Expand Down
8 changes: 2 additions & 6 deletions custom_components/gtfs2/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"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_data_file": "Problème de collecte de données : URL incorrecte ou nom de fichier ne se trouve pas dans le bon dossier",
"no_zip_file": "Problème de collecte de données : fichier ZIP ne se trouve pas dans le bon dossier, note: sensible à la casse",
"extracting": "Extraction des données, cela prend du temps"
"extracting": "Extraction des données en cours, cela prend du temps (voir la docu)"
}
},
"options": {
Expand Down Expand Up @@ -100,12 +100,8 @@
},
"route_type": {
"options": {
"0": "Tramway",
"1": "Métro, tout système ferroviaire souterrain",
"2": "Voyages ferroviaires, interurbains ou longue distance",
"3": "Bus, lignes courtes et longues distances",
"4": "Ferries, service de bateaux courte et longue distance",
"99": "Tout, inclut également les plus obscurs pas mentionnés ci-dessus."
"99": "Tout, à part des ferroviaires"
}
}
},
Expand Down

0 comments on commit 51bf9de

Please sign in to comment.