Fix time_date timestamp offsets (#43165)

This commit is contained in:
Anders Melchiorsen 2020-11-13 13:29:57 +01:00 committed by GitHub
parent 8dbd54bed1
commit 87d86026ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 4 deletions

View file

@ -100,10 +100,13 @@ class TimeDateSensor(Entity):
return now + timedelta(seconds=86400)
if self.type == "beat":
# Add 1 hour because @0 beats is at 23:00:00 UTC.
timestamp = dt_util.as_timestamp(now + timedelta(hours=1))
interval = 86.4
else:
timestamp = dt_util.as_timestamp(now)
interval = 60
timestamp = int(dt_util.as_timestamp(now))
delta = interval - (timestamp % interval)
next_interval = now + timedelta(seconds=delta)
_LOGGER.debug("%s + %s -> %s (%s)", now, delta, next_interval, self.type)

View file

@ -20,16 +20,16 @@ def restore_ts():
async def test_intervals(hass):
"""Test timing intervals of sensors."""
device = time_date.TimeDateSensor(hass, "time")
now = dt_util.utc_from_timestamp(45)
now = dt_util.utc_from_timestamp(45.5)
with patch("homeassistant.util.dt.utcnow", return_value=now):
next_time = device.get_next_interval()
assert next_time == dt_util.utc_from_timestamp(60)
device = time_date.TimeDateSensor(hass, "beat")
now = dt_util.utc_from_timestamp(29)
now = dt_util.parse_datetime("2020-11-13 00:00:29+01:00")
with patch("homeassistant.util.dt.utcnow", return_value=now):
next_time = device.get_next_interval()
assert next_time == dt_util.utc_from_timestamp(86.4)
assert next_time == dt_util.parse_datetime("2020-11-13 00:01:26.4+01:00")
device = time_date.TimeDateSensor(hass, "date_time")
now = dt_util.utc_from_timestamp(1495068899)