Skip to content

Commit

Permalink
Set the state to a string rather than a datetime.
Browse files Browse the repository at this point in the history
As reported on issue #2, iOS is fussy about the format of timestamp strings.
The correct ISO format is actually returned by the Metlink API, but when
we parse it into a datetime object, Home Assistant later converts it back
using a more human readable format (a space instead of T between date and time).

Although I had a lot of trouble getting timestamps to display correctly
early on, it seems the format from Metlink API is OK to use as-is.
  • Loading branch information
make-all committed Jun 19, 2021
1 parent 2843dce commit abb287d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion custom_components/metlink/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"issue_tracker": "https://github.com/make-all/metlink-nz/issues",
"name": "Metlink Wellington Transport",
"requirements": ["isodate==0.6.0"],
"version": "1.0.1"
"version": "1.0.2"
}
11 changes: 6 additions & 5 deletions custom_components/metlink/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,9 @@ async def async_update(self):
name = f"{departure[ATTR_SERVICE]} {dest}"
if num == 1:
# First record is the next departure, so use that
# to set the state (departure time) and friendly name
self._state = dt_util.parse_datetime(time)
# to set the state (departure time)
next_departure = dt_util.parse_datetime(time)
self._state = time
self._icon = OPERATOR_ICONS.get(
departure[ATTR_OPERATOR], DEFAULT_ICON
)
Expand All @@ -226,7 +227,7 @@ async def async_update(self):
# Dynamic polling of the API to get accurate predictions
# close to the time, without overloading the server when
# there is nothing pending:
when = (self._state - now).total_seconds()
when = (next_departure - now).total_seconds()
# Within 3 minutes, poll next call as well
if when < 180:
self.update_time = now
Expand All @@ -238,10 +239,10 @@ async def async_update(self):
self.update_time = now + timedelta(minutes=10)
# More than an hour away, don't poll until 1 hour before
else:
self.update_time = self._state - timedelta(hours=1)
self.update_time = next_departure - timedelta(hours=1)

_LOGGER.debug(
f"Next departure at {self._state}, blocking updates until {self.update_time}"
f"Next departure at {next_departure}, blocking updates until {self.update_time}"
)
else:
suffix = f"_{num}"
Expand Down

0 comments on commit abb287d

Please sign in to comment.