From c5dc99c05211a144ec57f1b9509c9b08e6993fef Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 1 Jun 2021 17:57:23 +0200 Subject: [PATCH] Fix time condition microsecond offset when using input helpers (#51337) --- homeassistant/helpers/condition.py | 1 - tests/helpers/test_condition.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/homeassistant/helpers/condition.py b/homeassistant/helpers/condition.py index 23816b94a65..a467d952683 100644 --- a/homeassistant/helpers/condition.py +++ b/homeassistant/helpers/condition.py @@ -752,7 +752,6 @@ def time( before_entity.attributes.get("hour", 23), before_entity.attributes.get("minute", 59), before_entity.attributes.get("second", 59), - 999999, ) if after < before: diff --git a/tests/helpers/test_condition.py b/tests/helpers/test_condition.py index 9347d0bc025..2290ce9f679 100644 --- a/tests/helpers/test_condition.py +++ b/tests/helpers/test_condition.py @@ -791,6 +791,34 @@ async def test_time_using_input_datetime(hass): hass, after="input_datetime.pm", before="input_datetime.am" ) + # Trigger on PM time + with patch( + "homeassistant.helpers.condition.dt_util.now", + return_value=dt_util.now().replace(hour=18, minute=0, second=0), + ): + assert condition.time( + hass, after="input_datetime.pm", before="input_datetime.am" + ) + assert not condition.time( + hass, after="input_datetime.am", before="input_datetime.pm" + ) + assert condition.time(hass, after="input_datetime.pm") + assert not condition.time(hass, before="input_datetime.pm") + + # Trigger on AM time + with patch( + "homeassistant.helpers.condition.dt_util.now", + return_value=dt_util.now().replace(hour=6, minute=0, second=0), + ): + assert not condition.time( + hass, after="input_datetime.pm", before="input_datetime.am" + ) + assert condition.time( + hass, after="input_datetime.am", before="input_datetime.pm" + ) + assert condition.time(hass, after="input_datetime.am") + assert not condition.time(hass, before="input_datetime.am") + with pytest.raises(ConditionError): condition.time(hass, after="input_datetime.not_existing")