diff --git a/homeassistant/components/calendar/strings.json b/homeassistant/components/calendar/strings.json index 061c0755268e..2edafb9c06f9 100644 --- a/homeassistant/components/calendar/strings.json +++ b/homeassistant/components/calendar/strings.json @@ -63,14 +63,6 @@ "title": "Detected use of deprecated action calendar.list_events" } }, - "selector": { - "trigger_offset_type": { - "options": { - "after": "After", - "before": "Before" - } - } - }, "services": { "create_event": { "description": "Adds a new calendar event.", @@ -135,12 +127,8 @@ "description": "Triggers when one or more calendar events end.", "fields": { "offset": { - "description": "Offset from the end of the event.", + "description": "Time to trigger before or after the end of the event.", "name": "Offset" - }, - "offset_type": { - "description": "Whether to trigger before or after the end of the event, if an offset is defined.", - "name": "Offset type" } }, "name": "Calendar event ended" @@ -149,12 +137,8 @@ "description": "Triggers when one or more calendar events start.", "fields": { "offset": { - "description": "Offset from the start of the event.", + "description": "Time to trigger before or after the start of the event.", "name": "Offset" - }, - "offset_type": { - "description": "Whether to trigger before or after the start of the event, if an offset is defined.", - "name": "Offset type" } }, "name": "Calendar event started" diff --git a/homeassistant/components/calendar/trigger.py b/homeassistant/components/calendar/trigger.py index a7b1535bb1d3..162b63aee489 100644 --- a/homeassistant/components/calendar/trigger.py +++ b/homeassistant/components/calendar/trigger.py @@ -64,14 +64,26 @@ _SINGLE_ENTITY_EVENT_TRIGGER_SCHEMA = probatio.Schema( }, ) + +def _apply_offset_type(options: dict[str, Any]) -> dict[str, Any]: + if options.pop(CONF_OFFSET_TYPE, None) == OFFSET_TYPE_BEFORE: + options[CONF_OFFSET] = -options[CONF_OFFSET] + return options + + _EVENT_TRIGGER_SCHEMA = probatio.Schema( { - probatio.Required(CONF_OPTIONS, default={}): { - probatio.Required(CONF_OFFSET, default=dt.timedelta(0)): cv.time_period, - probatio.Required( - CONF_OFFSET_TYPE, default=OFFSET_TYPE_BEFORE - ): probatio.In({OFFSET_TYPE_BEFORE, OFFSET_TYPE_AFTER}), - }, + probatio.Required(CONF_OPTIONS, default={}): probatio.All( + { + probatio.Required(CONF_OFFSET, default=dt.timedelta(0)): cv.time_period, + # Older configs set a positive offset with an offset type, + # which is folded into the sign of the offset. + probatio.Optional(CONF_OFFSET_TYPE): probatio.In( + {OFFSET_TYPE_BEFORE, OFFSET_TYPE_AFTER} + ), + }, + _apply_offset_type, + ), probatio.Required(CONF_TARGET): cv.TARGET_FIELDS, } ) @@ -466,11 +478,6 @@ class EventTrigger(Trigger): """Attach a trigger.""" offset = self._options[CONF_OFFSET] - offset_type = self._options[CONF_OFFSET_TYPE] - - if offset_type == OFFSET_TYPE_BEFORE: - offset = -offset - target_selection = TargetSelection(self._target) if not target_selection.has_any_target: raise HomeAssistantError(f"No target defined in {self._target}") diff --git a/homeassistant/components/calendar/triggers.yaml b/homeassistant/components/calendar/triggers.yaml index 37599b4515a7..5b4cf5a4defb 100644 --- a/homeassistant/components/calendar/triggers.yaml +++ b/homeassistant/components/calendar/triggers.yaml @@ -13,15 +13,7 @@ selector: duration: enable_day: true - offset_type: - required: true - default: before - selector: - select: - translation_key: trigger_offset_type - options: - - before - - after + mode: offset event_started: *trigger_common event_ended: *trigger_common diff --git a/homeassistant/components/sun/strings.json b/homeassistant/components/sun/strings.json index 66ffee5551cb..f0157c844612 100644 --- a/homeassistant/components/sun/strings.json +++ b/homeassistant/components/sun/strings.json @@ -4,10 +4,8 @@ "period_description": "The time of day to consider: the morning, the evening, or either.", "period_name": "Period", "trigger_for_name": "For at least", - "trigger_offset_description": "Time to offset the trigger from the solar event.", + "trigger_offset_description": "Time to trigger before or after the solar event.", "trigger_offset_name": "Offset", - "trigger_offset_type_description": "Whether to trigger before or after the solar event.", - "trigger_offset_type_name": "Offset type", "trigger_threshold_name": "Threshold type", "twilight_type_description": "The phase of twilight.", "twilight_type_name": "Twilight type" @@ -136,12 +134,6 @@ "morning": "Morning" } }, - "trigger_offset_type": { - "options": { - "after": "After", - "before": "Before" - } - }, "twilight_type": { "options": { "any": "Any", @@ -160,10 +152,6 @@ "description": "[%key:component::sun::common::trigger_offset_description%]", "name": "[%key:component::sun::common::trigger_offset_name%]" }, - "offset_type": { - "description": "[%key:component::sun::common::trigger_offset_type_description%]", - "name": "[%key:component::sun::common::trigger_offset_type_name%]" - }, "period": { "description": "[%key:component::sun::common::period_description%]", "name": "[%key:component::sun::common::period_name%]" @@ -178,10 +166,6 @@ "description": "[%key:component::sun::common::trigger_offset_description%]", "name": "[%key:component::sun::common::trigger_offset_name%]" }, - "offset_type": { - "description": "[%key:component::sun::common::trigger_offset_type_description%]", - "name": "[%key:component::sun::common::trigger_offset_type_name%]" - }, "period": { "description": "[%key:component::sun::common::period_description%]", "name": "[%key:component::sun::common::period_name%]" @@ -196,10 +180,6 @@ "description": "[%key:component::sun::common::trigger_offset_description%]", "name": "[%key:component::sun::common::trigger_offset_name%]" }, - "offset_type": { - "description": "[%key:component::sun::common::trigger_offset_type_description%]", - "name": "[%key:component::sun::common::trigger_offset_type_name%]" - }, "type": { "description": "[%key:component::sun::common::twilight_type_description%]", "name": "[%key:component::sun::common::twilight_type_name%]" @@ -214,10 +194,6 @@ "description": "[%key:component::sun::common::trigger_offset_description%]", "name": "[%key:component::sun::common::trigger_offset_name%]" }, - "offset_type": { - "description": "[%key:component::sun::common::trigger_offset_type_description%]", - "name": "[%key:component::sun::common::trigger_offset_type_name%]" - }, "type": { "description": "[%key:component::sun::common::twilight_type_description%]", "name": "[%key:component::sun::common::twilight_type_name%]" @@ -253,10 +229,6 @@ "description": "[%key:component::sun::common::trigger_offset_description%]", "name": "[%key:component::sun::common::trigger_offset_name%]" }, - "offset_type": { - "description": "[%key:component::sun::common::trigger_offset_type_description%]", - "name": "[%key:component::sun::common::trigger_offset_type_name%]" - }, "period": { "description": "[%key:component::sun::common::period_description%]", "name": "[%key:component::sun::common::period_name%]" @@ -271,10 +243,6 @@ "description": "[%key:component::sun::common::trigger_offset_description%]", "name": "[%key:component::sun::common::trigger_offset_name%]" }, - "offset_type": { - "description": "[%key:component::sun::common::trigger_offset_type_description%]", - "name": "[%key:component::sun::common::trigger_offset_type_name%]" - }, "period": { "description": "[%key:component::sun::common::period_description%]", "name": "[%key:component::sun::common::period_name%]" @@ -288,10 +256,6 @@ "offset": { "description": "[%key:component::sun::common::trigger_offset_description%]", "name": "[%key:component::sun::common::trigger_offset_name%]" - }, - "offset_type": { - "description": "[%key:component::sun::common::trigger_offset_type_description%]", - "name": "[%key:component::sun::common::trigger_offset_type_name%]" } }, "name": "Midnight sun ended" @@ -302,10 +266,6 @@ "offset": { "description": "[%key:component::sun::common::trigger_offset_description%]", "name": "[%key:component::sun::common::trigger_offset_name%]" - }, - "offset_type": { - "description": "[%key:component::sun::common::trigger_offset_type_description%]", - "name": "[%key:component::sun::common::trigger_offset_type_name%]" } }, "name": "Midnight sun started" @@ -316,10 +276,6 @@ "offset": { "description": "[%key:component::sun::common::trigger_offset_description%]", "name": "[%key:component::sun::common::trigger_offset_name%]" - }, - "offset_type": { - "description": "[%key:component::sun::common::trigger_offset_type_description%]", - "name": "[%key:component::sun::common::trigger_offset_type_name%]" } }, "name": "Polar night ended" @@ -330,10 +286,6 @@ "offset": { "description": "[%key:component::sun::common::trigger_offset_description%]", "name": "[%key:component::sun::common::trigger_offset_name%]" - }, - "offset_type": { - "description": "[%key:component::sun::common::trigger_offset_type_description%]", - "name": "[%key:component::sun::common::trigger_offset_type_name%]" } }, "name": "Polar night started" @@ -344,10 +296,6 @@ "offset": { "description": "[%key:component::sun::common::trigger_offset_description%]", "name": "[%key:component::sun::common::trigger_offset_name%]" - }, - "offset_type": { - "description": "[%key:component::sun::common::trigger_offset_type_description%]", - "name": "[%key:component::sun::common::trigger_offset_type_name%]" } }, "name": "Solar midnight" @@ -358,10 +306,6 @@ "offset": { "description": "[%key:component::sun::common::trigger_offset_description%]", "name": "[%key:component::sun::common::trigger_offset_name%]" - }, - "offset_type": { - "description": "[%key:component::sun::common::trigger_offset_type_description%]", - "name": "[%key:component::sun::common::trigger_offset_type_name%]" } }, "name": "Solar noon" @@ -372,10 +316,6 @@ "offset": { "description": "[%key:component::sun::common::trigger_offset_description%]", "name": "[%key:component::sun::common::trigger_offset_name%]" - }, - "offset_type": { - "description": "[%key:component::sun::common::trigger_offset_type_description%]", - "name": "[%key:component::sun::common::trigger_offset_type_name%]" } }, "name": "Sunrise" @@ -386,10 +326,6 @@ "offset": { "description": "[%key:component::sun::common::trigger_offset_description%]", "name": "[%key:component::sun::common::trigger_offset_name%]" - }, - "offset_type": { - "description": "[%key:component::sun::common::trigger_offset_type_description%]", - "name": "[%key:component::sun::common::trigger_offset_type_name%]" } }, "name": "Sunset" diff --git a/homeassistant/components/sun/trigger.py b/homeassistant/components/sun/trigger.py index e31ad18449b1..4c4447c72bcb 100644 --- a/homeassistant/components/sun/trigger.py +++ b/homeassistant/components/sun/trigger.py @@ -80,15 +80,23 @@ CONF_OFFSET_TYPE = "offset_type" OFFSET_TYPE_BEFORE = "before" OFFSET_TYPE_AFTER = "after" -# Offset options shared by the solar event triggers. A positive offset combined -# with an offset type of "before" fires earlier than the event; "after" later. +# Offset options shared by the solar event triggers. A negative offset fires +# earlier than the event, a positive one later. Older configs set a positive +# offset with an offset type, which is folded into the sign. _OFFSET_OPTIONS: dict[probatio.Marker, Any] = { probatio.Required(CONF_OFFSET, default=timedelta(0)): cv.time_period, - probatio.Required(CONF_OFFSET_TYPE, default=OFFSET_TYPE_BEFORE): probatio.In( + probatio.Optional(CONF_OFFSET_TYPE): probatio.In( {OFFSET_TYPE_BEFORE, OFFSET_TYPE_AFTER} ), } + +def _apply_offset_type(options: dict[str, Any]) -> dict[str, Any]: + if options.pop(CONF_OFFSET_TYPE, None) == OFFSET_TYPE_BEFORE: + options[CONF_OFFSET] = -options[CONF_OFFSET] + return options + + # Sun elevation at each twilight boundary. _TWILIGHT_ELEVATIONS = { _TWILIGHT_CIVIL: ELEVATION_CIVIL, @@ -160,7 +168,11 @@ class SunElevationCrossedTrigger( _EVENT_TRIGGER_SCHEMA = probatio.Schema( - {probatio.Required(CONF_OPTIONS, default=dict): {**_OFFSET_OPTIONS}} + { + probatio.Required(CONF_OPTIONS, default=dict): probatio.All( + {**_OFFSET_OPTIONS}, _apply_offset_type + ) + } ) @@ -188,10 +200,7 @@ class SunEventTrigger(Trigger): """Initialize the trigger.""" super().__init__(hass, config) self._options = config.options or {} - offset = self._options.get(CONF_OFFSET) or timedelta(0) - if self._options.get(CONF_OFFSET_TYPE) == OFFSET_TYPE_BEFORE: - offset = -offset - self._offset = offset + self._offset: timedelta = self._options.get(CONF_OFFSET) or timedelta(0) def _get_next_event(self, utc_point_in_time: datetime) -> datetime | None: """Return the next time this solar event occurs. @@ -280,12 +289,15 @@ class SolarMidnightTrigger(SunEventTrigger): _DAWN_DUSK_TRIGGER_SCHEMA = probatio.Schema( { - probatio.Required(CONF_OPTIONS, default=dict): { - probatio.Optional(CONF_TYPE, default=_TWILIGHT_CIVIL): probatio.In( - _TWILIGHT_ELEVATIONS - ), - **_OFFSET_OPTIONS, - } + probatio.Required(CONF_OPTIONS, default=dict): probatio.All( + { + probatio.Optional(CONF_TYPE, default=_TWILIGHT_CIVIL): probatio.In( + _TWILIGHT_ELEVATIONS + ), + **_OFFSET_OPTIONS, + }, + _apply_offset_type, + ) } ) @@ -332,10 +344,15 @@ class DuskTrigger(SunDawnDuskTrigger): _GOLDEN_BLUE_HOUR_TRIGGER_SCHEMA = probatio.Schema( { - probatio.Required(CONF_OPTIONS, default=dict): { - probatio.Optional(CONF_PERIOD, default=_PERIOD_ANY): probatio.In(_PERIODS), - **_OFFSET_OPTIONS, - } + probatio.Required(CONF_OPTIONS, default=dict): probatio.All( + { + probatio.Optional(CONF_PERIOD, default=_PERIOD_ANY): probatio.In( + _PERIODS + ), + **_OFFSET_OPTIONS, + }, + _apply_offset_type, + ) } ) diff --git a/homeassistant/components/sun/triggers.yaml b/homeassistant/components/sun/triggers.yaml index c85095f0e530..881d529efd00 100644 --- a/homeassistant/components/sun/triggers.yaml +++ b/homeassistant/components/sun/triggers.yaml @@ -27,25 +27,16 @@ duration: .offset: &trigger_offset - offset: - required: true - default: - days: 0 - hours: 0 - minutes: 0 - seconds: 0 - selector: - duration: - enable_day: true - offset_type: - required: true - default: before - selector: - select: - translation_key: trigger_offset_type - options: - - before - - after + required: true + default: + days: 0 + hours: 0 + minutes: 0 + seconds: 0 + selector: + duration: + enable_day: true + mode: offset .elevation_threshold_entity: &trigger_elevation_threshold_entity - domain: input_number @@ -64,65 +55,65 @@ sunrise: fields: - <<: *trigger_offset + offset: *trigger_offset sunset: fields: - <<: *trigger_offset + offset: *trigger_offset solar_noon: fields: - <<: *trigger_offset + offset: *trigger_offset solar_midnight: fields: - <<: *trigger_offset + offset: *trigger_offset dawn: fields: type: *twilight_type - <<: *trigger_offset + offset: *trigger_offset dusk: fields: type: *twilight_type - <<: *trigger_offset + offset: *trigger_offset golden_hour_started: fields: period: *period_type - <<: *trigger_offset + offset: *trigger_offset golden_hour_ended: fields: period: *period_type - <<: *trigger_offset + offset: *trigger_offset blue_hour_started: fields: period: *period_type - <<: *trigger_offset + offset: *trigger_offset blue_hour_ended: fields: period: *period_type - <<: *trigger_offset + offset: *trigger_offset midnight_sun_started: fields: - <<: *trigger_offset + offset: *trigger_offset midnight_sun_ended: fields: - <<: *trigger_offset + offset: *trigger_offset polar_night_started: fields: - <<: *trigger_offset + offset: *trigger_offset polar_night_ended: fields: - <<: *trigger_offset + offset: *trigger_offset elevation_changed: fields: diff --git a/tests/components/calendar/test_trigger.py b/tests/components/calendar/test_trigger.py index 3aa097112704..39b899b1abcd 100644 --- a/tests/components/calendar/test_trigger.py +++ b/tests/components/calendar/test_trigger.py @@ -157,20 +157,35 @@ class TargetTriggerFormat(TriggerFormat): CONF_TARGET: {"entity_id": entity_id}, } if offset: - options: dict[str, Any] = {} - # Convert signed offset to offset + offset_type - if offset < dt.timedelta(0): - options[CONF_OFFSET] = -offset - options[CONF_OFFSET_TYPE] = OFFSET_TYPE_BEFORE - else: - options[CONF_OFFSET] = offset - options[CONF_OFFSET_TYPE] = OFFSET_TYPE_AFTER - trigger_data[CONF_OPTIONS] = options + trigger_data[CONF_OPTIONS] = {CONF_OFFSET: offset} + return trigger_data + + +@dataclass +class TargetOffsetTypeTriggerFormat(TargetTriggerFormat): + """Target trigger format with a positive offset and an offset type.""" + + id: str = "target_offset_type" + + def get_trigger_data( + self, entity_id: str, event_type: str, offset: dt.timedelta | None = None + ) -> dict[str, Any]: + """Get the trigger configuration data.""" + trigger_data = super().get_trigger_data(entity_id, event_type) + if offset: + trigger_data[CONF_OPTIONS] = { + CONF_OFFSET: abs(offset), + CONF_OFFSET_TYPE: OFFSET_TYPE_BEFORE + if offset < dt.timedelta(0) + else OFFSET_TYPE_AFTER, + } return trigger_data TRIGGER_FORMATS = [LegacyTriggerFormat(), TargetTriggerFormat()] TRIGGER_FORMAT_IDS = [fmt.id for fmt in TRIGGER_FORMATS] +OFFSET_TRIGGER_FORMATS = [*TRIGGER_FORMATS, TargetOffsetTypeTriggerFormat()] +OFFSET_TRIGGER_FORMAT_IDS = [fmt.id for fmt in OFFSET_TRIGGER_FORMATS] @pytest.fixture(params=TRIGGER_FORMATS, ids=TRIGGER_FORMAT_IDS) @@ -444,6 +459,9 @@ async def test_event_start_trigger( ] +@pytest.mark.parametrize( + "trigger_format", OFFSET_TRIGGER_FORMATS, ids=OFFSET_TRIGGER_FORMAT_IDS +) @pytest.mark.parametrize( ("offset_delta"), [ @@ -514,6 +532,9 @@ async def test_event_end_trigger( ] +@pytest.mark.parametrize( + "trigger_format", OFFSET_TRIGGER_FORMATS, ids=OFFSET_TRIGGER_FORMAT_IDS +) @pytest.mark.parametrize( ("offset_delta"), [ diff --git a/tests/components/sun/test_trigger.py b/tests/components/sun/test_trigger.py index ba89a0186d79..ea5c5151bce1 100644 --- a/tests/components/sun/test_trigger.py +++ b/tests/components/sun/test_trigger.py @@ -340,6 +340,18 @@ async def test_dawn_defaults_to_civil( assert service_calls[0].data["type"] == "civil" +_OFFSET_CASES = [ + pytest.param({"offset": {"hours": -1}}, -1, id="before"), + pytest.param({"offset": {"hours": 1}}, 1, id="after"), + pytest.param( + {"offset": {"hours": 1}, "offset_type": "before"}, -1, id="legacy_before" + ), + pytest.param( + {"offset": {"hours": 1}, "offset_type": "after"}, 1, id="legacy_after" + ), +] + + @pytest.mark.parametrize( ("trigger_key", "astral_event"), [ @@ -349,17 +361,13 @@ async def test_dawn_defaults_to_civil( ("sun.solar_midnight", "midnight"), ], ) -@pytest.mark.parametrize( - ("offset_type", "sign"), - [("before", -1), ("after", 1)], - ids=["before", "after"], -) +@pytest.mark.parametrize(("offset_options", "sign"), _OFFSET_CASES) async def test_event_trigger_offset( hass: HomeAssistant, service_calls: list[ServiceCall], trigger_key: str, astral_event: str, - offset_type: str, + offset_options: dict[str, Any], sign: int, ) -> None: """Test the solar event triggers apply a before/after time offset.""" @@ -369,7 +377,7 @@ async def test_event_trigger_offset( hass, { "platform": trigger_key, - "options": {"offset": {"hours": 1}, "offset_type": offset_type}, + "options": offset_options, }, {}, ) @@ -386,16 +394,12 @@ async def test_event_trigger_offset( @pytest.mark.parametrize("trigger_key", ["sun.dawn", "sun.dusk"]) -@pytest.mark.parametrize( - ("offset_type", "sign"), - [("before", -1), ("after", 1)], - ids=["before", "after"], -) +@pytest.mark.parametrize(("offset_options", "sign"), _OFFSET_CASES) async def test_dawn_dusk_trigger_offset( hass: HomeAssistant, service_calls: list[ServiceCall], trigger_key: str, - offset_type: str, + offset_options: dict[str, Any], sign: int, ) -> None: """Test the dawn and dusk triggers apply a before/after time offset.""" @@ -406,7 +410,7 @@ async def test_dawn_dusk_trigger_offset( hass, { "platform": trigger_key, - "options": {"offset": {"hours": 1}, "offset_type": offset_type}, + "options": offset_options, }, {}, ) @@ -730,11 +734,7 @@ async def test_golden_hour_defaults_to_any( ("sun.golden_hour_ended", "evening", "dusk", 4), ], ) -@pytest.mark.parametrize( - ("offset_type", "sign"), - [("before", -1), ("after", 1)], - ids=["before", "after"], -) +@pytest.mark.parametrize(("offset_options", "sign"), _OFFSET_CASES) async def test_golden_blue_hour_trigger_offset( hass: HomeAssistant, service_calls: list[ServiceCall], @@ -742,7 +742,7 @@ async def test_golden_blue_hour_trigger_offset( period: str, event: str, depression: int, - offset_type: str, + offset_options: dict[str, Any], sign: int, ) -> None: """Test the golden/blue hour triggers apply a before/after time offset.""" @@ -752,11 +752,7 @@ async def test_golden_blue_hour_trigger_offset( hass, { "platform": trigger_key, - "options": { - "period": period, - "offset": {"hours": 1}, - "offset_type": offset_type, - }, + "options": {"period": period, **offset_options}, }, {}, ) @@ -865,7 +861,7 @@ async def test_midnight_sun_trigger_offset_catches_pending_crossing( hass, { "platform": "sun.midnight_sun_started", - "options": {"offset": {"days": 3}, "offset_type": "after"}, + "options": {"offset": {"days": 3}}, }, {}, )