From 13fbefcdf8489183555b9aa985a106c8e40046f7 Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 10:28:31 +0100 Subject: [PATCH 01/17] 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)) From ec5d88b98e399e5a726763aed4e60440d07a8594 Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 17:06:50 +0100 Subject: [PATCH 02/17] hourly periodic task --- .gitignore | 1 + homeassistant/components/automation/time.py | 2 +- homeassistant/helpers/event.py | 7 ++++--- tests/helpers/test_event.py | 18 ++++++++++++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 90562a0f909..cee7b52c8a0 100644 --- a/.gitignore +++ b/.gitignore @@ -72,6 +72,7 @@ nosetests.xml # venv stuff pyvenv.cfg pip-selfcheck.json +venv # vimmy stuff *.swp diff --git a/homeassistant/components/automation/time.py b/homeassistant/components/automation/time.py index a289741a1f6..81acaeb0dfd 100644 --- a/homeassistant/components/automation/time.py +++ b/homeassistant/components/automation/time.py @@ -34,7 +34,7 @@ def trigger(hass, config, action): hours, minutes, seconds = after.hour, after.minute, after.second elif (CONF_HOURS in config or CONF_MINUTES in config or CONF_SECONDS in config): - hours = convert(config.get(CONF_HOURS), int) + hours = config.get(CONF_HOURS) minutes = config.get(CONF_MINUTES) seconds = config.get(CONF_SECONDS) else: diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index 9028e9e64b0..419c80189eb 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -162,7 +162,7 @@ def track_utc_time_change(hass, action, year=None, month=None, day=None, pmp = _process_match_param year, month, day = pmp(year), pmp(month), pmp(day) - hour, minute, second = pmp(hour), pmp(minute), pmp(second) + hour, minute, second = pmp(hour, rang=24), pmp(minute), pmp(second) @ft.wraps(action) def pattern_time_change_listener(event): @@ -196,12 +196,13 @@ def track_time_change(hass, action, year=None, month=None, day=None, local=True) -def _process_match_param(parameter): +def _process_match_param(parameter, rang=None): """ 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))) + rang = rang or 60 + return tuple(range(0, rang, 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 80937f8c33e..7c80a8de22f 100644 --- a/tests/helpers/test_event.py +++ b/tests/helpers/test_event.py @@ -237,3 +237,21 @@ class TestEventHelpers(unittest.TestCase): self._send_time_changed(datetime(2014, 5, 24, 12, 5, 0)) self.hass.pool.block_till_done() self.assertEqual(2, len(specific_runs)) + + def test_periodic_task_hour(self): + specific_runs = [] + + track_utc_time_change( + self.hass, lambda x: specific_runs.append(1), hour='/2') + + 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, 13, 0, 0)) + self.hass.pool.block_till_done() + self.assertEqual(1, len(specific_runs)) + + self._send_time_changed(datetime(2014, 5, 24, 14, 0, 0)) + self.hass.pool.block_till_done() + self.assertEqual(2, len(specific_runs)) \ No newline at end of file From a918be517d0c80bb05bb74e0cf5f96e12cad5e5a Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 17:13:18 +0100 Subject: [PATCH 03/17] test past midnight support for hours --- tests/helpers/test_event.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/helpers/test_event.py b/tests/helpers/test_event.py index 7c80a8de22f..4b8f984b32e 100644 --- a/tests/helpers/test_event.py +++ b/tests/helpers/test_event.py @@ -244,14 +244,22 @@ class TestEventHelpers(unittest.TestCase): track_utc_time_change( self.hass, lambda x: specific_runs.append(1), hour='/2') - self._send_time_changed(datetime(2014, 5, 24, 12, 0, 0)) + self._send_time_changed(datetime(2014, 5, 24, 22, 0, 0)) self.hass.pool.block_till_done() self.assertEqual(1, len(specific_runs)) - self._send_time_changed(datetime(2014, 5, 24, 13, 0, 0)) + self._send_time_changed(datetime(2014, 5, 24, 23, 0, 0)) self.hass.pool.block_till_done() self.assertEqual(1, len(specific_runs)) - self._send_time_changed(datetime(2014, 5, 24, 14, 0, 0)) + self._send_time_changed(datetime(2014, 5, 24, 0, 0, 0)) self.hass.pool.block_till_done() - self.assertEqual(2, len(specific_runs)) \ No newline at end of file + self.assertEqual(2, len(specific_runs)) + + self._send_time_changed(datetime(2014, 5, 25, 1, 0, 0)) + self.hass.pool.block_till_done() + self.assertEqual(2, len(specific_runs)) + + self._send_time_changed(datetime(2014, 5, 25, 2, 0, 0)) + self.hass.pool.block_till_done() + self.assertEqual(3, len(specific_runs)) From e8e28143139c89f37016eae08635ab94319c79f4 Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 17:30:55 +0100 Subject: [PATCH 04/17] Warnings for not divisible settings --- homeassistant/components/automation/time.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/homeassistant/components/automation/time.py b/homeassistant/components/automation/time.py index 81acaeb0dfd..1f532ec70aa 100644 --- a/homeassistant/components/automation/time.py +++ b/homeassistant/components/automation/time.py @@ -37,6 +37,15 @@ def trigger(hass, config, action): hours = config.get(CONF_HOURS) minutes = config.get(CONF_MINUTES) seconds = config.get(CONF_SECONDS) + if minutes.startswith('/') and not minutes.lstrip('/') % 60 == 0: + _LOGGER.warning('Periodic minutes should be divisible with 60' + 'there will be an offset every hour') + if seconds.startswith('/') and not seconds.lstrip('/') % 60 == 0: + _LOGGER.warning('Periodic seconds should be divisible with 60' + 'there will be an offset every minute') + if hours.startswith('/') and not hours.lstrip('/') % 24 == 0: + _LOGGER.warning('Periodic hours should be divisible with 24' + 'there will be an offset every midnight') else: _LOGGER.error('One of %s, %s, %s OR %s needs to be specified', CONF_HOURS, CONF_MINUTES, CONF_SECONDS, CONF_AFTER) From 6ae57b5aaff15f345afd759acd92f57c4d83bc03 Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 17:35:12 +0100 Subject: [PATCH 05/17] Some warnings if input is wrong for periodic tasks --- homeassistant/components/automation/time.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/automation/time.py b/homeassistant/components/automation/time.py index 1f532ec70aa..3d590ea8cfa 100644 --- a/homeassistant/components/automation/time.py +++ b/homeassistant/components/automation/time.py @@ -37,13 +37,16 @@ def trigger(hass, config, action): hours = config.get(CONF_HOURS) minutes = config.get(CONF_MINUTES) seconds = config.get(CONF_SECONDS) - if minutes.startswith('/') and not minutes.lstrip('/') % 60 == 0: + if isinstance(minutes, str) and minutes.startswith('/') \ + and not convert(minutes.lstrip('/'), int) % 60 == 0: _LOGGER.warning('Periodic minutes should be divisible with 60' 'there will be an offset every hour') - if seconds.startswith('/') and not seconds.lstrip('/') % 60 == 0: + if isinstance(seconds, str) and seconds.startswith('/') \ + and not convert(seconds.lstrip('/'), int) % 60 == 0: _LOGGER.warning('Periodic seconds should be divisible with 60' 'there will be an offset every minute') - if hours.startswith('/') and not hours.lstrip('/') % 24 == 0: + if isinstance(hours, str) and hours.startswith('/') \ + and not convert(hours.lstrip('/'), int) % 24 == 0: _LOGGER.warning('Periodic hours should be divisible with 24' 'there will be an offset every midnight') else: From 726637b8675c25c0e20c49fb450356cc78be5023 Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 18:37:19 +0100 Subject: [PATCH 06/17] New and improved handling of the matching! Kudos to @balloob --- homeassistant/helpers/event.py | 17 ++++++++++------- tests/helpers/test_event.py | 20 +++++++++++++++++++- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index 419c80189eb..e079fbcc444 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -1,7 +1,8 @@ """ Helpers for listening to events """ -from datetime import timedelta +from calendar import monthrange +from datetime import timedelta, date import functools as ft from ..util import convert @@ -162,7 +163,7 @@ def track_utc_time_change(hass, action, year=None, month=None, day=None, pmp = _process_match_param year, month, day = pmp(year), pmp(month), pmp(day) - hour, minute, second = pmp(hour, rang=24), pmp(minute), pmp(second) + hour, minute, second = pmp(hour), pmp(minute), pmp(second) @ft.wraps(action) def pattern_time_change_listener(event): @@ -171,7 +172,6 @@ def track_utc_time_change(hass, action, year=None, month=None, day=None, if local: now = dt_util.as_local(now) - mat = _matcher # pylint: disable=too-many-boolean-expressions @@ -196,13 +196,10 @@ def track_time_change(hass, action, year=None, month=None, day=None, local=True) -def _process_match_param(parameter, rang=None): +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('/'): - rang = rang or 60 - return tuple(range(0, rang, convert(parameter.lstrip('/'), int))) elif isinstance(parameter, str) or not hasattr(parameter, '__iter__'): return (parameter,) else: @@ -214,4 +211,10 @@ def _matcher(subject, pattern): Pattern is either a tuple of allowed subjects or a `MATCH_ALL`. """ + if isinstance(pattern, tuple) \ + and isinstance(pattern[0], str) and pattern[0].startswith('/'): + try: + return subject % float(pattern[0].lstrip('/')) == 0 + except ValueError: + return False return MATCH_ALL == pattern or subject in pattern diff --git a/tests/helpers/test_event.py b/tests/helpers/test_event.py index 4b8f984b32e..3e57e357f9c 100644 --- a/tests/helpers/test_event.py +++ b/tests/helpers/test_event.py @@ -220,7 +220,7 @@ class TestEventHelpers(unittest.TestCase): """ Send a time changed event. """ self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: now}) - def test_periodic_task(self): + def test_periodic_task_minute(self): specific_runs = [] track_utc_time_change( @@ -263,3 +263,21 @@ class TestEventHelpers(unittest.TestCase): self._send_time_changed(datetime(2014, 5, 25, 2, 0, 0)) self.hass.pool.block_till_done() self.assertEqual(3, len(specific_runs)) + + def test_periodic_task_day(self): + specific_runs = [] + + track_utc_time_change( + self.hass, lambda x: specific_runs.append(1), day='/2') + + self._send_time_changed(datetime(2014, 5, 2, 0, 0, 0)) + self.hass.pool.block_till_done() + self.assertEqual(1, len(specific_runs)) + + self._send_time_changed(datetime(2014, 5, 3, 12, 0, 0)) + self.hass.pool.block_till_done() + self.assertEqual(1, len(specific_runs)) + + self._send_time_changed(datetime(2014, 5, 4, 0, 0, 0)) + self.hass.pool.block_till_done() + self.assertEqual(2, len(specific_runs)) From cff77a175d4d59204245040e07d0524be9dc2c22 Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 18:44:41 +0100 Subject: [PATCH 07/17] Year periodic test (just to trigger travis but should maybe be here anyway) --- tests/helpers/test_event.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/helpers/test_event.py b/tests/helpers/test_event.py index 3e57e357f9c..0442fe9e2a0 100644 --- a/tests/helpers/test_event.py +++ b/tests/helpers/test_event.py @@ -281,3 +281,21 @@ class TestEventHelpers(unittest.TestCase): self._send_time_changed(datetime(2014, 5, 4, 0, 0, 0)) self.hass.pool.block_till_done() self.assertEqual(2, len(specific_runs)) + + def test_periodic_task_year(self): + specific_runs = [] + + track_utc_time_change( + self.hass, lambda x: specific_runs.append(1), year='/2') + + self._send_time_changed(datetime(2014, 5, 2, 0, 0, 0)) + self.hass.pool.block_till_done() + self.assertEqual(1, len(specific_runs)) + + self._send_time_changed(datetime(2015, 5, 2, 0, 0, 0)) + self.hass.pool.block_till_done() + self.assertEqual(1, len(specific_runs)) + + self._send_time_changed(datetime(2016, 5, 2, 0, 0, 0)) + self.hass.pool.block_till_done() + self.assertEqual(2, len(specific_runs)) From ebd475b38075f5f680218dbc59cca426647595c2 Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 18:50:25 +0100 Subject: [PATCH 08/17] Unused import was the problem --- homeassistant/helpers/event.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index e079fbcc444..42d182b77da 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -1,10 +1,8 @@ """ Helpers for listening to events """ -from calendar import monthrange -from datetime import timedelta, date +from datetime import timedelta import functools as ft -from ..util import convert from ..util import dt as dt_util from ..const import ( From 9a79ecf2d3728c88ec3325b66a04a23e6403dccf Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 19:25:41 +0100 Subject: [PATCH 09/17] Remove warnings no longer needed (I think :D) --- homeassistant/components/automation/time.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/homeassistant/components/automation/time.py b/homeassistant/components/automation/time.py index 3d590ea8cfa..5b8c89b429c 100644 --- a/homeassistant/components/automation/time.py +++ b/homeassistant/components/automation/time.py @@ -8,7 +8,6 @@ at https://home-assistant.io/components/automation/#time-trigger """ import logging -from homeassistant.util import convert import homeassistant.util.dt as dt_util from homeassistant.helpers.event import track_time_change @@ -37,18 +36,6 @@ def trigger(hass, config, action): hours = config.get(CONF_HOURS) minutes = config.get(CONF_MINUTES) seconds = config.get(CONF_SECONDS) - if isinstance(minutes, str) and minutes.startswith('/') \ - and not convert(minutes.lstrip('/'), int) % 60 == 0: - _LOGGER.warning('Periodic minutes should be divisible with 60' - 'there will be an offset every hour') - if isinstance(seconds, str) and seconds.startswith('/') \ - and not convert(seconds.lstrip('/'), int) % 60 == 0: - _LOGGER.warning('Periodic seconds should be divisible with 60' - 'there will be an offset every minute') - if isinstance(hours, str) and hours.startswith('/') \ - and not convert(hours.lstrip('/'), int) % 24 == 0: - _LOGGER.warning('Periodic hours should be divisible with 24' - 'there will be an offset every midnight') else: _LOGGER.error('One of %s, %s, %s OR %s needs to be specified', CONF_HOURS, CONF_MINUTES, CONF_SECONDS, CONF_AFTER) From 1f6f2de9c6a645d5fa5e7ec544d122eac7d547b7 Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 19:37:53 +0100 Subject: [PATCH 10/17] Sorry, I was wrong they are needed --- homeassistant/components/automation/time.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/homeassistant/components/automation/time.py b/homeassistant/components/automation/time.py index 5b8c89b429c..0197ae07d29 100644 --- a/homeassistant/components/automation/time.py +++ b/homeassistant/components/automation/time.py @@ -8,6 +8,7 @@ at https://home-assistant.io/components/automation/#time-trigger """ import logging +from homeassistant.util import convert import homeassistant.util.dt as dt_util from homeassistant.helpers.event import track_time_change @@ -36,6 +37,18 @@ def trigger(hass, config, action): hours = config.get(CONF_HOURS) minutes = config.get(CONF_MINUTES) seconds = config.get(CONF_SECONDS) + if isinstance(minutes, str) and minutes.startswith('/') \ + and not convert(minutes.lstrip('/'), int) % 60 == 0: + _LOGGER.warning('Periodic minutes should be divisible with 60' + 'there will be an offset every hour') + if isinstance(seconds, str) and seconds.startswith('/') \ + and not convert(seconds.lstrip('/'), int) % 60 == 0: + _LOGGER.warning('Periodic seconds should be divisible with 60' + 'there will be an offset every minute') + if isinstance(minutes, str) and hours.startswith('/') \ + and not convert(hours.lstrip('/'), int) % 24 == 0: + _LOGGER.warning('Periodic hours should be divisible with 24' + 'there will be an offset every midnight') else: _LOGGER.error('One of %s, %s, %s OR %s needs to be specified', CONF_HOURS, CONF_MINUTES, CONF_SECONDS, CONF_AFTER) From 33b0f4d05d5e7479fce392053763f98d650085b3 Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 20:43:29 +0100 Subject: [PATCH 11/17] Fixes bug in time trigger and adds test for ex. /two --- homeassistant/components/automation/time.py | 2 +- homeassistant/helpers/event.py | 1 + tests/helpers/test_event.py | 10 ++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/automation/time.py b/homeassistant/components/automation/time.py index 0197ae07d29..3d590ea8cfa 100644 --- a/homeassistant/components/automation/time.py +++ b/homeassistant/components/automation/time.py @@ -45,7 +45,7 @@ def trigger(hass, config, action): and not convert(seconds.lstrip('/'), int) % 60 == 0: _LOGGER.warning('Periodic seconds should be divisible with 60' 'there will be an offset every minute') - if isinstance(minutes, str) and hours.startswith('/') \ + if isinstance(hours, str) and hours.startswith('/') \ and not convert(hours.lstrip('/'), int) % 24 == 0: _LOGGER.warning('Periodic hours should be divisible with 24' 'there will be an offset every midnight') diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index 42d182b77da..6ca186a1744 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -215,4 +215,5 @@ def _matcher(subject, pattern): return subject % float(pattern[0].lstrip('/')) == 0 except ValueError: return False + return MATCH_ALL == pattern or subject in pattern diff --git a/tests/helpers/test_event.py b/tests/helpers/test_event.py index 0442fe9e2a0..2f760ee1382 100644 --- a/tests/helpers/test_event.py +++ b/tests/helpers/test_event.py @@ -299,3 +299,13 @@ class TestEventHelpers(unittest.TestCase): self._send_time_changed(datetime(2016, 5, 2, 0, 0, 0)) self.hass.pool.block_till_done() self.assertEqual(2, len(specific_runs)) + + def test_periodic_task_wrong_input(self): + 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)) + self.hass.pool.block_till_done() + self.assertEqual(1, len(specific_runs)) From 2382dffbf4e267485241aaba2db16f83c265fc7d Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 20:49:51 +0100 Subject: [PATCH 12/17] pushed test code :) fixed now --- tests/helpers/test_event.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/helpers/test_event.py b/tests/helpers/test_event.py index 2f760ee1382..aef7b834016 100644 --- a/tests/helpers/test_event.py +++ b/tests/helpers/test_event.py @@ -308,4 +308,4 @@ class TestEventHelpers(unittest.TestCase): self._send_time_changed(datetime(2014, 5, 2, 0, 0, 0)) self.hass.pool.block_till_done() - self.assertEqual(1, len(specific_runs)) + self.assertEqual(0, len(specific_runs)) From d4b444823c49a53c48865f242509b64ebbe8c07b Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 21:11:30 +0100 Subject: [PATCH 13/17] Tests for time and a fix for wrong access of _LOGGER --- homeassistant/components/automation/time.py | 2 +- tests/components/automation/test_time.py | 57 +++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/automation/time.py b/homeassistant/components/automation/time.py index 3d590ea8cfa..4ef55820e61 100644 --- a/homeassistant/components/automation/time.py +++ b/homeassistant/components/automation/time.py @@ -71,7 +71,7 @@ def if_action(hass, config): weekday = config.get(CONF_WEEKDAY) if before is None and after is None and weekday is None: - logging.getLogger(__name__).error( + _LOGGER.error( "Missing if-condition configuration key %s, %s or %s", CONF_BEFORE, CONF_AFTER, CONF_WEEKDAY) return None diff --git a/tests/components/automation/test_time.py b/tests/components/automation/test_time.py index e233c93988d..991ca8dbc5a 100644 --- a/tests/components/automation/test_time.py +++ b/tests/components/automation/test_time.py @@ -301,6 +301,63 @@ class TestAutomationTime(unittest.TestCase): self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) + def test_if_fires_periodic_seconds(self): + self.assertTrue(automation.setup(self.hass, { + automation.DOMAIN: { + 'trigger': { + 'platform': 'time', + 'seconds': "/2", + }, + 'action': { + 'service': 'test.automation' + } + } + })) + + fire_time_changed(self.hass, dt_util.utcnow().replace( + hour=0, minute=0, second=2)) + + self.hass.pool.block_till_done() + self.assertEqual(1, len(self.calls)) + + def test_if_fires_periodic_minutes(self): + self.assertTrue(automation.setup(self.hass, { + automation.DOMAIN: { + 'trigger': { + 'platform': 'time', + 'minutes': "/2", + }, + 'action': { + 'service': 'test.automation' + } + } + })) + + fire_time_changed(self.hass, dt_util.utcnow().replace( + hour=0, minute=2, second=0)) + + self.hass.pool.block_till_done() + self.assertEqual(1, len(self.calls)) + + def test_if_fires_periodic_hours(self): + self.assertTrue(automation.setup(self.hass, { + automation.DOMAIN: { + 'trigger': { + 'platform': 'time', + 'hours': "/2", + }, + 'action': { + 'service': 'test.automation' + } + } + })) + + fire_time_changed(self.hass, dt_util.utcnow().replace( + hour=2, minute=0, second=0)) + + self.hass.pool.block_till_done() + self.assertEqual(1, len(self.calls)) + def test_if_fires_using_after(self): self.assertTrue(automation.setup(self.hass, { automation.DOMAIN: { From 0a4e85790134185667ec6c6f7f636740faf16eb2 Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 21:21:54 +0100 Subject: [PATCH 14/17] And a test just to see if nothing works if not providing any vars --- tests/components/automation/test_time.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/components/automation/test_time.py b/tests/components/automation/test_time.py index 991ca8dbc5a..dd3f5a6e9fa 100644 --- a/tests/components/automation/test_time.py +++ b/tests/components/automation/test_time.py @@ -377,6 +377,24 @@ class TestAutomationTime(unittest.TestCase): self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) + def test_if_not_working_if_no_values_in_conf_provided(self): + self.assertTrue(automation.setup(self.hass, { + automation.DOMAIN: { + 'trigger': { + 'platform': 'time', + }, + 'action': { + 'service': 'test.automation' + } + } + })) + + fire_time_changed(self.hass, dt_util.utcnow().replace( + hour=5, minute=0, second=0)) + + self.hass.pool.block_till_done() + self.assertEqual(0, len(self.calls)) + @patch('homeassistant.components.automation.time._LOGGER.error') def test_if_not_fires_using_wrong_after(self, mock_error): """ YAML translates time values to total seconds. This should break the From 2c3a6e7905569f7b4dc49fe004f378ed26e86ba9 Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 21:30:00 +0100 Subject: [PATCH 15/17] Remove not needed log warning --- homeassistant/components/automation/time.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/homeassistant/components/automation/time.py b/homeassistant/components/automation/time.py index 4ef55820e61..2f2173dce55 100644 --- a/homeassistant/components/automation/time.py +++ b/homeassistant/components/automation/time.py @@ -37,18 +37,6 @@ def trigger(hass, config, action): hours = config.get(CONF_HOURS) minutes = config.get(CONF_MINUTES) seconds = config.get(CONF_SECONDS) - if isinstance(minutes, str) and minutes.startswith('/') \ - and not convert(minutes.lstrip('/'), int) % 60 == 0: - _LOGGER.warning('Periodic minutes should be divisible with 60' - 'there will be an offset every hour') - if isinstance(seconds, str) and seconds.startswith('/') \ - and not convert(seconds.lstrip('/'), int) % 60 == 0: - _LOGGER.warning('Periodic seconds should be divisible with 60' - 'there will be an offset every minute') - if isinstance(hours, str) and hours.startswith('/') \ - and not convert(hours.lstrip('/'), int) % 24 == 0: - _LOGGER.warning('Periodic hours should be divisible with 24' - 'there will be an offset every midnight') else: _LOGGER.error('One of %s, %s, %s OR %s needs to be specified', CONF_HOURS, CONF_MINUTES, CONF_SECONDS, CONF_AFTER) From ca070a36e30198e00c10073ac6644ae9144f9395 Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 21:38:07 +0100 Subject: [PATCH 16/17] Dont wrap '/int' parameters with tuple --- homeassistant/helpers/event.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index 6ca186a1744..0f0deac58b1 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -198,6 +198,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 parameter elif isinstance(parameter, str) or not hasattr(parameter, '__iter__'): return (parameter,) else: @@ -209,10 +211,9 @@ def _matcher(subject, pattern): Pattern is either a tuple of allowed subjects or a `MATCH_ALL`. """ - if isinstance(pattern, tuple) \ - and isinstance(pattern[0], str) and pattern[0].startswith('/'): + if isinstance(pattern, str) and pattern.startswith('/'): try: - return subject % float(pattern[0].lstrip('/')) == 0 + return subject % float(pattern.lstrip('/')) == 0 except ValueError: return False From 6add5e387b8a17045bdb66e50d7877aa7888408b Mon Sep 17 00:00:00 2001 From: magnusknutas Date: Tue, 26 Jan 2016 21:41:12 +0100 Subject: [PATCH 17/17] Remove unused import --- homeassistant/components/automation/time.py | 1 - 1 file changed, 1 deletion(-) diff --git a/homeassistant/components/automation/time.py b/homeassistant/components/automation/time.py index 2f2173dce55..d02765f75c6 100644 --- a/homeassistant/components/automation/time.py +++ b/homeassistant/components/automation/time.py @@ -8,7 +8,6 @@ at https://home-assistant.io/components/automation/#time-trigger """ import logging -from homeassistant.util import convert import homeassistant.util.dt as dt_util from homeassistant.helpers.event import track_time_change