From 3e1f20f0ac76e6a90c9228dcb43e3e0e05a7fed3 Mon Sep 17 00:00:00 2001 From: Arjan <44190435+vingerha@users.noreply.github.com> Date: Mon, 4 Dec 2023 06:52:56 +0100 Subject: [PATCH] Finetuning --- custom_components/gtfs2/__init__.py | 15 ++++++++++++--- custom_components/gtfs2/coordinator.py | 7 +++---- custom_components/gtfs2/gtfs_rt_helper.py | 3 +-- custom_components/gtfs2/manifest.json | 2 +- custom_components/gtfs2/sensor.py | 2 +- 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/custom_components/gtfs2/__init__.py b/custom_components/gtfs2/__init__.py index 7aee183..6b18126 100644 --- a/custom_components/gtfs2/__init__.py +++ b/custom_components/gtfs2/__init__.py @@ -8,6 +8,7 @@ from datetime import timedelta from .const import DOMAIN, PLATFORMS, DEFAULT_PATH, DEFAULT_REFRESH_INTERVAL +from homeassistant.const import CONF_HOST from .coordinator import GTFSUpdateCoordinator import voluptuous as vol from .gtfs_helper import get_gtfs @@ -107,19 +108,27 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data[DOMAIN].pop(entry.entry_id) return unload_ok - + +async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: + """Remove a config entry.""" + await hass.async_add_executor_job(_remove_token_file, hass, entry.data[CONF_HOST]) + if DOMAIN in hass.data: + hass.data[DOMAIN].pop(entry.entry_id, None) + if not hass.data[DOMAIN]: + hass.data.pop(DOMAIN) def setup(hass, config): - """Setup the service example component.""" + """Setup the service component.""" def update_gtfs(call): """My GTFS service.""" _LOGGER.debug("Updating GTFS with: %s", call.data) get_gtfs(hass, DEFAULT_PATH, call.data, True) - return True + return True hass.services.register( DOMAIN, "update_gtfs", update_gtfs) + return True async def update_listener(hass: HomeAssistant, entry: ConfigEntry): diff --git a/custom_components/gtfs2/coordinator.py b/custom_components/gtfs2/coordinator.py index 2067b1c..380a710 100644 --- a/custom_components/gtfs2/coordinator.py +++ b/custom_components/gtfs2/coordinator.py @@ -123,10 +123,9 @@ async def _async_update_data(self) -> dict[str, str]: self._headers = None self._headers = None self.info = {} - try: - self._route_id = self._data["next_departure"]["route_id"] - except Exception as ex: # pylint: disable=broad-except - _LOGGER.error("Error getting entity route_id for realtime data, for origin: %s with error: %s", data["origin"], ex) + self._route_id = self._data["next_departure"].get("route_id", None) + if self._route_id == None: + _LOGGER.debug("GTFS RT: no route_id in sensor data, using route_id from config_entry") self._route_id = data["route"].split(": ")[0] self._stop_id = data["origin"].split(": ")[0] self._destination_id = data["destination"].split(": ")[0] diff --git a/custom_components/gtfs2/gtfs_rt_helper.py b/custom_components/gtfs2/gtfs_rt_helper.py index f58bf9f..f996e18 100644 --- a/custom_components/gtfs2/gtfs_rt_helper.py +++ b/custom_components/gtfs2/gtfs_rt_helper.py @@ -396,7 +396,6 @@ def get_rt_alerts(self): label="alerts", ) for entity in feed_entities: - _LOGGER.debug("RT Alert entity: %s", entity) if entity.HasField("alert"): for x in entity.alert.informed_entity: if x.HasField("stop_id"): @@ -427,7 +426,7 @@ def update_geojson(self): file = os.path.join(geojson_dir, self._route_dir + ".json") _LOGGER.debug("GTFS RT geojson file: %s", file) with open(file, "w") as outfile: - json.dump(self.geojson, outfile) + json.dump(self.geojson, outfile) diff --git a/custom_components/gtfs2/manifest.json b/custom_components/gtfs2/manifest.json index 63fac40..8a6071a 100644 --- a/custom_components/gtfs2/manifest.json +++ b/custom_components/gtfs2/manifest.json @@ -7,5 +7,5 @@ "iot_class": "local_polling", "issue_tracker": "https://github.com/vingerha/gtfs2/issues", "requirements": ["pygtfs==0.1.9","gtfs-realtime-bindings==1.0.0"], - "version": "0.1.6" + "version": "0.1.9" } diff --git a/custom_components/gtfs2/sensor.py b/custom_components/gtfs2/sensor.py index 4b8e50d..07c9a3e 100644 --- a/custom_components/gtfs2/sensor.py +++ b/custom_components/gtfs2/sensor.py @@ -346,7 +346,7 @@ def _update_attrs(self): # noqa: C901 PLR0911 if "destination_stop_time" in self._departure: _LOGGER.debug("Destination_stop_time %s", self._departure["destination_stop_time"]) else: - _LOGGER.warning("No destination_stop_time") + _LOGGER.debug("No destination_stop_time, possibly no service today") prefix = "destination_stop" if self._departure: self.append_keys(self._departure["destination_stop_time"], prefix)