mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
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:
parent
3f5c748560
commit
7695ca2c8b
@ -90,7 +90,7 @@ class TimeDateSensor(Entity):
|
|||||||
if now is None:
|
if now is None:
|
||||||
now = dt_util.utcnow()
|
now = dt_util.utcnow()
|
||||||
if self.type == 'date':
|
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)
|
return now + timedelta(seconds=86400)
|
||||||
elif self.type == 'beat':
|
elif self.type == 'beat':
|
||||||
interval = 86.4
|
interval = 86.4
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
"""The tests for Kira sensor platform."""
|
"""The tests for Kira sensor platform."""
|
||||||
import unittest
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
from homeassistant.components.sensor import time_date as time_date
|
from homeassistant.components.sensor import time_date as time_date
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
@ -36,11 +37,6 @@ class TestTimeDateSensor(unittest.TestCase):
|
|||||||
next_time = device.get_next_interval(now)
|
next_time = device.get_next_interval(now)
|
||||||
assert next_time == dt_util.utc_from_timestamp(60)
|
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')
|
device = time_date.TimeDateSensor(self.hass, 'beat')
|
||||||
now = dt_util.utc_from_timestamp(29)
|
now = dt_util.utc_from_timestamp(29)
|
||||||
next_time = device.get_next_interval(now)
|
next_time = device.get_next_interval(now)
|
||||||
@ -89,6 +85,27 @@ class TestTimeDateSensor(unittest.TestCase):
|
|||||||
# so the second day was 18000 + 86400
|
# so the second day was 18000 + 86400
|
||||||
assert next_time.timestamp() == 104400
|
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):
|
def test_icons(self):
|
||||||
"""Test attributes of sensors."""
|
"""Test attributes of sensors."""
|
||||||
device = time_date.TimeDateSensor(self.hass, 'time')
|
device = time_date.TimeDateSensor(self.hass, 'time')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user