From 7876cdb37a7de94e7ff0ffe1d11362d745ceacb9 Mon Sep 17 00:00:00 2001 From: Anders Melchiorsen Date: Wed, 23 Sep 2020 09:21:57 +0200 Subject: [PATCH] Fix handling of quoted time_pattern values (#40470) Co-authored-by: J. Nick Koston Co-authored-by: Franck Nijhof --- .../homeassistant/triggers/time_pattern.py | 2 +- .../triggers/test_time_pattern.py | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/homeassistant/triggers/time_pattern.py b/homeassistant/components/homeassistant/triggers/time_pattern.py index adacc939870..5f03fb593d6 100644 --- a/homeassistant/components/homeassistant/triggers/time_pattern.py +++ b/homeassistant/components/homeassistant/triggers/time_pattern.py @@ -36,7 +36,7 @@ class TimePattern: if isinstance(value, str) and value.startswith("/"): number = int(value[1:]) else: - number = int(value) + value = number = int(value) if not (0 <= number <= self.maximum): raise vol.Invalid(f"must be a value between 0 and {self.maximum}") diff --git a/tests/components/homeassistant/triggers/test_time_pattern.py b/tests/components/homeassistant/triggers/test_time_pattern.py index 0ef071aadb6..3d32748c176 100644 --- a/tests/components/homeassistant/triggers/test_time_pattern.py +++ b/tests/components/homeassistant/triggers/test_time_pattern.py @@ -1,4 +1,6 @@ """The tests for the time_pattern automation.""" +from datetime import timedelta + import pytest import voluptuous as vol @@ -123,6 +125,39 @@ async def test_if_fires_when_second_matches(hass, calls): assert len(calls) == 1 +async def test_if_fires_when_second_as_string_matches(hass, calls): + """Test for firing if seconds are matching.""" + now = dt_util.utcnow() + time_that_will_not_match_right_away = dt_util.utcnow().replace( + year=now.year + 1, second=15 + ) + with patch( + "homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away + ): + assert await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "trigger": { + "platform": "time_pattern", + "hours": "*", + "minutes": "*", + "seconds": "30", + }, + "action": {"service": "test.automation"}, + } + }, + ) + + async_fire_time_changed( + hass, time_that_will_not_match_right_away + timedelta(seconds=15) + ) + + await hass.async_block_till_done() + assert len(calls) == 1 + + async def test_if_fires_when_all_matches(hass, calls): """Test for firing if everything matches.""" now = dt_util.utcnow()