Skip to content

Commit

Permalink
Fix for time_date sensor (home-assistant#10694)
Browse files Browse the repository at this point in the history
* fix to time_date sensor

* cleaned up the code and added unit tests

* fixed lint errors
  • Loading branch information
etsinko authored and balloob committed Nov 20, 2017
1 parent 3f5c748 commit 7695ca2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/sensor/time_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def get_next_interval(self, now=None):
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
Expand Down
27 changes: 22 additions & 5 deletions tests/components/sensor/test_time_date.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -36,11 +37,6 @@ def test_intervals(self):
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)
Expand Down Expand Up @@ -89,6 +85,27 @@ def test_timezone_intervals(self):
# 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')
Expand Down

0 comments on commit 7695ca2

Please sign in to comment.