Running periodic tasks

This commit is contained in:
magnusknutas 2016-01-26 10:28:31 +01:00
parent e72f61ce73
commit 13fbefcdf8
3 changed files with 23 additions and 2 deletions

View File

@ -35,8 +35,8 @@ def trigger(hass, config, action):
elif (CONF_HOURS in config or CONF_MINUTES in config or
CONF_SECONDS in config):
hours = convert(config.get(CONF_HOURS), int)
minutes = convert(config.get(CONF_MINUTES), int)
seconds = convert(config.get(CONF_SECONDS), int)
minutes = config.get(CONF_MINUTES)
seconds = config.get(CONF_SECONDS)
else:
_LOGGER.error('One of %s, %s, %s OR %s needs to be specified',
CONF_HOURS, CONF_MINUTES, CONF_SECONDS, CONF_AFTER)

View File

@ -3,6 +3,7 @@ Helpers for listening to events
"""
from datetime import timedelta
import functools as ft
from ..util import convert
from ..util import dt as dt_util
from ..const import (
@ -199,6 +200,8 @@ def _process_match_param(parameter):
""" Wraps parameter in a tuple if it is not one and returns it. """
if parameter is None or parameter == MATCH_ALL:
return MATCH_ALL
elif isinstance(parameter, str) and parameter.startswith('/'):
return tuple(range(0, 60, convert(parameter.lstrip('/'), int)))
elif isinstance(parameter, str) or not hasattr(parameter, '__iter__'):
return (parameter,)
else:

View File

@ -219,3 +219,21 @@ class TestEventHelpers(unittest.TestCase):
def _send_time_changed(self, now):
""" Send a time changed event. """
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: now})
def test_periodic_task(self):
specific_runs = []
track_utc_time_change(
self.hass, lambda x: specific_runs.append(1), minute='/5')
self._send_time_changed(datetime(2014, 5, 24, 12, 0, 0))
self.hass.pool.block_till_done()
self.assertEqual(1, len(specific_runs))
self._send_time_changed(datetime(2014, 5, 24, 12, 3, 0))
self.hass.pool.block_till_done()
self.assertEqual(1, len(specific_runs))
self._send_time_changed(datetime(2014, 5, 24, 12, 5, 0))
self.hass.pool.block_till_done()
self.assertEqual(2, len(specific_runs))