Skip to content

Commit

Permalink
fix: round timers and reminders to nearest second
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Jan 19, 2020
1 parent 31db58f commit 1671c8b
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions custom_components/alexa_media/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ def _fix_alarm_date_time(self, value):
)
return value

@staticmethod
def _round_time(value: datetime.datetime) -> datetime.datetime:
precision = datetime.timedelta(seconds=1).total_seconds()
seconds = (value - value.min.replace(tzinfo=value.tzinfo)).seconds
rounding = (seconds + precision / 2) // precision * precision
return value + datetime.timedelta(0, rounding - seconds, -value.microsecond)

async def async_added_to_hass(self):
"""Store register state change callback."""
try:
Expand Down Expand Up @@ -369,8 +376,11 @@ def state(self) -> datetime.datetime:
"""Return the state of the sensor."""
return (
dt.as_local(
dt.utc_from_timestamp(
dt.utcnow().timestamp() + self._next[self._sensor_property] / 1000
super()._round_time(
dt.utc_from_timestamp(
dt.utcnow().timestamp()
+ self._next[self._sensor_property] / 1000
)
)
).isoformat()
if self._next
Expand Down Expand Up @@ -404,8 +414,10 @@ def state(self):
"""Return the state of the sensor."""
return (
dt.as_local(
datetime.datetime.fromtimestamp(
self._next[self._sensor_property] / 1000, tz=LOCAL_TIMEZONE
super()._round_time(
datetime.datetime.fromtimestamp(
self._next[self._sensor_property] / 1000, tz=LOCAL_TIMEZONE
)
)
).isoformat()
if self._next
Expand Down

0 comments on commit 1671c8b

Please sign in to comment.