diff --git a/homeassistant/helpers/condition.py b/homeassistant/helpers/condition.py index 138fa81947c..b6030c61a1c 100644 --- a/homeassistant/helpers/condition.py +++ b/homeassistant/helpers/condition.py @@ -97,7 +97,7 @@ def condition_trace_set_result(result: bool, **kwargs: Any) -> None: node.set_result(result=result, **kwargs) -def condition_trace_update_result(result: bool, **kwargs: Any) -> None: +def condition_trace_update_result(**kwargs: Any) -> None: """Update the result of TraceElement at the top of the stack.""" node = trace_stack_top(trace_stack_cv) @@ -106,7 +106,7 @@ def condition_trace_update_result(result: bool, **kwargs: Any) -> None: if not node: return - node.update_result(result=result, **kwargs) + node.update_result(**kwargs) @contextmanager @@ -131,7 +131,7 @@ def trace_condition_function(condition: ConditionCheckerType) -> ConditionChecke """Trace condition.""" with trace_condition(variables): result = condition(hass, variables) - condition_trace_update_result(result) + condition_trace_update_result(result=result) return result return wrapper @@ -607,23 +607,37 @@ def sun( if sunrise is None and SUN_EVENT_SUNRISE in (before, after): # There is no sunrise today + condition_trace_set_result(False, message="no sunrise today") return False if sunset is None and SUN_EVENT_SUNSET in (before, after): # There is no sunset today + condition_trace_set_result(False, message="no sunset today") return False - if before == SUN_EVENT_SUNRISE and utcnow > cast(datetime, sunrise) + before_offset: - return False + if before == SUN_EVENT_SUNRISE: + wanted_time_before = cast(datetime, sunrise) + before_offset + condition_trace_update_result(wanted_time_before=wanted_time_before) + if utcnow > wanted_time_before: + return False - if before == SUN_EVENT_SUNSET and utcnow > cast(datetime, sunset) + before_offset: - return False + if before == SUN_EVENT_SUNSET: + wanted_time_before = cast(datetime, sunset) + before_offset + condition_trace_update_result(wanted_time_before=wanted_time_before) + if utcnow > wanted_time_before: + return False - if after == SUN_EVENT_SUNRISE and utcnow < cast(datetime, sunrise) + after_offset: - return False + if after == SUN_EVENT_SUNRISE: + wanted_time_after = cast(datetime, sunrise) + after_offset + condition_trace_update_result(wanted_time_after=wanted_time_after) + if utcnow < wanted_time_after: + return False - if after == SUN_EVENT_SUNSET and utcnow < cast(datetime, sunset) + after_offset: - return False + if after == SUN_EVENT_SUNSET: + wanted_time_after = cast(datetime, sunset) + after_offset + condition_trace_update_result(wanted_time_after=wanted_time_after) + if utcnow < wanted_time_after: + return False return True diff --git a/tests/components/sun/test_trigger.py b/tests/components/sun/test_trigger.py index 54dcef96e28..6e6a1f11ef9 100644 --- a/tests/components/sun/test_trigger.py +++ b/tests/components/sun/test_trigger.py @@ -168,695 +168,3 @@ async def test_sunrise_trigger_with_offset(hass, calls, legacy_patchable_time): async_fire_time_changed(hass, trigger_time) await hass.async_block_till_done() assert len(calls) == 1 - - -async def test_if_action_before_sunrise_no_offset(hass, calls): - """ - Test if action was before sunrise. - - Before sunrise is true from midnight until sunset, local time. - """ - await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": {"platform": "event", "event_type": "test_event"}, - "condition": {"condition": "sun", "before": SUN_EVENT_SUNRISE}, - "action": {"service": "test.automation"}, - } - }, - ) - - # sunrise: 2015-09-16 06:33:18 local, sunset: 2015-09-16 18:53:45 local - # sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC - # now = sunrise + 1s -> 'before sunrise' not true - now = datetime(2015, 9, 16, 13, 33, 19, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 0 - - # now = sunrise -> 'before sunrise' true - now = datetime(2015, 9, 16, 13, 33, 18, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = local midnight -> 'before sunrise' true - now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - # now = local midnight - 1s -> 'before sunrise' not true - now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - -async def test_if_action_after_sunrise_no_offset(hass, calls): - """ - Test if action was after sunrise. - - After sunrise is true from sunrise until midnight, local time. - """ - await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": {"platform": "event", "event_type": "test_event"}, - "condition": {"condition": "sun", "after": SUN_EVENT_SUNRISE}, - "action": {"service": "test.automation"}, - } - }, - ) - - # sunrise: 2015-09-16 06:33:18 local, sunset: 2015-09-16 18:53:45 local - # sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC - # now = sunrise - 1s -> 'after sunrise' not true - now = datetime(2015, 9, 16, 13, 33, 17, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 0 - - # now = sunrise + 1s -> 'after sunrise' true - now = datetime(2015, 9, 16, 13, 33, 19, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = local midnight -> 'after sunrise' not true - now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = local midnight - 1s -> 'after sunrise' true - now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - -async def test_if_action_before_sunrise_with_offset(hass, calls): - """ - Test if action was before sunrise with offset. - - Before sunrise is true from midnight until sunset, local time. - """ - await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": {"platform": "event", "event_type": "test_event"}, - "condition": { - "condition": "sun", - "before": SUN_EVENT_SUNRISE, - "before_offset": "+1:00:00", - }, - "action": {"service": "test.automation"}, - } - }, - ) - - # sunrise: 2015-09-16 06:33:18 local, sunset: 2015-09-16 18:53:45 local - # sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC - # now = sunrise + 1s + 1h -> 'before sunrise' with offset +1h not true - now = datetime(2015, 9, 16, 14, 33, 19, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 0 - - # now = sunrise + 1h -> 'before sunrise' with offset +1h true - now = datetime(2015, 9, 16, 14, 33, 18, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = UTC midnight -> 'before sunrise' with offset +1h not true - now = datetime(2015, 9, 17, 0, 0, 0, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = UTC midnight - 1s -> 'before sunrise' with offset +1h not true - now = datetime(2015, 9, 16, 23, 59, 59, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = local midnight -> 'before sunrise' with offset +1h true - now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - # now = local midnight - 1s -> 'before sunrise' with offset +1h not true - now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - # now = sunset -> 'before sunrise' with offset +1h not true - now = datetime(2015, 9, 17, 1, 53, 45, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - # now = sunset -1s -> 'before sunrise' with offset +1h not true - now = datetime(2015, 9, 17, 1, 53, 44, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - -async def test_if_action_before_sunset_with_offset(hass, calls): - """ - Test if action was before sunset with offset. - - Before sunset is true from midnight until sunset, local time. - """ - await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": {"platform": "event", "event_type": "test_event"}, - "condition": { - "condition": "sun", - "before": "sunset", - "before_offset": "+1:00:00", - }, - "action": {"service": "test.automation"}, - } - }, - ) - - # sunrise: 2015-09-16 06:33:18 local, sunset: 2015-09-16 18:53:45 local - # sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC - # now = local midnight -> 'before sunset' with offset +1h true - now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = sunset + 1s + 1h -> 'before sunset' with offset +1h not true - now = datetime(2015, 9, 17, 2, 53, 46, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = sunset + 1h -> 'before sunset' with offset +1h true - now = datetime(2015, 9, 17, 2, 53, 44, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - # now = UTC midnight -> 'before sunset' with offset +1h true - now = datetime(2015, 9, 17, 0, 0, 0, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 3 - - # now = UTC midnight - 1s -> 'before sunset' with offset +1h true - now = datetime(2015, 9, 16, 23, 59, 59, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 4 - - # now = sunrise -> 'before sunset' with offset +1h true - now = datetime(2015, 9, 16, 13, 33, 18, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 5 - - # now = sunrise -1s -> 'before sunset' with offset +1h true - now = datetime(2015, 9, 16, 13, 33, 17, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 6 - - # now = local midnight-1s -> 'after sunrise' with offset +1h not true - now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 6 - - -async def test_if_action_after_sunrise_with_offset(hass, calls): - """ - Test if action was after sunrise with offset. - - After sunrise is true from sunrise until midnight, local time. - """ - await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": {"platform": "event", "event_type": "test_event"}, - "condition": { - "condition": "sun", - "after": SUN_EVENT_SUNRISE, - "after_offset": "+1:00:00", - }, - "action": {"service": "test.automation"}, - } - }, - ) - - # sunrise: 2015-09-16 06:33:18 local, sunset: 2015-09-16 18:53:45 local - # sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC - # now = sunrise - 1s + 1h -> 'after sunrise' with offset +1h not true - now = datetime(2015, 9, 16, 14, 33, 17, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 0 - - # now = sunrise + 1h -> 'after sunrise' with offset +1h true - now = datetime(2015, 9, 16, 14, 33, 58, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = UTC noon -> 'after sunrise' with offset +1h not true - now = datetime(2015, 9, 16, 12, 0, 0, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = UTC noon - 1s -> 'after sunrise' with offset +1h not true - now = datetime(2015, 9, 16, 11, 59, 59, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = local noon -> 'after sunrise' with offset +1h true - now = datetime(2015, 9, 16, 19, 1, 0, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - # now = local noon - 1s -> 'after sunrise' with offset +1h true - now = datetime(2015, 9, 16, 18, 59, 59, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 3 - - # now = sunset -> 'after sunrise' with offset +1h true - now = datetime(2015, 9, 17, 1, 53, 45, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 4 - - # now = sunset + 1s -> 'after sunrise' with offset +1h true - now = datetime(2015, 9, 17, 1, 53, 45, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 5 - - # now = local midnight-1s -> 'after sunrise' with offset +1h true - now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 6 - - # now = local midnight -> 'after sunrise' with offset +1h not true - now = datetime(2015, 9, 17, 7, 0, 0, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 6 - - -async def test_if_action_after_sunset_with_offset(hass, calls): - """ - Test if action was after sunset with offset. - - After sunset is true from sunset until midnight, local time. - """ - await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": {"platform": "event", "event_type": "test_event"}, - "condition": { - "condition": "sun", - "after": "sunset", - "after_offset": "+1:00:00", - }, - "action": {"service": "test.automation"}, - } - }, - ) - - # sunrise: 2015-09-16 06:33:18 local, sunset: 2015-09-16 18:53:45 local - # sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC - # now = sunset - 1s + 1h -> 'after sunset' with offset +1h not true - now = datetime(2015, 9, 17, 2, 53, 44, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 0 - - # now = sunset + 1h -> 'after sunset' with offset +1h true - now = datetime(2015, 9, 17, 2, 53, 45, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = midnight-1s -> 'after sunset' with offset +1h true - now = datetime(2015, 9, 16, 6, 59, 59, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - # now = midnight -> 'after sunset' with offset +1h not true - now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - -async def test_if_action_before_and_after_during(hass, calls): - """ - Test if action was after sunset and before sunrise. - - This is true from sunrise until sunset. - """ - await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": {"platform": "event", "event_type": "test_event"}, - "condition": { - "condition": "sun", - "after": SUN_EVENT_SUNRISE, - "before": SUN_EVENT_SUNSET, - }, - "action": {"service": "test.automation"}, - } - }, - ) - - # sunrise: 2015-09-16 06:33:18 local, sunset: 2015-09-16 18:53:45 local - # sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC - # now = sunrise - 1s -> 'after sunrise' + 'before sunset' not true - now = datetime(2015, 9, 16, 13, 33, 17, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 0 - - # now = sunset + 1s -> 'after sunrise' + 'before sunset' not true - now = datetime(2015, 9, 17, 1, 53, 46, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 0 - - # now = sunrise + 1s -> 'after sunrise' + 'before sunset' true - now = datetime(2015, 9, 16, 13, 33, 19, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = sunset - 1s -> 'after sunrise' + 'before sunset' true - now = datetime(2015, 9, 17, 1, 53, 44, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - # now = 9AM local -> 'after sunrise' + 'before sunset' true - now = datetime(2015, 9, 16, 16, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 3 - - -async def test_if_action_before_sunrise_no_offset_kotzebue(hass, calls): - """ - Test if action was before sunrise. - - Local timezone: Alaska time - Location: Kotzebue, which has a very skewed local timezone with sunrise - at 7 AM and sunset at 3AM during summer - After sunrise is true from sunrise until midnight, local time. - """ - tz = dt_util.get_time_zone("America/Anchorage") - dt_util.set_default_time_zone(tz) - hass.config.latitude = 66.5 - hass.config.longitude = 162.4 - await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": {"platform": "event", "event_type": "test_event"}, - "condition": {"condition": "sun", "before": SUN_EVENT_SUNRISE}, - "action": {"service": "test.automation"}, - } - }, - ) - - # sunrise: 2015-07-24 07:21:12 local, sunset: 2015-07-25 03:13:33 local - # sunrise: 2015-07-24 15:21:12 UTC, sunset: 2015-07-25 11:13:33 UTC - # now = sunrise + 1s -> 'before sunrise' not true - now = datetime(2015, 7, 24, 15, 21, 13, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 0 - - # now = sunrise - 1h -> 'before sunrise' true - now = datetime(2015, 7, 24, 14, 21, 12, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = local midnight -> 'before sunrise' true - now = datetime(2015, 7, 24, 8, 0, 0, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - # now = local midnight - 1s -> 'before sunrise' not true - now = datetime(2015, 7, 24, 7, 59, 59, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - -async def test_if_action_after_sunrise_no_offset_kotzebue(hass, calls): - """ - Test if action was after sunrise. - - Local timezone: Alaska time - Location: Kotzebue, which has a very skewed local timezone with sunrise - at 7 AM and sunset at 3AM during summer - Before sunrise is true from midnight until sunrise, local time. - """ - tz = dt_util.get_time_zone("America/Anchorage") - dt_util.set_default_time_zone(tz) - hass.config.latitude = 66.5 - hass.config.longitude = 162.4 - await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": {"platform": "event", "event_type": "test_event"}, - "condition": {"condition": "sun", "after": SUN_EVENT_SUNRISE}, - "action": {"service": "test.automation"}, - } - }, - ) - - # sunrise: 2015-07-24 07:21:12 local, sunset: 2015-07-25 03:13:33 local - # sunrise: 2015-07-24 15:21:12 UTC, sunset: 2015-07-25 11:13:33 UTC - # now = sunrise -> 'after sunrise' true - now = datetime(2015, 7, 24, 15, 21, 12, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = sunrise - 1h -> 'after sunrise' not true - now = datetime(2015, 7, 24, 14, 21, 12, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = local midnight -> 'after sunrise' not true - now = datetime(2015, 7, 24, 8, 0, 1, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = local midnight - 1s -> 'after sunrise' true - now = datetime(2015, 7, 24, 7, 59, 59, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - -async def test_if_action_before_sunset_no_offset_kotzebue(hass, calls): - """ - Test if action was before sunrise. - - Local timezone: Alaska time - Location: Kotzebue, which has a very skewed local timezone with sunrise - at 7 AM and sunset at 3AM during summer - Before sunset is true from midnight until sunset, local time. - """ - tz = dt_util.get_time_zone("America/Anchorage") - dt_util.set_default_time_zone(tz) - hass.config.latitude = 66.5 - hass.config.longitude = 162.4 - await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": {"platform": "event", "event_type": "test_event"}, - "condition": {"condition": "sun", "before": SUN_EVENT_SUNSET}, - "action": {"service": "test.automation"}, - } - }, - ) - - # sunrise: 2015-07-24 07:21:12 local, sunset: 2015-07-25 03:13:33 local - # sunrise: 2015-07-24 15:21:12 UTC, sunset: 2015-07-25 11:13:33 UTC - # now = sunset + 1s -> 'before sunset' not true - now = datetime(2015, 7, 25, 11, 13, 34, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 0 - - # now = sunset - 1h-> 'before sunset' true - now = datetime(2015, 7, 25, 10, 13, 33, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = local midnight -> 'before sunrise' true - now = datetime(2015, 7, 24, 8, 0, 0, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - # now = local midnight - 1s -> 'before sunrise' not true - now = datetime(2015, 7, 24, 7, 59, 59, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 - - -async def test_if_action_after_sunset_no_offset_kotzebue(hass, calls): - """ - Test if action was after sunrise. - - Local timezone: Alaska time - Location: Kotzebue, which has a very skewed local timezone with sunrise - at 7 AM and sunset at 3AM during summer - After sunset is true from sunset until midnight, local time. - """ - tz = dt_util.get_time_zone("America/Anchorage") - dt_util.set_default_time_zone(tz) - hass.config.latitude = 66.5 - hass.config.longitude = 162.4 - await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "trigger": {"platform": "event", "event_type": "test_event"}, - "condition": {"condition": "sun", "after": SUN_EVENT_SUNSET}, - "action": {"service": "test.automation"}, - } - }, - ) - - # sunrise: 2015-07-24 07:21:12 local, sunset: 2015-07-25 03:13:33 local - # sunrise: 2015-07-24 15:21:12 UTC, sunset: 2015-07-25 11:13:33 UTC - # now = sunset -> 'after sunset' true - now = datetime(2015, 7, 25, 11, 13, 33, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = sunset - 1s -> 'after sunset' not true - now = datetime(2015, 7, 25, 11, 13, 32, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = local midnight -> 'after sunset' not true - now = datetime(2015, 7, 24, 8, 0, 1, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 1 - - # now = local midnight - 1s -> 'after sunset' true - now = datetime(2015, 7, 24, 7, 59, 59, tzinfo=dt_util.UTC) - with patch("homeassistant.util.dt.utcnow", return_value=now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 2 diff --git a/tests/helpers/test_condition.py b/tests/helpers/test_condition.py index d46c343dfb1..11705502e77 100644 --- a/tests/helpers/test_condition.py +++ b/tests/helpers/test_condition.py @@ -1,13 +1,41 @@ """Test the condition helper.""" +from datetime import datetime from unittest.mock import patch import pytest +from homeassistant.components import sun +import homeassistant.components.automation as automation +from homeassistant.const import SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET from homeassistant.exceptions import ConditionError, HomeAssistantError from homeassistant.helpers import condition, trace from homeassistant.helpers.template import Template from homeassistant.setup import async_setup_component -from homeassistant.util import dt +import homeassistant.util.dt as dt_util + +from tests.common import async_mock_service + +ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE + + +@pytest.fixture +def calls(hass): + """Track calls to a mock service.""" + return async_mock_service(hass, "test", "automation") + + +@pytest.fixture(autouse=True) +def setup_comp(hass): + """Initialize components.""" + dt_util.set_default_time_zone(hass.config.time_zone) + hass.loop.run_until_complete( + async_setup_component(hass, sun.DOMAIN, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}}) + ) + + +def teardown(): + """Restore.""" + dt_util.set_default_time_zone(ORIG_TIME_ZONE) def assert_element(trace_element, expected_element, path): @@ -651,28 +679,28 @@ async def test_time_window(hass): with patch( "homeassistant.helpers.condition.dt_util.now", - return_value=dt.now().replace(hour=3), + return_value=dt_util.now().replace(hour=3), ): assert not test1(hass) assert test2(hass) with patch( "homeassistant.helpers.condition.dt_util.now", - return_value=dt.now().replace(hour=9), + return_value=dt_util.now().replace(hour=9), ): assert test1(hass) assert not test2(hass) with patch( "homeassistant.helpers.condition.dt_util.now", - return_value=dt.now().replace(hour=15), + return_value=dt_util.now().replace(hour=15), ): assert test1(hass) assert not test2(hass) with patch( "homeassistant.helpers.condition.dt_util.now", - return_value=dt.now().replace(hour=21), + return_value=dt_util.now().replace(hour=21), ): assert not test1(hass) assert test2(hass) @@ -697,7 +725,7 @@ async def test_time_using_input_datetime(hass): { "entity_id": "input_datetime.am", "datetime": str( - dt.now() + dt_util.now() .replace(hour=6, minute=0, second=0, microsecond=0) .replace(tzinfo=None) ), @@ -711,7 +739,7 @@ async def test_time_using_input_datetime(hass): { "entity_id": "input_datetime.pm", "datetime": str( - dt.now() + dt_util.now() .replace(hour=18, minute=0, second=0, microsecond=0) .replace(tzinfo=None) ), @@ -721,7 +749,7 @@ async def test_time_using_input_datetime(hass): with patch( "homeassistant.helpers.condition.dt_util.now", - return_value=dt.now().replace(hour=3), + return_value=dt_util.now().replace(hour=3), ): assert not condition.time( hass, after="input_datetime.am", before="input_datetime.pm" @@ -732,7 +760,7 @@ async def test_time_using_input_datetime(hass): with patch( "homeassistant.helpers.condition.dt_util.now", - return_value=dt.now().replace(hour=9), + return_value=dt_util.now().replace(hour=9), ): assert condition.time( hass, after="input_datetime.am", before="input_datetime.pm" @@ -743,7 +771,7 @@ async def test_time_using_input_datetime(hass): with patch( "homeassistant.helpers.condition.dt_util.now", - return_value=dt.now().replace(hour=15), + return_value=dt_util.now().replace(hour=15), ): assert condition.time( hass, after="input_datetime.am", before="input_datetime.pm" @@ -754,7 +782,7 @@ async def test_time_using_input_datetime(hass): with patch( "homeassistant.helpers.condition.dt_util.now", - return_value=dt.now().replace(hour=21), + return_value=dt_util.now().replace(hour=21), ): assert not condition.time( hass, after="input_datetime.am", before="input_datetime.pm" @@ -1628,3 +1656,1063 @@ async def test_condition_template_invalid_results(hass): hass, {"condition": "template", "value_template": "{{ [1, 2, 3] }}"} ) assert not test(hass) + + +def _find_run_id(traces, trace_type, item_id): + """Find newest run_id for a script or automation.""" + for _trace in reversed(traces): + if _trace["domain"] == trace_type and _trace["item_id"] == item_id: + return _trace["run_id"] + + return None + + +async def assert_automation_condition_trace(hass_ws_client, automation_id, expected): + """Test the result of automation condition.""" + id = 1 + + def next_id(): + nonlocal id + id += 1 + return id + + client = await hass_ws_client() + + # List traces + await client.send_json( + {"id": next_id(), "type": "trace/list", "domain": "automation"} + ) + response = await client.receive_json() + assert response["success"] + run_id = _find_run_id(response["result"], "automation", automation_id) + + # Get trace + await client.send_json( + { + "id": next_id(), + "type": "trace/get", + "domain": "automation", + "item_id": "sun", + "run_id": run_id, + } + ) + response = await client.receive_json() + assert response["success"] + trace = response["result"] + assert len(trace["trace"]["condition/0"]) == 1 + condition_trace = trace["trace"]["condition/0"][0]["result"] + assert condition_trace == expected + + +async def test_if_action_before_sunrise_no_offset(hass, hass_ws_client, calls): + """ + Test if action was before sunrise. + + Before sunrise is true from midnight until sunset, local time. + """ + await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "id": "sun", + "trigger": {"platform": "event", "event_type": "test_event"}, + "condition": {"condition": "sun", "before": SUN_EVENT_SUNRISE}, + "action": {"service": "test.automation"}, + } + }, + ) + + # sunrise: 2015-09-16 06:33:18 local, sunset: 2015-09-16 18:53:45 local + # sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC + # now = sunrise + 1s -> 'before sunrise' not true + now = datetime(2015, 9, 16, 13, 33, 19, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 0 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_before": "2015-09-16T13:33:18.342542+00:00"}, + ) + + # now = sunrise -> 'before sunrise' true + now = datetime(2015, 9, 16, 13, 33, 18, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_before": "2015-09-16T13:33:18.342542+00:00"}, + ) + + # now = local midnight -> 'before sunrise' true + now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_before": "2015-09-16T13:33:18.342542+00:00"}, + ) + + # now = local midnight - 1s -> 'before sunrise' not true + now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_before": "2015-09-16T13:33:18.342542+00:00"}, + ) + + +async def test_if_action_after_sunrise_no_offset(hass, hass_ws_client, calls): + """ + Test if action was after sunrise. + + After sunrise is true from sunrise until midnight, local time. + """ + await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "id": "sun", + "trigger": {"platform": "event", "event_type": "test_event"}, + "condition": {"condition": "sun", "after": SUN_EVENT_SUNRISE}, + "action": {"service": "test.automation"}, + } + }, + ) + + # sunrise: 2015-09-16 06:33:18 local, sunset: 2015-09-16 18:53:45 local + # sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC + # now = sunrise - 1s -> 'after sunrise' not true + now = datetime(2015, 9, 16, 13, 33, 17, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 0 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_after": "2015-09-16T13:33:18.342542+00:00"}, + ) + + # now = sunrise + 1s -> 'after sunrise' true + now = datetime(2015, 9, 16, 13, 33, 19, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_after": "2015-09-16T13:33:18.342542+00:00"}, + ) + + # now = local midnight -> 'after sunrise' not true + now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_after": "2015-09-16T13:33:18.342542+00:00"}, + ) + + # now = local midnight - 1s -> 'after sunrise' true + now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_after": "2015-09-16T13:33:18.342542+00:00"}, + ) + + +async def test_if_action_before_sunrise_with_offset(hass, hass_ws_client, calls): + """ + Test if action was before sunrise with offset. + + Before sunrise is true from midnight until sunset, local time. + """ + await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "id": "sun", + "trigger": {"platform": "event", "event_type": "test_event"}, + "condition": { + "condition": "sun", + "before": SUN_EVENT_SUNRISE, + "before_offset": "+1:00:00", + }, + "action": {"service": "test.automation"}, + } + }, + ) + + # sunrise: 2015-09-16 06:33:18 local, sunset: 2015-09-16 18:53:45 local + # sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC + # now = sunrise + 1s + 1h -> 'before sunrise' with offset +1h not true + now = datetime(2015, 9, 16, 14, 33, 19, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 0 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_before": "2015-09-16T14:33:18.342542+00:00"}, + ) + + # now = sunrise + 1h -> 'before sunrise' with offset +1h true + now = datetime(2015, 9, 16, 14, 33, 18, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_before": "2015-09-16T14:33:18.342542+00:00"}, + ) + + # now = UTC midnight -> 'before sunrise' with offset +1h not true + now = datetime(2015, 9, 17, 0, 0, 0, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_before": "2015-09-16T14:33:18.342542+00:00"}, + ) + + # now = UTC midnight - 1s -> 'before sunrise' with offset +1h not true + now = datetime(2015, 9, 16, 23, 59, 59, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_before": "2015-09-16T14:33:18.342542+00:00"}, + ) + + # now = local midnight -> 'before sunrise' with offset +1h true + now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_before": "2015-09-16T14:33:18.342542+00:00"}, + ) + + # now = local midnight - 1s -> 'before sunrise' with offset +1h not true + now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_before": "2015-09-16T14:33:18.342542+00:00"}, + ) + + # now = sunset -> 'before sunrise' with offset +1h not true + now = datetime(2015, 9, 17, 1, 53, 45, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_before": "2015-09-16T14:33:18.342542+00:00"}, + ) + + # now = sunset -1s -> 'before sunrise' with offset +1h not true + now = datetime(2015, 9, 17, 1, 53, 44, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_before": "2015-09-16T14:33:18.342542+00:00"}, + ) + + +async def test_if_action_before_sunset_with_offset(hass, hass_ws_client, calls): + """ + Test if action was before sunset with offset. + + Before sunset is true from midnight until sunset, local time. + """ + await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "id": "sun", + "trigger": {"platform": "event", "event_type": "test_event"}, + "condition": { + "condition": "sun", + "before": "sunset", + "before_offset": "+1:00:00", + }, + "action": {"service": "test.automation"}, + } + }, + ) + + # sunrise: 2015-09-16 06:33:18 local, sunset: 2015-09-16 18:53:45 local + # sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC + # now = local midnight -> 'before sunset' with offset +1h true + now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_before": "2015-09-17T02:53:44.723614+00:00"}, + ) + + # now = sunset + 1s + 1h -> 'before sunset' with offset +1h not true + now = datetime(2015, 9, 17, 2, 53, 46, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_before": "2015-09-17T02:53:44.723614+00:00"}, + ) + + # now = sunset + 1h -> 'before sunset' with offset +1h true + now = datetime(2015, 9, 17, 2, 53, 44, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_before": "2015-09-17T02:53:44.723614+00:00"}, + ) + + # now = UTC midnight -> 'before sunset' with offset +1h true + now = datetime(2015, 9, 17, 0, 0, 0, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 3 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_before": "2015-09-17T02:53:44.723614+00:00"}, + ) + + # now = UTC midnight - 1s -> 'before sunset' with offset +1h true + now = datetime(2015, 9, 16, 23, 59, 59, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 4 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_before": "2015-09-17T02:53:44.723614+00:00"}, + ) + + # now = sunrise -> 'before sunset' with offset +1h true + now = datetime(2015, 9, 16, 13, 33, 18, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 5 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_before": "2015-09-17T02:53:44.723614+00:00"}, + ) + + # now = sunrise -1s -> 'before sunset' with offset +1h true + now = datetime(2015, 9, 16, 13, 33, 17, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 6 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_before": "2015-09-17T02:53:44.723614+00:00"}, + ) + + # now = local midnight-1s -> 'after sunrise' with offset +1h not true + now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 6 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_before": "2015-09-17T02:53:44.723614+00:00"}, + ) + + +async def test_if_action_after_sunrise_with_offset(hass, hass_ws_client, calls): + """ + Test if action was after sunrise with offset. + + After sunrise is true from sunrise until midnight, local time. + """ + await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "id": "sun", + "trigger": {"platform": "event", "event_type": "test_event"}, + "condition": { + "condition": "sun", + "after": SUN_EVENT_SUNRISE, + "after_offset": "+1:00:00", + }, + "action": {"service": "test.automation"}, + } + }, + ) + + # sunrise: 2015-09-16 06:33:18 local, sunset: 2015-09-16 18:53:45 local + # sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC + # now = sunrise - 1s + 1h -> 'after sunrise' with offset +1h not true + now = datetime(2015, 9, 16, 14, 33, 17, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 0 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_after": "2015-09-16T14:33:18.342542+00:00"}, + ) + + # now = sunrise + 1h -> 'after sunrise' with offset +1h true + now = datetime(2015, 9, 16, 14, 33, 58, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_after": "2015-09-16T14:33:18.342542+00:00"}, + ) + + # now = UTC noon -> 'after sunrise' with offset +1h not true + now = datetime(2015, 9, 16, 12, 0, 0, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_after": "2015-09-16T14:33:18.342542+00:00"}, + ) + + # now = UTC noon - 1s -> 'after sunrise' with offset +1h not true + now = datetime(2015, 9, 16, 11, 59, 59, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_after": "2015-09-16T14:33:18.342542+00:00"}, + ) + + # now = local noon -> 'after sunrise' with offset +1h true + now = datetime(2015, 9, 16, 19, 1, 0, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_after": "2015-09-16T14:33:18.342542+00:00"}, + ) + + # now = local noon - 1s -> 'after sunrise' with offset +1h true + now = datetime(2015, 9, 16, 18, 59, 59, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 3 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_after": "2015-09-16T14:33:18.342542+00:00"}, + ) + + # now = sunset -> 'after sunrise' with offset +1h true + now = datetime(2015, 9, 17, 1, 53, 45, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 4 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_after": "2015-09-16T14:33:18.342542+00:00"}, + ) + + # now = sunset + 1s -> 'after sunrise' with offset +1h true + now = datetime(2015, 9, 17, 1, 53, 45, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 5 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_after": "2015-09-16T14:33:18.342542+00:00"}, + ) + + # now = local midnight-1s -> 'after sunrise' with offset +1h true + now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 6 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_after": "2015-09-16T14:33:18.342542+00:00"}, + ) + + # now = local midnight -> 'after sunrise' with offset +1h not true + now = datetime(2015, 9, 17, 7, 0, 0, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 6 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_after": "2015-09-17T14:33:57.053037+00:00"}, + ) + + +async def test_if_action_after_sunset_with_offset(hass, hass_ws_client, calls): + """ + Test if action was after sunset with offset. + + After sunset is true from sunset until midnight, local time. + """ + await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "id": "sun", + "trigger": {"platform": "event", "event_type": "test_event"}, + "condition": { + "condition": "sun", + "after": "sunset", + "after_offset": "+1:00:00", + }, + "action": {"service": "test.automation"}, + } + }, + ) + + # sunrise: 2015-09-16 06:33:18 local, sunset: 2015-09-16 18:53:45 local + # sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC + # now = sunset - 1s + 1h -> 'after sunset' with offset +1h not true + now = datetime(2015, 9, 17, 2, 53, 44, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 0 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_after": "2015-09-17T02:53:44.723614+00:00"}, + ) + + # now = sunset + 1h -> 'after sunset' with offset +1h true + now = datetime(2015, 9, 17, 2, 53, 45, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_after": "2015-09-17T02:53:44.723614+00:00"}, + ) + + # now = midnight-1s -> 'after sunset' with offset +1h true + now = datetime(2015, 9, 16, 6, 59, 59, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_after": "2015-09-16T02:55:06.099767+00:00"}, + ) + + # now = midnight -> 'after sunset' with offset +1h not true + now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_after": "2015-09-17T02:53:44.723614+00:00"}, + ) + + +async def test_if_action_before_and_after_during(hass, hass_ws_client, calls): + """ + Test if action was after sunset and before sunrise. + + This is true from sunrise until sunset. + """ + await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "id": "sun", + "trigger": {"platform": "event", "event_type": "test_event"}, + "condition": { + "condition": "sun", + "after": SUN_EVENT_SUNRISE, + "before": SUN_EVENT_SUNSET, + }, + "action": {"service": "test.automation"}, + } + }, + ) + + # sunrise: 2015-09-16 06:33:18 local, sunset: 2015-09-16 18:53:45 local + # sunrise: 2015-09-16 13:33:18 UTC, sunset: 2015-09-17 01:53:45 UTC + # now = sunrise - 1s -> 'after sunrise' + 'before sunset' not true + now = datetime(2015, 9, 16, 13, 33, 17, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 0 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + { + "result": False, + "wanted_time_before": "2015-09-17T01:53:44.723614+00:00", + "wanted_time_after": "2015-09-16T13:33:18.342542+00:00", + }, + ) + + # now = sunset + 1s -> 'after sunrise' + 'before sunset' not true + now = datetime(2015, 9, 17, 1, 53, 46, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 0 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_before": "2015-09-17T01:53:44.723614+00:00"}, + ) + + # now = sunrise + 1s -> 'after sunrise' + 'before sunset' true + now = datetime(2015, 9, 16, 13, 33, 19, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + { + "result": True, + "wanted_time_before": "2015-09-17T01:53:44.723614+00:00", + "wanted_time_after": "2015-09-16T13:33:18.342542+00:00", + }, + ) + + # now = sunset - 1s -> 'after sunrise' + 'before sunset' true + now = datetime(2015, 9, 17, 1, 53, 44, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + { + "result": True, + "wanted_time_before": "2015-09-17T01:53:44.723614+00:00", + "wanted_time_after": "2015-09-16T13:33:18.342542+00:00", + }, + ) + + # now = 9AM local -> 'after sunrise' + 'before sunset' true + now = datetime(2015, 9, 16, 16, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 3 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + { + "result": True, + "wanted_time_before": "2015-09-17T01:53:44.723614+00:00", + "wanted_time_after": "2015-09-16T13:33:18.342542+00:00", + }, + ) + + +async def test_if_action_before_sunrise_no_offset_kotzebue(hass, hass_ws_client, calls): + """ + Test if action was before sunrise. + + Local timezone: Alaska time + Location: Kotzebue, which has a very skewed local timezone with sunrise + at 7 AM and sunset at 3AM during summer + After sunrise is true from sunrise until midnight, local time. + """ + tz = dt_util.get_time_zone("America/Anchorage") + dt_util.set_default_time_zone(tz) + hass.config.latitude = 66.5 + hass.config.longitude = 162.4 + await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "id": "sun", + "trigger": {"platform": "event", "event_type": "test_event"}, + "condition": {"condition": "sun", "before": SUN_EVENT_SUNRISE}, + "action": {"service": "test.automation"}, + } + }, + ) + + # sunrise: 2015-07-24 07:21:12 local, sunset: 2015-07-25 03:13:33 local + # sunrise: 2015-07-24 15:21:12 UTC, sunset: 2015-07-25 11:13:33 UTC + # now = sunrise + 1s -> 'before sunrise' not true + now = datetime(2015, 7, 24, 15, 21, 13, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 0 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_before": "2015-07-24T15:16:46.975735+00:00"}, + ) + + # now = sunrise - 1h -> 'before sunrise' true + now = datetime(2015, 7, 24, 14, 21, 12, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_before": "2015-07-24T15:16:46.975735+00:00"}, + ) + + # now = local midnight -> 'before sunrise' true + now = datetime(2015, 7, 24, 8, 0, 0, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_before": "2015-07-24T15:16:46.975735+00:00"}, + ) + + # now = local midnight - 1s -> 'before sunrise' not true + now = datetime(2015, 7, 24, 7, 59, 59, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_before": "2015-07-23T15:12:19.155123+00:00"}, + ) + + +async def test_if_action_after_sunrise_no_offset_kotzebue(hass, hass_ws_client, calls): + """ + Test if action was after sunrise. + + Local timezone: Alaska time + Location: Kotzebue, which has a very skewed local timezone with sunrise + at 7 AM and sunset at 3AM during summer + Before sunrise is true from midnight until sunrise, local time. + """ + tz = dt_util.get_time_zone("America/Anchorage") + dt_util.set_default_time_zone(tz) + hass.config.latitude = 66.5 + hass.config.longitude = 162.4 + await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "id": "sun", + "trigger": {"platform": "event", "event_type": "test_event"}, + "condition": {"condition": "sun", "after": SUN_EVENT_SUNRISE}, + "action": {"service": "test.automation"}, + } + }, + ) + + # sunrise: 2015-07-24 07:21:12 local, sunset: 2015-07-25 03:13:33 local + # sunrise: 2015-07-24 15:21:12 UTC, sunset: 2015-07-25 11:13:33 UTC + # now = sunrise -> 'after sunrise' true + now = datetime(2015, 7, 24, 15, 21, 12, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_after": "2015-07-24T15:16:46.975735+00:00"}, + ) + + # now = sunrise - 1h -> 'after sunrise' not true + now = datetime(2015, 7, 24, 14, 21, 12, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_after": "2015-07-24T15:16:46.975735+00:00"}, + ) + + # now = local midnight -> 'after sunrise' not true + now = datetime(2015, 7, 24, 8, 0, 1, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_after": "2015-07-24T15:16:46.975735+00:00"}, + ) + + # now = local midnight - 1s -> 'after sunrise' true + now = datetime(2015, 7, 24, 7, 59, 59, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_after": "2015-07-23T15:12:19.155123+00:00"}, + ) + + +async def test_if_action_before_sunset_no_offset_kotzebue(hass, hass_ws_client, calls): + """ + Test if action was before sunrise. + + Local timezone: Alaska time + Location: Kotzebue, which has a very skewed local timezone with sunrise + at 7 AM and sunset at 3AM during summer + Before sunset is true from midnight until sunset, local time. + """ + tz = dt_util.get_time_zone("America/Anchorage") + dt_util.set_default_time_zone(tz) + hass.config.latitude = 66.5 + hass.config.longitude = 162.4 + await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "id": "sun", + "trigger": {"platform": "event", "event_type": "test_event"}, + "condition": {"condition": "sun", "before": SUN_EVENT_SUNSET}, + "action": {"service": "test.automation"}, + } + }, + ) + + # sunrise: 2015-07-24 07:21:12 local, sunset: 2015-07-25 03:13:33 local + # sunrise: 2015-07-24 15:21:12 UTC, sunset: 2015-07-25 11:13:33 UTC + # now = sunset + 1s -> 'before sunset' not true + now = datetime(2015, 7, 25, 11, 13, 34, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 0 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_before": "2015-07-25T11:13:32.501837+00:00"}, + ) + + # now = sunset - 1h-> 'before sunset' true + now = datetime(2015, 7, 25, 10, 13, 33, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_before": "2015-07-25T11:13:32.501837+00:00"}, + ) + + # now = local midnight -> 'before sunrise' true + now = datetime(2015, 7, 24, 8, 0, 0, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_before": "2015-07-24T11:17:54.446913+00:00"}, + ) + + # now = local midnight - 1s -> 'before sunrise' not true + now = datetime(2015, 7, 24, 7, 59, 59, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_before": "2015-07-23T11:22:18.467277+00:00"}, + ) + + +async def test_if_action_after_sunset_no_offset_kotzebue(hass, hass_ws_client, calls): + """ + Test if action was after sunrise. + + Local timezone: Alaska time + Location: Kotzebue, which has a very skewed local timezone with sunrise + at 7 AM and sunset at 3AM during summer + After sunset is true from sunset until midnight, local time. + """ + tz = dt_util.get_time_zone("America/Anchorage") + dt_util.set_default_time_zone(tz) + hass.config.latitude = 66.5 + hass.config.longitude = 162.4 + await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "id": "sun", + "trigger": {"platform": "event", "event_type": "test_event"}, + "condition": {"condition": "sun", "after": SUN_EVENT_SUNSET}, + "action": {"service": "test.automation"}, + } + }, + ) + + # sunrise: 2015-07-24 07:21:12 local, sunset: 2015-07-25 03:13:33 local + # sunrise: 2015-07-24 15:21:12 UTC, sunset: 2015-07-25 11:13:33 UTC + # now = sunset -> 'after sunset' true + now = datetime(2015, 7, 25, 11, 13, 33, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_after": "2015-07-25T11:13:32.501837+00:00"}, + ) + + # now = sunset - 1s -> 'after sunset' not true + now = datetime(2015, 7, 25, 11, 13, 32, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_after": "2015-07-25T11:13:32.501837+00:00"}, + ) + + # now = local midnight -> 'after sunset' not true + now = datetime(2015, 7, 24, 8, 0, 1, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": False, "wanted_time_after": "2015-07-24T11:17:54.446913+00:00"}, + ) + + # now = local midnight - 1s -> 'after sunset' true + now = datetime(2015, 7, 24, 7, 59, 59, tzinfo=dt_util.UTC) + with patch("homeassistant.util.dt.utcnow", return_value=now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_after": "2015-07-23T11:22:18.467277+00:00"}, + )