Fix for time_date sensor (#10694)

* fix to time_date sensor

* cleaned up the code and added unit tests

* fixed lint errors
This commit is contained in:
Egor Tsinko 2017-11-19 20:41:30 -07:00 committed by Paulus Schoutsen
parent 8cb87d5e64
commit 235707d31c
2 changed files with 23 additions and 6 deletions

View file

@ -90,7 +90,7 @@ class TimeDateSensor(Entity):
if now is None:
now = dt_util.utcnow()
if self.type == 'date':
now = dt_util.start_of_local_day(now)
now = dt_util.start_of_local_day(dt_util.as_local(now))
return now + timedelta(seconds=86400)
elif self.type == 'beat':
interval = 86.4

View file

@ -1,5 +1,6 @@
"""The tests for Kira sensor platform."""
import unittest
from unittest.mock import patch
from homeassistant.components.sensor import time_date as time_date
import homeassistant.util.dt as dt_util
@ -36,11 +37,6 @@ class TestTimeDateSensor(unittest.TestCase):
next_time = device.get_next_interval(now)
assert next_time == dt_util.utc_from_timestamp(60)
device = time_date.TimeDateSensor(self.hass, 'date')
now = dt_util.utc_from_timestamp(12345)
next_time = device.get_next_interval(now)
assert next_time == dt_util.utc_from_timestamp(86400)
device = time_date.TimeDateSensor(self.hass, 'beat')
now = dt_util.utc_from_timestamp(29)
next_time = device.get_next_interval(now)
@ -89,6 +85,27 @@ class TestTimeDateSensor(unittest.TestCase):
# so the second day was 18000 + 86400
assert next_time.timestamp() == 104400
new_tz = dt_util.get_time_zone('America/Edmonton')
assert new_tz is not None
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(self.hass, 'date')
next_time = device.get_next_interval(now)
assert (next_time.timestamp() ==
dt_util.as_timestamp('2017-11-14 00:00:00-07:00'))
@patch('homeassistant.util.dt.utcnow',
return_value=dt_util.parse_datetime('2017-11-14 02:47:19-00:00'))
def test_timezone_intervals_empty_parameter(self, _):
"""Test get_interval() without parameters."""
new_tz = dt_util.get_time_zone('America/Edmonton')
assert new_tz is not None
dt_util.set_default_time_zone(new_tz)
device = time_date.TimeDateSensor(self.hass, 'date')
next_time = device.get_next_interval()
assert (next_time.timestamp() ==
dt_util.as_timestamp('2017-11-14 00:00:00-07:00'))
def test_icons(self):
"""Test attributes of sensors."""
device = time_date.TimeDateSensor(self.hass, 'time')