Mock time_date sensor tests (#43141)

pull/43165/head
Anders Melchiorsen 2020-11-13 09:31:55 +01:00 committed by GitHub
parent 76b843118b
commit e0f1d0ab20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 10 deletions

View File

@ -80,7 +80,7 @@ class TimeDateSensor(Entity):
return "mdi:clock"
async def async_added_to_hass(self) -> None:
"""Set up next update."""
"""Set up first update."""
self.unsub = async_track_point_in_utc_time(
self.hass, self.point_in_time_listener, self.get_next_interval()
)
@ -91,20 +91,24 @@ class TimeDateSensor(Entity):
self.unsub()
self.unsub = None
def get_next_interval(self, now=None):
def get_next_interval(self):
"""Compute next time an update should occur."""
if now is None:
now = dt_util.utcnow()
now = dt_util.utcnow()
if self.type == "date":
now = dt_util.start_of_local_day(dt_util.as_local(now))
return now + timedelta(seconds=86400)
if self.type == "beat":
interval = 86.4
else:
interval = 60
timestamp = int(dt_util.as_timestamp(now))
delta = interval - (timestamp % interval)
return now + timedelta(seconds=delta)
next_interval = now + timedelta(seconds=delta)
_LOGGER.debug("%s + %s -> %s (%s)", now, delta, next_interval, self.type)
return next_interval
def _update_internal_state(self, time_date):
time = dt_util.as_local(time_date).strftime(TIME_STR_FORMAT)

View File

@ -21,17 +21,20 @@ async def test_intervals(hass):
"""Test timing intervals of sensors."""
device = time_date.TimeDateSensor(hass, "time")
now = dt_util.utc_from_timestamp(45)
next_time = device.get_next_interval(now)
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)
next_time = device.get_next_interval(now)
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)
device = time_date.TimeDateSensor(hass, "date_time")
now = dt_util.utc_from_timestamp(1495068899)
next_time = device.get_next_interval(now)
with patch("homeassistant.util.dt.utcnow", return_value=now):
next_time = device.get_next_interval()
assert next_time == dt_util.utc_from_timestamp(1495068900)
now = dt_util.utcnow()
@ -117,7 +120,8 @@ async def test_timezone_intervals(hass):
device = time_date.TimeDateSensor(hass, "date")
now = dt_util.utc_from_timestamp(50000)
next_time = device.get_next_interval(now)
with patch("homeassistant.util.dt.utcnow", return_value=now):
next_time = device.get_next_interval()
# start of local day in EST was 18000.0
# so the second day was 18000 + 86400
assert next_time.timestamp() == 104400
@ -127,7 +131,8 @@ async def test_timezone_intervals(hass):
dt_util.set_default_time_zone(new_tz)
now = dt_util.parse_datetime("2017-11-13 19:47:19-07:00")
device = time_date.TimeDateSensor(hass, "date")
next_time = device.get_next_interval(now)
with patch("homeassistant.util.dt.utcnow", return_value=now):
next_time = device.get_next_interval()
assert next_time.timestamp() == dt_util.as_timestamp("2017-11-14 00:00:00-07:00")