diff --git a/homeassistant/components/sun/condition.py b/homeassistant/components/sun/condition.py new file mode 100644 index 00000000000..c52ada51e06 --- /dev/null +++ b/homeassistant/components/sun/condition.py @@ -0,0 +1,136 @@ +"""Offer sun based automation rules.""" + +from __future__ import annotations + +from datetime import datetime, timedelta +from typing import cast + +import voluptuous as vol + +from homeassistant.const import CONF_CONDITION, SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET +from homeassistant.core import HomeAssistant +from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.condition import ( + ConditionCheckerType, + condition_trace_set_result, + condition_trace_update_result, + trace_condition_function, +) +from homeassistant.helpers.sun import get_astral_event_date +from homeassistant.helpers.typing import ConfigType, TemplateVarsType +from homeassistant.util import dt as dt_util + +CONDITION_SCHEMA = vol.All( + vol.Schema( + { + **cv.CONDITION_BASE_SCHEMA, + vol.Required(CONF_CONDITION): "sun", + vol.Optional("before"): cv.sun_event, + vol.Optional("before_offset"): cv.time_period, + vol.Optional("after"): vol.All( + vol.Lower, vol.Any(SUN_EVENT_SUNSET, SUN_EVENT_SUNRISE) + ), + vol.Optional("after_offset"): cv.time_period, + } + ), + cv.has_at_least_one_key("before", "after"), +) + + +def sun( + hass: HomeAssistant, + before: str | None = None, + after: str | None = None, + before_offset: timedelta | None = None, + after_offset: timedelta | None = None, +) -> bool: + """Test if current time matches sun requirements.""" + utcnow = dt_util.utcnow() + today = dt_util.as_local(utcnow).date() + before_offset = before_offset or timedelta(0) + after_offset = after_offset or timedelta(0) + + sunrise = get_astral_event_date(hass, SUN_EVENT_SUNRISE, today) + sunset = get_astral_event_date(hass, SUN_EVENT_SUNSET, today) + + has_sunrise_condition = SUN_EVENT_SUNRISE in (before, after) + has_sunset_condition = SUN_EVENT_SUNSET in (before, after) + + after_sunrise = today > dt_util.as_local(cast(datetime, sunrise)).date() + if after_sunrise and has_sunrise_condition: + tomorrow = today + timedelta(days=1) + sunrise = get_astral_event_date(hass, SUN_EVENT_SUNRISE, tomorrow) + + after_sunset = today > dt_util.as_local(cast(datetime, sunset)).date() + if after_sunset and has_sunset_condition: + tomorrow = today + timedelta(days=1) + sunset = get_astral_event_date(hass, SUN_EVENT_SUNSET, tomorrow) + + # Special case: before sunrise OR after sunset + # This will handle the very rare case in the polar region when the sun rises/sets + # but does not set/rise. + # However this entire condition does not handle those full days of darkness + # or light, the following should be used instead: + # + # condition: + # condition: state + # entity_id: sun.sun + # state: 'above_horizon' (or 'below_horizon') + # + if before == SUN_EVENT_SUNRISE and after == SUN_EVENT_SUNSET: + wanted_time_before = cast(datetime, sunrise) + before_offset + condition_trace_update_result(wanted_time_before=wanted_time_before) + wanted_time_after = cast(datetime, sunset) + after_offset + condition_trace_update_result(wanted_time_after=wanted_time_after) + return utcnow < wanted_time_before or utcnow > wanted_time_after + + if sunrise is None and has_sunrise_condition: + # There is no sunrise today + condition_trace_set_result(False, message="no sunrise today") + return False + + if sunset is None and has_sunset_condition: + # There is no sunset today + condition_trace_set_result(False, message="no sunset today") + 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: + 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: + 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: + 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 + + +def async_condition_from_config(config: ConfigType) -> ConditionCheckerType: + """Wrap action method with sun based condition.""" + before = config.get("before") + after = config.get("after") + before_offset = config.get("before_offset") + after_offset = config.get("after_offset") + + @trace_condition_function + def sun_if(hass: HomeAssistant, variables: TemplateVarsType = None) -> bool: + """Validate time based if-condition.""" + return sun(hass, before, after, before_offset, after_offset) + + return sun_if diff --git a/homeassistant/helpers/condition.py b/homeassistant/helpers/condition.py index fa2dd42589b..c1b87dd755a 100644 --- a/homeassistant/helpers/condition.py +++ b/homeassistant/helpers/condition.py @@ -42,8 +42,6 @@ from homeassistant.const import ( ENTITY_MATCH_ANY, STATE_UNAVAILABLE, STATE_UNKNOWN, - SUN_EVENT_SUNRISE, - SUN_EVENT_SUNSET, WEEKDAYS, ) from homeassistant.core import HomeAssistant, State, callback @@ -60,7 +58,6 @@ from homeassistant.util import dt as dt_util from homeassistant.util.async_ import run_callback_threadsafe from . import config_validation as cv, entity_registry as er -from .sun import get_astral_event_date from .template import Template, render_complex from .trace import ( TraceElement, @@ -85,7 +82,6 @@ _PLATFORM_ALIASES = { "numeric_state": None, "or": None, "state": None, - "sun": None, "template": None, "time": None, "trigger": None, @@ -655,105 +651,6 @@ def state_from_config(config: ConfigType) -> ConditionCheckerType: return if_state -def sun( - hass: HomeAssistant, - before: str | None = None, - after: str | None = None, - before_offset: timedelta | None = None, - after_offset: timedelta | None = None, -) -> bool: - """Test if current time matches sun requirements.""" - utcnow = dt_util.utcnow() - today = dt_util.as_local(utcnow).date() - before_offset = before_offset or timedelta(0) - after_offset = after_offset or timedelta(0) - - sunrise = get_astral_event_date(hass, SUN_EVENT_SUNRISE, today) - sunset = get_astral_event_date(hass, SUN_EVENT_SUNSET, today) - - has_sunrise_condition = SUN_EVENT_SUNRISE in (before, after) - has_sunset_condition = SUN_EVENT_SUNSET in (before, after) - - after_sunrise = today > dt_util.as_local(cast(datetime, sunrise)).date() - if after_sunrise and has_sunrise_condition: - tomorrow = today + timedelta(days=1) - sunrise = get_astral_event_date(hass, SUN_EVENT_SUNRISE, tomorrow) - - after_sunset = today > dt_util.as_local(cast(datetime, sunset)).date() - if after_sunset and has_sunset_condition: - tomorrow = today + timedelta(days=1) - sunset = get_astral_event_date(hass, SUN_EVENT_SUNSET, tomorrow) - - # Special case: before sunrise OR after sunset - # This will handle the very rare case in the polar region when the sun rises/sets - # but does not set/rise. - # However this entire condition does not handle those full days of darkness - # or light, the following should be used instead: - # - # condition: - # condition: state - # entity_id: sun.sun - # state: 'above_horizon' (or 'below_horizon') - # - if before == SUN_EVENT_SUNRISE and after == SUN_EVENT_SUNSET: - wanted_time_before = cast(datetime, sunrise) + before_offset - condition_trace_update_result(wanted_time_before=wanted_time_before) - wanted_time_after = cast(datetime, sunset) + after_offset - condition_trace_update_result(wanted_time_after=wanted_time_after) - return utcnow < wanted_time_before or utcnow > wanted_time_after - - if sunrise is None and has_sunrise_condition: - # There is no sunrise today - condition_trace_set_result(False, message="no sunrise today") - return False - - if sunset is None and has_sunset_condition: - # There is no sunset today - condition_trace_set_result(False, message="no sunset today") - 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: - 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: - 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: - 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 - - -def sun_from_config(config: ConfigType) -> ConditionCheckerType: - """Wrap action method with sun based condition.""" - before = config.get("before") - after = config.get("after") - before_offset = config.get("before_offset") - after_offset = config.get("after_offset") - - @trace_condition_function - def sun_if(hass: HomeAssistant, variables: TemplateVarsType = None) -> bool: - """Validate time based if-condition.""" - return sun(hass, before, after, before_offset, after_offset) - - return sun_if - - def template( hass: HomeAssistant, value_template: Template, variables: TemplateVarsType = None ) -> bool: @@ -1054,8 +951,10 @@ async def async_validate_condition_config( return config platform = await _async_get_condition_platform(hass, config) - if platform is not None and hasattr(platform, "async_validate_condition_config"): - return await platform.async_validate_condition_config(hass, config) + if platform is not None: + if hasattr(platform, "async_validate_condition_config"): + return await platform.async_validate_condition_config(hass, config) + return cast(ConfigType, platform.CONDITION_SCHEMA(config)) if platform is None and condition in ("numeric_state", "state"): validator = cast( Callable[[HomeAssistant, ConfigType], ConfigType], diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index 4c760bd9d70..31a3e365071 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -1090,7 +1090,7 @@ type ValueSchemas = dict[Hashable, VolSchemaType | Callable[[Any], dict[str, Any def key_value_schemas( key: str, value_schemas: ValueSchemas, - default_schema: VolSchemaType | None = None, + default_schema: VolSchemaType | Callable[[Any], dict[str, Any]] | None = None, default_description: str | None = None, ) -> Callable[[Any], dict[Hashable, Any]]: """Create a validator that validates based on a value for specific key. @@ -1745,18 +1745,35 @@ BUILT_IN_CONDITIONS: ValueSchemas = { "numeric_state": NUMERIC_STATE_CONDITION_SCHEMA, "or": OR_CONDITION_SCHEMA, "state": STATE_CONDITION_SCHEMA, - "sun": SUN_CONDITION_SCHEMA, "template": TEMPLATE_CONDITION_SCHEMA, "time": TIME_CONDITION_SCHEMA, "trigger": TRIGGER_CONDITION_SCHEMA, "zone": ZONE_CONDITION_SCHEMA, } + +# This is first round of validation, we don't want to mutate the config here already, +# just ensure basics as condition type and alias are there. +def _base_condition_validator(value: Any) -> Any: + vol.Schema( + { + **CONDITION_BASE_SCHEMA, + CONF_CONDITION: vol.NotIn(BUILT_IN_CONDITIONS), + }, + extra=vol.ALLOW_EXTRA, + )(value) + return value + + CONDITION_SCHEMA: vol.Schema = vol.Schema( vol.Any( vol.All( expand_condition_shorthand, - key_value_schemas(CONF_CONDITION, BUILT_IN_CONDITIONS), + key_value_schemas( + CONF_CONDITION, + BUILT_IN_CONDITIONS, + _base_condition_validator, + ), ), dynamic_template_condition, ) @@ -1783,7 +1800,10 @@ CONDITION_ACTION_SCHEMA: vol.Schema = vol.Schema( key_value_schemas( CONF_CONDITION, BUILT_IN_CONDITIONS, - dynamic_template_condition_action, + vol.Any( + dynamic_template_condition_action, + _base_condition_validator, + ), "a list of conditions or a valid template", ), ) @@ -1842,7 +1862,7 @@ def _base_trigger_list_flatten(triggers: list[Any]) -> list[Any]: return flatlist -# This is first round of validation, we don't want to process the config here already, +# This is first round of validation, we don't want to mutate the config here already, # just ensure basics as platform and ID are there. def _base_trigger_validator(value: Any) -> Any: _base_trigger_validator_schema(value) diff --git a/tests/components/sun/test_condition.py b/tests/components/sun/test_condition.py new file mode 100644 index 00000000000..52c0d885461 --- /dev/null +++ b/tests/components/sun/test_condition.py @@ -0,0 +1,1235 @@ +"""The tests for sun conditions.""" + +from datetime import datetime + +from freezegun import freeze_time +import pytest + +from homeassistant.components import automation +from homeassistant.const import SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET +from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.helpers import trace +from homeassistant.setup import async_setup_component +from homeassistant.util import dt as dt_util + +from tests.typing import WebSocketGenerator + + +@pytest.fixture(autouse=True) +def prepare_condition_trace() -> None: + """Clear previous trace.""" + trace.trace_clear() + + +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.""" + msg_id = 1 + + def next_id(): + nonlocal msg_id + msg_id += 1 + return msg_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: HomeAssistant, + hass_ws_client: WebSocketGenerator, + service_calls: list[ServiceCall], +) -> None: + """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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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: HomeAssistant, + hass_ws_client: WebSocketGenerator, + service_calls: list[ServiceCall], +) -> None: + """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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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: HomeAssistant, + hass_ws_client: WebSocketGenerator, + service_calls: list[ServiceCall], +) -> None: + """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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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: HomeAssistant, + hass_ws_client: WebSocketGenerator, + service_calls: list[ServiceCall], +) -> None: + """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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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: HomeAssistant, + hass_ws_client: WebSocketGenerator, + service_calls: list[ServiceCall], +) -> None: + """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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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: HomeAssistant, + hass_ws_client: WebSocketGenerator, + service_calls: list[ServiceCall], +) -> None: + """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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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_after_and_before_during( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + service_calls: list[ServiceCall], +) -> None: + """Test if action was after sunrise and before sunset. + + 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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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_or_after_during( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + service_calls: list[ServiceCall], +) -> None: + """Test if action was before sunrise or after sunset. + + This is true from midnight until sunrise and from sunset until midnight + """ + 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, + "after": 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 -> 'before sunrise' | 'after sunset' true + now = datetime(2015, 9, 16, 13, 33, 17, tzinfo=dt_util.UTC) + with freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_calls) == 1 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + { + "result": True, + "wanted_time_after": "2015-09-17T01:53:44.723614+00:00", + "wanted_time_before": "2015-09-16T13:33:18.342542+00:00", + }, + ) + + # now = sunset + 1s -> 'before sunrise' | 'after sunset' true + now = datetime(2015, 9, 17, 1, 53, 46, tzinfo=dt_util.UTC) + with freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + { + "result": True, + "wanted_time_after": "2015-09-17T01:53:44.723614+00:00", + "wanted_time_before": "2015-09-16T13:33:18.342542+00:00", + }, + ) + + # now = sunrise + 1s -> 'before sunrise' | 'after sunset' false + now = datetime(2015, 9, 16, 13, 33, 19, tzinfo=dt_util.UTC) + with freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + { + "result": False, + "wanted_time_after": "2015-09-17T01:53:44.723614+00:00", + "wanted_time_before": "2015-09-16T13:33:18.342542+00:00", + }, + ) + + # now = sunset - 1s -> 'before sunrise' | 'after sunset' false + now = datetime(2015, 9, 17, 1, 53, 44, tzinfo=dt_util.UTC) + with freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + { + "result": False, + "wanted_time_after": "2015-09-17T01:53:44.723614+00:00", + "wanted_time_before": "2015-09-16T13:33:18.342542+00:00", + }, + ) + + # now = midnight + 1s local -> 'before sunrise' | 'after sunset' true + now = datetime(2015, 9, 16, 7, 0, 1, tzinfo=dt_util.UTC) + with freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_calls) == 3 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + { + "result": True, + "wanted_time_after": "2015-09-17T01:53:44.723614+00:00", + "wanted_time_before": "2015-09-16T13:33:18.342542+00:00", + }, + ) + + # now = midnight - 1s local -> 'before sunrise' | 'after sunset' true + now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC) + with freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_calls) == 4 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + { + "result": True, + "wanted_time_after": "2015-09-17T01:53:44.723614+00:00", + "wanted_time_before": "2015-09-16T13:33:18.342542+00:00", + }, + ) + + +async def test_if_action_before_sunrise_no_offset_kotzebue( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + service_calls: list[ServiceCall], +) -> None: + """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. + """ + await hass.config.async_set_time_zone("America/Anchorage") + 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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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: HomeAssistant, + hass_ws_client: WebSocketGenerator, + service_calls: list[ServiceCall], +) -> None: + """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. + """ + await hass.config.async_set_time_zone("America/Anchorage") + 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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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: HomeAssistant, + hass_ws_client: WebSocketGenerator, + service_calls: list[ServiceCall], +) -> None: + """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. + """ + await hass.config.async_set_time_zone("America/Anchorage") + 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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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: HomeAssistant, + hass_ws_client: WebSocketGenerator, + service_calls: list[ServiceCall], +) -> None: + """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. + """ + await hass.config.async_set_time_zone("America/Anchorage") + 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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_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 freeze_time(now): + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(service_calls) == 2 + await assert_automation_condition_trace( + hass_ws_client, + "sun", + {"result": True, "wanted_time_after": "2015-07-23T11:22:18.467277+00:00"}, + ) diff --git a/tests/components/websocket_api/test_commands.py b/tests/components/websocket_api/test_commands.py index 80e6b8be056..4ca2098550b 100644 --- a/tests/components/websocket_api/test_commands.py +++ b/tests/components/websocket_api/test_commands.py @@ -2529,9 +2529,8 @@ async def test_validate_config_works( "state": "paulus", }, ( - "Unexpected value for condition: 'non_existing'. Expected and, device," - " not, numeric_state, or, state, sun, template, time, trigger, zone " - "@ data[0]" + "Invalid condition \"non_existing\" specified {'condition': " + "'non_existing', 'entity_id': 'hello.world', 'state': 'paulus'}" ), ), # Raises HomeAssistantError diff --git a/tests/helpers/test_condition.py b/tests/helpers/test_condition.py index aac64f6139a..7285301f12b 100644 --- a/tests/helpers/test_condition.py +++ b/tests/helpers/test_condition.py @@ -1,6 +1,6 @@ """Test the condition helper.""" -from datetime import datetime, timedelta +from datetime import timedelta from typing import Any from unittest.mock import AsyncMock, patch @@ -8,7 +8,6 @@ from freezegun import freeze_time import pytest import voluptuous as vol -from homeassistant.components import automation from homeassistant.components.sensor import SensorDeviceClass from homeassistant.const import ( ATTR_DEVICE_CLASS, @@ -17,10 +16,8 @@ from homeassistant.const import ( CONF_DOMAIN, STATE_UNAVAILABLE, STATE_UNKNOWN, - SUN_EVENT_SUNRISE, - SUN_EVENT_SUNSET, ) -from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConditionError, HomeAssistantError from homeassistant.helpers import ( condition, @@ -32,8 +29,6 @@ from homeassistant.helpers.template import Template from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util -from tests.typing import WebSocketGenerator - def assert_element(trace_element, expected_element, path): """Assert a trace element is as expected. @@ -2242,1220 +2237,6 @@ async def test_condition_template_invalid_results(hass: HomeAssistant) -> None: 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.""" - msg_id = 1 - - def next_id(): - nonlocal msg_id - msg_id += 1 - return msg_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: HomeAssistant, - hass_ws_client: WebSocketGenerator, - service_calls: list[ServiceCall], -) -> None: - """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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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: HomeAssistant, - hass_ws_client: WebSocketGenerator, - service_calls: list[ServiceCall], -) -> None: - """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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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: HomeAssistant, - hass_ws_client: WebSocketGenerator, - service_calls: list[ServiceCall], -) -> None: - """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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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: HomeAssistant, - hass_ws_client: WebSocketGenerator, - service_calls: list[ServiceCall], -) -> None: - """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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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: HomeAssistant, - hass_ws_client: WebSocketGenerator, - service_calls: list[ServiceCall], -) -> None: - """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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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: HomeAssistant, - hass_ws_client: WebSocketGenerator, - service_calls: list[ServiceCall], -) -> None: - """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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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_after_and_before_during( - hass: HomeAssistant, - hass_ws_client: WebSocketGenerator, - service_calls: list[ServiceCall], -) -> None: - """Test if action was after sunrise and before sunset. - - 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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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_or_after_during( - hass: HomeAssistant, - hass_ws_client: WebSocketGenerator, - service_calls: list[ServiceCall], -) -> None: - """Test if action was before sunrise or after sunset. - - This is true from midnight until sunrise and from sunset until midnight - """ - 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, - "after": 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 -> 'before sunrise' | 'after sunset' true - now = datetime(2015, 9, 16, 13, 33, 17, tzinfo=dt_util.UTC) - with freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_calls) == 1 - await assert_automation_condition_trace( - hass_ws_client, - "sun", - { - "result": True, - "wanted_time_after": "2015-09-17T01:53:44.723614+00:00", - "wanted_time_before": "2015-09-16T13:33:18.342542+00:00", - }, - ) - - # now = sunset + 1s -> 'before sunrise' | 'after sunset' true - now = datetime(2015, 9, 17, 1, 53, 46, tzinfo=dt_util.UTC) - with freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_calls) == 2 - await assert_automation_condition_trace( - hass_ws_client, - "sun", - { - "result": True, - "wanted_time_after": "2015-09-17T01:53:44.723614+00:00", - "wanted_time_before": "2015-09-16T13:33:18.342542+00:00", - }, - ) - - # now = sunrise + 1s -> 'before sunrise' | 'after sunset' false - now = datetime(2015, 9, 16, 13, 33, 19, tzinfo=dt_util.UTC) - with freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_calls) == 2 - await assert_automation_condition_trace( - hass_ws_client, - "sun", - { - "result": False, - "wanted_time_after": "2015-09-17T01:53:44.723614+00:00", - "wanted_time_before": "2015-09-16T13:33:18.342542+00:00", - }, - ) - - # now = sunset - 1s -> 'before sunrise' | 'after sunset' false - now = datetime(2015, 9, 17, 1, 53, 44, tzinfo=dt_util.UTC) - with freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_calls) == 2 - await assert_automation_condition_trace( - hass_ws_client, - "sun", - { - "result": False, - "wanted_time_after": "2015-09-17T01:53:44.723614+00:00", - "wanted_time_before": "2015-09-16T13:33:18.342542+00:00", - }, - ) - - # now = midnight + 1s local -> 'before sunrise' | 'after sunset' true - now = datetime(2015, 9, 16, 7, 0, 1, tzinfo=dt_util.UTC) - with freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_calls) == 3 - await assert_automation_condition_trace( - hass_ws_client, - "sun", - { - "result": True, - "wanted_time_after": "2015-09-17T01:53:44.723614+00:00", - "wanted_time_before": "2015-09-16T13:33:18.342542+00:00", - }, - ) - - # now = midnight - 1s local -> 'before sunrise' | 'after sunset' true - now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC) - with freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_calls) == 4 - await assert_automation_condition_trace( - hass_ws_client, - "sun", - { - "result": True, - "wanted_time_after": "2015-09-17T01:53:44.723614+00:00", - "wanted_time_before": "2015-09-16T13:33:18.342542+00:00", - }, - ) - - -async def test_if_action_before_sunrise_no_offset_kotzebue( - hass: HomeAssistant, - hass_ws_client: WebSocketGenerator, - service_calls: list[ServiceCall], -) -> None: - """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. - """ - await hass.config.async_set_time_zone("America/Anchorage") - 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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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: HomeAssistant, - hass_ws_client: WebSocketGenerator, - service_calls: list[ServiceCall], -) -> None: - """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. - """ - await hass.config.async_set_time_zone("America/Anchorage") - 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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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: HomeAssistant, - hass_ws_client: WebSocketGenerator, - service_calls: list[ServiceCall], -) -> None: - """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. - """ - await hass.config.async_set_time_zone("America/Anchorage") - 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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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: HomeAssistant, - hass_ws_client: WebSocketGenerator, - service_calls: list[ServiceCall], -) -> None: - """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. - """ - await hass.config.async_set_time_zone("America/Anchorage") - 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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_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 freeze_time(now): - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(service_calls) == 2 - await assert_automation_condition_trace( - hass_ws_client, - "sun", - {"result": True, "wanted_time_after": "2015-07-23T11:22:18.467277+00:00"}, - ) - - async def test_trigger(hass: HomeAssistant) -> None: """Test trigger condition.""" config = {"alias": "Trigger Cond", "condition": "trigger", "id": "123456"} diff --git a/tests/helpers/test_config_validation.py b/tests/helpers/test_config_validation.py index ecf5271dafd..aec687be40a 100644 --- a/tests/helpers/test_config_validation.py +++ b/tests/helpers/test_config_validation.py @@ -1460,11 +1460,6 @@ def test_key_value_schemas_with_default() -> None: [ ({"delay": "{{ invalid"}, "should be format 'HH:MM'"), ({"wait_template": "{{ invalid"}, "invalid template"), - ({"condition": "invalid"}, "Unexpected value for condition: 'invalid'"), - ( - {"condition": "not", "conditions": {"condition": "invalid"}}, - "Unexpected value for condition: 'invalid'", - ), # The validation error message could be improved to explain that this is not # a valid shorthand template ( @@ -1496,7 +1491,7 @@ def test_key_value_schemas_with_default() -> None: ) @pytest.mark.usefixtures("hass") def test_script(caplog: pytest.LogCaptureFixture, config: dict, error: str) -> None: - """Test script validation is user friendly.""" + """Test script action validation is user friendly.""" with pytest.raises(vol.Invalid, match=error): cv.script_action(config)