From a6433efc8227bf3cc0565be79ed74f84b1706585 Mon Sep 17 00:00:00 2001 From: Arjan <44190435+vingerha@users.noreply.github.com> Date: Sun, 5 May 2024 09:08:49 +0200 Subject: [PATCH] Improve handling of realtime debug_output as json in servicecall --- custom_components/gtfs2/gtfs_rt_helper.py | 24 +++++++++++++++-------- custom_components/gtfs2/services.yaml | 13 ++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/custom_components/gtfs2/gtfs_rt_helper.py b/custom_components/gtfs2/gtfs_rt_helper.py index b551dde..4271405 100644 --- a/custom_components/gtfs2/gtfs_rt_helper.py +++ b/custom_components/gtfs2/gtfs_rt_helper.py @@ -408,14 +408,22 @@ def get_gtfs_rt(hass, path, data): _LOGGER.error("Ìssues with downloading GTFS RT data to: %s", os.path.join(gtfs_dir, file)) return "no_rt_data_file" if data.get("debug_output", False): - data_out = "" - feed_entities = get_gtfs_feed_entities( - url=data.get("url", None), - headers=_headers, - label="converting", - ) - file_all = data["file"] + "_converted.txt" - open(os.path.join(gtfs_dir, file_all), "w").write(json.dumps(feed_entities, indent=4)) + try: + data_out = "" + feed_entities = get_gtfs_feed_entities( + url=data.get("url", None), + headers=_headers, + label=data.get("rt_type", "-"), + ) + file_all = data["file"] + "_converted.txt" + except Exception as ex: # pylint: disable=broad-except + _LOGGER.info("Ìssues with converting GTFS RT data to JSON, output to string") + # check if content is json else write without format + try: + open(os.path.join(gtfs_dir, file_all), "w").write(json.dumps(feed_entities, indent=4)) + except Exception as ex: + _LOGGER.debug("Not writing to file as json because of error: %s", ex) + open(os.path.join(gtfs_dir, file_all), "w").write(str(feed_entities)) return "ok" class LocalFileAdapter(requests.adapters.HTTPAdapter): diff --git a/custom_components/gtfs2/services.yaml b/custom_components/gtfs2/services.yaml index 9e39a76..a70164f 100644 --- a/custom_components/gtfs2/services.yaml +++ b/custom_components/gtfs2/services.yaml @@ -96,6 +96,19 @@ update_gtfs_rt_local: required: true selector: boolean: + + rt_type: + name: Indicate type of realtime data + description: Select if you update from url or zip + required: true + default: "trip_data" + selector: + select: + translation_key: "extract_from" + options: + - "trip_data" + - "vehicle_positions" + - "alerts" debug_output: name: Save all to local file in readable format