Skip to content

Commit

Permalink
Cache GTFS attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
renemarc committed Feb 11, 2019
1 parent 987b5cd commit 1b1555f
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions homeassistant/components/sensor/gtfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,19 +256,19 @@ def update(self):
agency = self._departure['agency']
route = self._departure['route']
trip = self._departure['trip']
trip_id = self._departure['trip_id']

name = '{} {} to {} next departure'
self._name = (self._custom_name or
name.format(agency.agency_name,
origin_station.stop_id,
destination_station.stop_id))

self._icon = ICONS.get(route.route_type, ICON)

# Build attributes
self._attributes = {}
self._attributes['offset'] = self._offset.seconds / 60

self._icon = ICONS.get(route.route_type, ICON)

def dict_for_table(resource):
"""Return a dict for the SQLAlchemy resource given."""
return dict((col, getattr(resource, col))
Expand All @@ -288,11 +288,22 @@ def append_keys(resource, prefix=None):
pretty_key = '{} {}'.format(prefix, pretty_key)
self._attributes[pretty_key] = val

append_keys(dict_for_table(agency), 'Agency')
append_keys(dict_for_table(route), 'Route')
append_keys(dict_for_table(trip), 'Trip')
append_keys(dict_for_table(origin_station), 'Origin Station')
append_keys(dict_for_table(destination_station),
'Destination Station')
append_keys(origin_stop_time, 'Origin Stop')
append_keys(destination_stop_time, 'Destination Stop')
if "Agency ID" not in self._attributes.keys():
append_keys(dict_for_table(agency), 'Agency')

if "Route ID" not in self._attributes.keys():
append_keys(dict_for_table(route), 'Route')

if "Origin Station Stop ID" not in self._attributes.keys():
append_keys(dict_for_table(origin_station), "Origin Station")

if "Destination Station Stop ID" not in self._attributes.keys():
append_keys(dict_for_table(destination_station),
"Destination Station")

if "Trip ID" not in self._attributes.keys() \
or self._attributes["Trip ID"] != trip_id:
_LOGGER.info("Fetching trip details for %s", trip_id)
append_keys(dict_for_table(trip), 'Trip')
append_keys(origin_stop_time, "Origin Stop")
append_keys(destination_stop_time, "Destination Stop")

0 comments on commit 1b1555f

Please sign in to comment.