From 13fbefcdf8489183555b9aa985a106c8e40046f7 Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 10:28:31 +0100 Subject: [PATCH] Running periodic tasks --- homeassistant/components/automation/time.py | 4 ++-- homeassistant/helpers/event.py | 3 +++ tests/helpers/test_event.py | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/automation/time.py b/homeassistant/components/automation/time.py index e8cf9c3b6ee..a289741a1f6 100644 --- a/homeassistant/components/automation/time.py +++ b/homeassistant/components/automation/time.py @@ -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) diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index 42725b8eea9..9028e9e64b0 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -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: diff --git a/tests/helpers/test_event.py b/tests/helpers/test_event.py index e12ca0c4124..80937f8c33e 100644 --- a/tests/helpers/test_event.py +++ b/tests/helpers/test_event.py @@ -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))