Skip to content

Commit

Permalink
Catch errors when extracting: UpdateSensor only
Browse files Browse the repository at this point in the history
Not covering LocalUpdateSensor (yet)
  • Loading branch information
vingerha committed Sep 8, 2024
1 parent a49e039 commit 475bf3b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions custom_components/gtfs2/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ async def _async_update_data(self) -> dict[str, str]:
self._pygtfs = get_gtfs(
self.hass, DEFAULT_PATH, data, False
)

self._data = {
"schedule": self._pygtfs,
"origin": data["origin"],
Expand All @@ -79,12 +80,11 @@ async def _async_update_data(self) -> dict[str, str]:

if check_extracting(self.hass, self._data['gtfs_dir'],self._data['file']):
_LOGGER.warning("Cannot update this sensor as still unpacking: %s", self._data["file"])
previous_data={}
previous_data["extracting"] = True
return previous_data


# determin static + rt or only static (refresh schedule depending)
# determine static + rt or only static (refresh schedule depending)
#1. sensor exists with data but refresh interval not yet reached, use existing data
if previous_data is not None and (datetime.datetime.strptime(previous_data["gtfs_updated_at"],'%Y-%m-%dT%H:%M:%S.%f%z') + timedelta(minutes=options.get("refresh_interval", DEFAULT_REFRESH_INTERVAL))) > dt_util.utcnow() + timedelta(seconds=1) :
run_static = False
Expand Down
6 changes: 4 additions & 2 deletions custom_components/gtfs2/gtfs_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,10 @@ def get_gtfs(hass, path, data, update=False):

(gtfs_root, _) = os.path.splitext(file)
sqlite_file = f"{gtfs_root}.sqlite?check_same_thread=False"
joined_path = os.path.join(gtfs_dir, sqlite_file)
joined_path = os.path.join(gtfs_dir, sqlite_file)

gtfs = pygtfs.Schedule(joined_path)

if not gtfs.feeds:
if data.get("clean_feed_info", False):
extract = Process(target=extract_from_zip, args = (hass, gtfs,gtfs_dir,file,['shapes.txt','transfers.txt','feed_info.txt']))
Expand Down Expand Up @@ -993,4 +995,4 @@ async def update_gtfs_local_stops(hass, data):
for cf_entry in entries:
_LOGGER.debug("Reloading local stops for config_entry_id: %s", cf_entry)
reload = await hass.config_entries.async_reload(cf_entry)
return
return
4 changes: 4 additions & 0 deletions custom_components/gtfs2/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ def icon(self) -> str:

def _update_attrs(self): # noqa: C901 PLR0911
_LOGGER.debug("SENSOR update attr data: %s", self.coordinator.data)
if self.coordinator.data["extracting"]:
_LOGGER.warning("Extracting datasource")
self._attr_native_value = None
return
self._pygtfs = self.coordinator.data["schedule"]
self.extracting = self.coordinator.data["extracting"]
self.origin = self.coordinator.data["origin"].split(": ")[0]
Expand Down

0 comments on commit 475bf3b

Please sign in to comment.