2016-03-09 09:25:50 +00:00
|
|
|
"""Test event helpers."""
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=protected-access
|
2016-10-05 03:44:32 +00:00
|
|
|
import asyncio
|
2015-07-26 08:45:49 +00:00
|
|
|
import unittest
|
2016-02-14 21:07:21 +00:00
|
|
|
from datetime import datetime, timedelta
|
2015-07-26 08:45:49 +00:00
|
|
|
|
2016-01-25 03:23:56 +00:00
|
|
|
from astral import Astral
|
|
|
|
|
2016-09-26 21:20:36 +00:00
|
|
|
from homeassistant.bootstrap import setup_component
|
2015-08-17 03:44:46 +00:00
|
|
|
import homeassistant.core as ha
|
2016-04-21 20:59:42 +00:00
|
|
|
from homeassistant.const import MATCH_ALL
|
2016-02-14 21:07:21 +00:00
|
|
|
from homeassistant.helpers.event import (
|
|
|
|
track_point_in_utc_time,
|
|
|
|
track_point_in_time,
|
|
|
|
track_utc_time_change,
|
|
|
|
track_time_change,
|
|
|
|
track_state_change,
|
2017-01-05 22:05:16 +00:00
|
|
|
track_time_interval,
|
2016-02-14 21:07:21 +00:00
|
|
|
track_sunrise,
|
|
|
|
track_sunset,
|
|
|
|
)
|
2016-01-25 03:23:56 +00:00
|
|
|
from homeassistant.components import sun
|
2016-02-14 21:07:21 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2015-07-26 08:45:49 +00:00
|
|
|
|
2016-02-14 23:08:23 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
2015-07-26 08:45:49 +00:00
|
|
|
|
|
|
|
class TestEventHelpers(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the Home Assistant event helpers."""
|
2015-07-26 08:45:49 +00:00
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def setUp(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-02-14 23:08:23 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2015-07-26 08:45:49 +00:00
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def tearDown(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2015-07-26 08:45:49 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_track_point_in_time(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test track point in time."""
|
2015-08-04 16:16:10 +00:00
|
|
|
before_birthday = datetime(1985, 7, 9, 12, 0, 0, tzinfo=dt_util.UTC)
|
|
|
|
birthday_paulus = datetime(1986, 7, 9, 12, 0, 0, tzinfo=dt_util.UTC)
|
|
|
|
after_birthday = datetime(1987, 7, 9, 12, 0, 0, tzinfo=dt_util.UTC)
|
2015-07-26 08:45:49 +00:00
|
|
|
|
|
|
|
runs = []
|
|
|
|
|
|
|
|
track_point_in_utc_time(
|
|
|
|
self.hass, lambda x: runs.append(1), birthday_paulus)
|
|
|
|
|
|
|
|
self._send_time_changed(before_birthday)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-07-26 08:45:49 +00:00
|
|
|
self.assertEqual(0, len(runs))
|
|
|
|
|
|
|
|
self._send_time_changed(birthday_paulus)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-07-26 08:45:49 +00:00
|
|
|
self.assertEqual(1, len(runs))
|
|
|
|
|
|
|
|
# A point in time tracker will only fire once, this should do nothing
|
|
|
|
self._send_time_changed(birthday_paulus)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-07-26 08:45:49 +00:00
|
|
|
self.assertEqual(1, len(runs))
|
|
|
|
|
2015-08-04 16:16:10 +00:00
|
|
|
track_point_in_time(
|
2015-07-26 08:45:49 +00:00
|
|
|
self.hass, lambda x: runs.append(1), birthday_paulus)
|
|
|
|
|
|
|
|
self._send_time_changed(after_birthday)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-07-26 08:45:49 +00:00
|
|
|
self.assertEqual(2, len(runs))
|
|
|
|
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub = track_point_in_time(
|
|
|
|
self.hass, lambda x: runs.append(1), birthday_paulus)
|
|
|
|
unsub()
|
|
|
|
|
|
|
|
self._send_time_changed(after_birthday)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:35 +00:00
|
|
|
self.assertEqual(2, len(runs))
|
|
|
|
|
2015-07-26 08:45:49 +00:00
|
|
|
def test_track_time_change(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test tracking time change."""
|
2015-07-26 08:45:49 +00:00
|
|
|
wildcard_runs = []
|
|
|
|
specific_runs = []
|
|
|
|
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub = track_time_change(self.hass, lambda x: wildcard_runs.append(1))
|
|
|
|
unsub_utc = track_utc_time_change(
|
2015-07-26 08:45:49 +00:00
|
|
|
self.hass, lambda x: specific_runs.append(1), second=[0, 30])
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 24, 12, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-07-26 08:45:49 +00:00
|
|
|
self.assertEqual(1, len(specific_runs))
|
|
|
|
self.assertEqual(1, len(wildcard_runs))
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 24, 12, 0, 15))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-07-26 08:45:49 +00:00
|
|
|
self.assertEqual(1, len(specific_runs))
|
|
|
|
self.assertEqual(2, len(wildcard_runs))
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 24, 12, 0, 30))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-07-26 08:45:49 +00:00
|
|
|
self.assertEqual(2, len(specific_runs))
|
|
|
|
self.assertEqual(3, len(wildcard_runs))
|
|
|
|
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub()
|
|
|
|
unsub_utc()
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 24, 12, 0, 30))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:35 +00:00
|
|
|
self.assertEqual(2, len(specific_runs))
|
|
|
|
self.assertEqual(3, len(wildcard_runs))
|
|
|
|
|
2015-07-26 08:45:49 +00:00
|
|
|
def test_track_state_change(self):
|
2016-02-14 06:57:40 +00:00
|
|
|
"""Test track_state_change."""
|
2015-07-26 08:45:49 +00:00
|
|
|
# 2 lists to track how often our callbacks get called
|
|
|
|
specific_runs = []
|
|
|
|
wildcard_runs = []
|
2016-04-21 20:59:42 +00:00
|
|
|
wildercard_runs = []
|
2015-07-26 08:45:49 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
def specific_run_callback(entity_id, old_state, new_state):
|
|
|
|
specific_runs.append(1)
|
2015-07-26 08:45:49 +00:00
|
|
|
|
|
|
|
track_state_change(
|
2016-10-05 03:44:32 +00:00
|
|
|
self.hass, 'light.Bowl', specific_run_callback, 'on', 'off')
|
2016-04-21 20:59:42 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
@ha.callback
|
|
|
|
def wildcard_run_callback(entity_id, old_state, new_state):
|
|
|
|
wildcard_runs.append((old_state, new_state))
|
|
|
|
|
|
|
|
track_state_change(self.hass, 'light.Bowl', wildcard_run_callback)
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def wildercard_run_callback(entity_id, old_state, new_state):
|
|
|
|
wildercard_runs.append((old_state, new_state))
|
|
|
|
|
|
|
|
track_state_change(self.hass, MATCH_ALL, wildercard_run_callback)
|
2015-07-26 08:45:49 +00:00
|
|
|
|
2016-02-14 06:57:40 +00:00
|
|
|
# Adding state to state machine
|
|
|
|
self.hass.states.set("light.Bowl", "on")
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-02-14 06:57:40 +00:00
|
|
|
self.assertEqual(0, len(specific_runs))
|
|
|
|
self.assertEqual(1, len(wildcard_runs))
|
2016-04-21 20:59:42 +00:00
|
|
|
self.assertEqual(1, len(wildercard_runs))
|
2016-02-14 06:57:40 +00:00
|
|
|
self.assertIsNone(wildcard_runs[-1][0])
|
|
|
|
self.assertIsNotNone(wildcard_runs[-1][1])
|
|
|
|
|
2015-07-26 08:45:49 +00:00
|
|
|
# Set same state should not trigger a state change/listener
|
|
|
|
self.hass.states.set('light.Bowl', 'on')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-07-26 08:45:49 +00:00
|
|
|
self.assertEqual(0, len(specific_runs))
|
2016-02-14 06:57:40 +00:00
|
|
|
self.assertEqual(1, len(wildcard_runs))
|
2016-04-21 20:59:42 +00:00
|
|
|
self.assertEqual(1, len(wildercard_runs))
|
2015-07-26 08:45:49 +00:00
|
|
|
|
|
|
|
# State change off -> on
|
|
|
|
self.hass.states.set('light.Bowl', 'off')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-07-26 08:45:49 +00:00
|
|
|
self.assertEqual(1, len(specific_runs))
|
2016-02-14 06:57:40 +00:00
|
|
|
self.assertEqual(2, len(wildcard_runs))
|
2016-04-21 20:59:42 +00:00
|
|
|
self.assertEqual(2, len(wildercard_runs))
|
2015-07-26 08:45:49 +00:00
|
|
|
|
|
|
|
# State change off -> off
|
|
|
|
self.hass.states.set('light.Bowl', 'off', {"some_attr": 1})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-07-26 08:45:49 +00:00
|
|
|
self.assertEqual(1, len(specific_runs))
|
2016-02-14 06:57:40 +00:00
|
|
|
self.assertEqual(3, len(wildcard_runs))
|
2016-04-21 20:59:42 +00:00
|
|
|
self.assertEqual(3, len(wildercard_runs))
|
2015-07-26 08:45:49 +00:00
|
|
|
|
|
|
|
# State change off -> on
|
|
|
|
self.hass.states.set('light.Bowl', 'on')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-07-26 08:45:49 +00:00
|
|
|
self.assertEqual(1, len(specific_runs))
|
2016-02-14 06:57:40 +00:00
|
|
|
self.assertEqual(4, len(wildcard_runs))
|
2016-04-21 20:59:42 +00:00
|
|
|
self.assertEqual(4, len(wildercard_runs))
|
2016-02-14 06:57:40 +00:00
|
|
|
|
|
|
|
self.hass.states.remove('light.bowl')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-02-14 06:57:40 +00:00
|
|
|
self.assertEqual(1, len(specific_runs))
|
|
|
|
self.assertEqual(5, len(wildcard_runs))
|
2016-04-21 20:59:42 +00:00
|
|
|
self.assertEqual(5, len(wildercard_runs))
|
2016-02-14 06:57:40 +00:00
|
|
|
self.assertIsNotNone(wildcard_runs[-1][0])
|
|
|
|
self.assertIsNone(wildcard_runs[-1][1])
|
2016-04-21 20:59:42 +00:00
|
|
|
self.assertIsNotNone(wildercard_runs[-1][0])
|
|
|
|
self.assertIsNone(wildercard_runs[-1][1])
|
|
|
|
|
|
|
|
# Set state for different entity id
|
|
|
|
self.hass.states.set('switch.kitchen', 'on')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-04-21 20:59:42 +00:00
|
|
|
self.assertEqual(1, len(specific_runs))
|
|
|
|
self.assertEqual(5, len(wildcard_runs))
|
|
|
|
self.assertEqual(6, len(wildercard_runs))
|
2015-07-26 08:45:49 +00:00
|
|
|
|
2017-01-05 22:05:16 +00:00
|
|
|
def test_track_time_interval(self):
|
|
|
|
"""Test tracking time interval."""
|
|
|
|
specific_runs = []
|
|
|
|
|
|
|
|
utc_now = dt_util.utcnow()
|
|
|
|
unsub = track_time_interval(
|
|
|
|
self.hass, lambda x: specific_runs.append(1),
|
|
|
|
timedelta(seconds=10)
|
|
|
|
)
|
|
|
|
|
|
|
|
self._send_time_changed(utc_now + timedelta(seconds=5))
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.assertEqual(0, len(specific_runs))
|
|
|
|
|
|
|
|
self._send_time_changed(utc_now + timedelta(seconds=13))
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.assertEqual(1, len(specific_runs))
|
|
|
|
|
|
|
|
self._send_time_changed(utc_now + timedelta(minutes=20))
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.assertEqual(2, len(specific_runs))
|
|
|
|
|
|
|
|
unsub()
|
|
|
|
|
|
|
|
self._send_time_changed(utc_now + timedelta(seconds=30))
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.assertEqual(2, len(specific_runs))
|
|
|
|
|
2016-01-25 03:23:56 +00:00
|
|
|
def test_track_sunrise(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test track the sunrise."""
|
2016-01-25 03:23:56 +00:00
|
|
|
latitude = 32.87336
|
|
|
|
longitude = 117.22743
|
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
# Setup sun component
|
2016-01-25 03:23:56 +00:00
|
|
|
self.hass.config.latitude = latitude
|
|
|
|
self.hass.config.longitude = longitude
|
2016-09-26 21:20:36 +00:00
|
|
|
setup_component(self.hass, sun.DOMAIN, {
|
|
|
|
sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
|
2016-01-25 03:23:56 +00:00
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
# Get next sunrise/sunset
|
2016-01-25 03:23:56 +00:00
|
|
|
astral = Astral()
|
|
|
|
utc_now = dt_util.utcnow()
|
|
|
|
|
|
|
|
mod = -1
|
|
|
|
while True:
|
|
|
|
next_rising = (astral.sunrise_utc(utc_now +
|
|
|
|
timedelta(days=mod), latitude, longitude))
|
|
|
|
if next_rising > utc_now:
|
|
|
|
break
|
|
|
|
mod += 1
|
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
# Track sunrise
|
2016-01-25 03:23:56 +00:00
|
|
|
runs = []
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub = track_sunrise(self.hass, lambda: runs.append(1))
|
2016-01-25 03:23:56 +00:00
|
|
|
|
|
|
|
offset_runs = []
|
|
|
|
offset = timedelta(minutes=30)
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub2 = track_sunrise(self.hass, lambda: offset_runs.append(1),
|
|
|
|
offset)
|
2016-01-25 03:23:56 +00:00
|
|
|
|
|
|
|
# run tests
|
|
|
|
self._send_time_changed(next_rising - offset)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-25 03:23:56 +00:00
|
|
|
self.assertEqual(0, len(runs))
|
|
|
|
self.assertEqual(0, len(offset_runs))
|
|
|
|
|
|
|
|
self._send_time_changed(next_rising)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-25 03:23:56 +00:00
|
|
|
self.assertEqual(1, len(runs))
|
|
|
|
self.assertEqual(0, len(offset_runs))
|
|
|
|
|
|
|
|
self._send_time_changed(next_rising + offset)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-25 03:23:56 +00:00
|
|
|
self.assertEqual(2, len(runs))
|
|
|
|
self.assertEqual(1, len(offset_runs))
|
|
|
|
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub()
|
|
|
|
unsub2()
|
|
|
|
|
|
|
|
self._send_time_changed(next_rising + offset)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:35 +00:00
|
|
|
self.assertEqual(2, len(runs))
|
|
|
|
self.assertEqual(1, len(offset_runs))
|
|
|
|
|
2016-01-25 03:23:56 +00:00
|
|
|
def test_track_sunset(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test track the sunset."""
|
2016-01-25 03:23:56 +00:00
|
|
|
latitude = 32.87336
|
|
|
|
longitude = 117.22743
|
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
# Setup sun component
|
2016-01-25 03:23:56 +00:00
|
|
|
self.hass.config.latitude = latitude
|
|
|
|
self.hass.config.longitude = longitude
|
2016-09-26 21:20:36 +00:00
|
|
|
setup_component(self.hass, sun.DOMAIN, {
|
|
|
|
sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
|
2016-01-25 03:23:56 +00:00
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
# Get next sunrise/sunset
|
2016-01-25 03:23:56 +00:00
|
|
|
astral = Astral()
|
|
|
|
utc_now = dt_util.utcnow()
|
|
|
|
|
|
|
|
mod = -1
|
|
|
|
while True:
|
|
|
|
next_setting = (astral.sunset_utc(utc_now +
|
|
|
|
timedelta(days=mod), latitude, longitude))
|
|
|
|
if next_setting > utc_now:
|
|
|
|
break
|
|
|
|
mod += 1
|
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
# Track sunset
|
2016-01-25 03:23:56 +00:00
|
|
|
runs = []
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub = track_sunset(self.hass, lambda: runs.append(1))
|
2016-01-25 03:23:56 +00:00
|
|
|
|
|
|
|
offset_runs = []
|
|
|
|
offset = timedelta(minutes=30)
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub2 = track_sunset(self.hass, lambda: offset_runs.append(1), offset)
|
2016-01-25 03:23:56 +00:00
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
# Run tests
|
2016-01-25 03:23:56 +00:00
|
|
|
self._send_time_changed(next_setting - offset)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-25 03:23:56 +00:00
|
|
|
self.assertEqual(0, len(runs))
|
|
|
|
self.assertEqual(0, len(offset_runs))
|
|
|
|
|
|
|
|
self._send_time_changed(next_setting)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-25 03:23:56 +00:00
|
|
|
self.assertEqual(1, len(runs))
|
|
|
|
self.assertEqual(0, len(offset_runs))
|
|
|
|
|
|
|
|
self._send_time_changed(next_setting + offset)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-25 03:23:56 +00:00
|
|
|
self.assertEqual(2, len(runs))
|
|
|
|
self.assertEqual(1, len(offset_runs))
|
|
|
|
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub()
|
|
|
|
unsub2()
|
|
|
|
|
|
|
|
self._send_time_changed(next_setting + offset)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:35 +00:00
|
|
|
self.assertEqual(2, len(runs))
|
|
|
|
self.assertEqual(1, len(offset_runs))
|
|
|
|
|
2015-07-26 08:45:49 +00:00
|
|
|
def _send_time_changed(self, now):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Send a time changed event."""
|
2015-07-26 08:45:49 +00:00
|
|
|
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: now})
|
2016-01-26 09:28:31 +00:00
|
|
|
|
2016-01-26 17:37:19 +00:00
|
|
|
def test_periodic_task_minute(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test periodic tasks per minute."""
|
2016-01-26 09:28:31 +00:00
|
|
|
specific_runs = []
|
|
|
|
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub = track_utc_time_change(
|
2016-01-26 09:28:31 +00:00
|
|
|
self.hass, lambda x: specific_runs.append(1), minute='/5')
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 24, 12, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-26 09:28:31 +00:00
|
|
|
self.assertEqual(1, len(specific_runs))
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 24, 12, 3, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-26 09:28:31 +00:00
|
|
|
self.assertEqual(1, len(specific_runs))
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 24, 12, 5, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-26 09:28:31 +00:00
|
|
|
self.assertEqual(2, len(specific_runs))
|
2016-01-26 16:06:50 +00:00
|
|
|
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub()
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 24, 12, 5, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:35 +00:00
|
|
|
self.assertEqual(2, len(specific_runs))
|
|
|
|
|
2016-01-26 16:06:50 +00:00
|
|
|
def test_periodic_task_hour(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test periodic tasks per hour."""
|
2016-01-26 16:06:50 +00:00
|
|
|
specific_runs = []
|
|
|
|
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub = track_utc_time_change(
|
2016-01-26 16:06:50 +00:00
|
|
|
self.hass, lambda x: specific_runs.append(1), hour='/2')
|
|
|
|
|
2016-01-26 16:13:18 +00:00
|
|
|
self._send_time_changed(datetime(2014, 5, 24, 22, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-26 16:06:50 +00:00
|
|
|
self.assertEqual(1, len(specific_runs))
|
|
|
|
|
2016-01-26 16:13:18 +00:00
|
|
|
self._send_time_changed(datetime(2014, 5, 24, 23, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-26 16:06:50 +00:00
|
|
|
self.assertEqual(1, len(specific_runs))
|
|
|
|
|
2016-01-26 16:13:18 +00:00
|
|
|
self._send_time_changed(datetime(2014, 5, 24, 0, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-26 16:13:18 +00:00
|
|
|
self.assertEqual(2, len(specific_runs))
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 25, 1, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-26 16:13:18 +00:00
|
|
|
self.assertEqual(2, len(specific_runs))
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 25, 2, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-26 16:13:18 +00:00
|
|
|
self.assertEqual(3, len(specific_runs))
|
2016-01-26 17:37:19 +00:00
|
|
|
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub()
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 25, 2, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:35 +00:00
|
|
|
self.assertEqual(3, len(specific_runs))
|
|
|
|
|
2016-01-26 17:37:19 +00:00
|
|
|
def test_periodic_task_day(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test periodic tasks per day."""
|
2016-01-26 17:37:19 +00:00
|
|
|
specific_runs = []
|
|
|
|
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub = track_utc_time_change(
|
2016-01-26 17:37:19 +00:00
|
|
|
self.hass, lambda x: specific_runs.append(1), day='/2')
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 2, 0, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-26 17:37:19 +00:00
|
|
|
self.assertEqual(1, len(specific_runs))
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 3, 12, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-26 17:37:19 +00:00
|
|
|
self.assertEqual(1, len(specific_runs))
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 4, 0, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-26 17:37:19 +00:00
|
|
|
self.assertEqual(2, len(specific_runs))
|
2016-01-26 17:44:41 +00:00
|
|
|
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub()
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 4, 0, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:35 +00:00
|
|
|
self.assertEqual(2, len(specific_runs))
|
|
|
|
|
2016-01-26 17:44:41 +00:00
|
|
|
def test_periodic_task_year(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test periodic tasks per year."""
|
2016-01-26 17:44:41 +00:00
|
|
|
specific_runs = []
|
|
|
|
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub = track_utc_time_change(
|
2016-01-26 17:44:41 +00:00
|
|
|
self.hass, lambda x: specific_runs.append(1), year='/2')
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 2, 0, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-26 17:44:41 +00:00
|
|
|
self.assertEqual(1, len(specific_runs))
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2015, 5, 2, 0, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-26 17:44:41 +00:00
|
|
|
self.assertEqual(1, len(specific_runs))
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2016, 5, 2, 0, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-26 17:44:41 +00:00
|
|
|
self.assertEqual(2, len(specific_runs))
|
2016-01-26 19:43:29 +00:00
|
|
|
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub()
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2016, 5, 2, 0, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:35 +00:00
|
|
|
self.assertEqual(2, len(specific_runs))
|
|
|
|
|
2016-01-26 19:43:29 +00:00
|
|
|
def test_periodic_task_wrong_input(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test periodic tasks with wrong input."""
|
2016-01-26 19:43:29 +00:00
|
|
|
specific_runs = []
|
|
|
|
|
|
|
|
track_utc_time_change(
|
|
|
|
self.hass, lambda x: specific_runs.append(1), year='/two')
|
|
|
|
|
|
|
|
self._send_time_changed(datetime(2014, 5, 2, 0, 0, 0))
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-01-26 19:49:51 +00:00
|
|
|
self.assertEqual(0, len(specific_runs))
|