From 916b430ae847437dc2e3269cb89681b739c910a5 Mon Sep 17 00:00:00 2001 From: Arjan <44190435+vingerha@users.noreply.github.com> Date: Sun, 5 Jan 2025 07:47:07 +0100 Subject: [PATCH] Route type handling for bus icon 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.) --- custom_components/gtfs2/config_flow.py | 5 +++-- custom_components/gtfs2/coordinator.py | 8 ++++++-- custom_components/gtfs2/gtfs_helper.py | 4 ++-- custom_components/gtfs2/gtfs_rt_helper.py | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/custom_components/gtfs2/config_flow.py b/custom_components/gtfs2/config_flow.py index 415d394..2669e2b 100644 --- a/custom_components/gtfs2/config_flow.py +++ b/custom_components/gtfs2/config_flow.py @@ -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() @@ -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] @@ -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] diff --git a/custom_components/gtfs2/coordinator.py b/custom_components/gtfs2/coordinator.py index 46f8d8c..05a78c2 100644 --- a/custom_components/gtfs2/coordinator.py +++ b/custom_components/gtfs2/coordinator.py @@ -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 @@ -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 @@ -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): diff --git a/custom_components/gtfs2/gtfs_helper.py b/custom_components/gtfs2/gtfs_helper.py index 842c7ee..c28b2a2 100644 --- a/custom_components/gtfs2/gtfs_helper.py +++ b/custom_components/gtfs2/gtfs_helper.py @@ -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 @@ -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 diff --git a/custom_components/gtfs2/gtfs_rt_helper.py b/custom_components/gtfs2/gtfs_rt_helper.py index c837533..28befe3 100644 --- a/custom_components/gtfs2/gtfs_rt_helper.py +++ b/custom_components/gtfs2/gtfs_rt_helper.py @@ -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"]