diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 3bfdd57be6a9..c4695b81d688 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -135,6 +135,7 @@ _EXPERIMENTAL_CONDITION_PLATFORMS = { "motion", "occupancy", "person", + "schedule", "siren", "switch", "vacuum", diff --git a/homeassistant/components/schedule/condition.py b/homeassistant/components/schedule/condition.py new file mode 100644 index 000000000000..e04126c664bf --- /dev/null +++ b/homeassistant/components/schedule/condition.py @@ -0,0 +1,17 @@ +"""Provides conditions for schedules.""" + +from homeassistant.const import STATE_OFF, STATE_ON +from homeassistant.core import HomeAssistant +from homeassistant.helpers.condition import Condition, make_entity_state_condition + +from .const import DOMAIN + +CONDITIONS: dict[str, type[Condition]] = { + "is_off": make_entity_state_condition(DOMAIN, STATE_OFF), + "is_on": make_entity_state_condition(DOMAIN, STATE_ON), +} + + +async def async_get_conditions(hass: HomeAssistant) -> dict[str, type[Condition]]: + """Return the schedule conditions.""" + return CONDITIONS diff --git a/homeassistant/components/schedule/conditions.yaml b/homeassistant/components/schedule/conditions.yaml new file mode 100644 index 000000000000..d9d89d329323 --- /dev/null +++ b/homeassistant/components/schedule/conditions.yaml @@ -0,0 +1,17 @@ +.condition_common: &condition_common + target: + entity: + domain: schedule + fields: + behavior: + required: true + default: any + selector: + select: + translation_key: condition_behavior + options: + - all + - any + +is_off: *condition_common +is_on: *condition_common diff --git a/homeassistant/components/schedule/icons.json b/homeassistant/components/schedule/icons.json index bf325ae5f88f..3acb44479de6 100644 --- a/homeassistant/components/schedule/icons.json +++ b/homeassistant/components/schedule/icons.json @@ -1,4 +1,12 @@ { + "conditions": { + "is_off": { + "condition": "mdi:calendar-blank" + }, + "is_on": { + "condition": "mdi:calendar-clock" + } + }, "services": { "get_schedule": { "service": "mdi:calendar-export" diff --git a/homeassistant/components/schedule/strings.json b/homeassistant/components/schedule/strings.json index 2d66ce96fbaf..f416cc149c1c 100644 --- a/homeassistant/components/schedule/strings.json +++ b/homeassistant/components/schedule/strings.json @@ -1,8 +1,32 @@ { "common": { + "condition_behavior_description": "How the state should match on the targeted schedules.", + "condition_behavior_name": "Behavior", "trigger_behavior_description": "The behavior of the targeted schedules to trigger on.", "trigger_behavior_name": "Behavior" }, + "conditions": { + "is_off": { + "description": "Tests if one or more schedule blocks are currently not active.", + "fields": { + "behavior": { + "description": "[%key:component::schedule::common::condition_behavior_description%]", + "name": "[%key:component::schedule::common::condition_behavior_name%]" + } + }, + "name": "Schedule is off" + }, + "is_on": { + "description": "Tests if one or more schedule blocks are currently active.", + "fields": { + "behavior": { + "description": "[%key:component::schedule::common::condition_behavior_description%]", + "name": "[%key:component::schedule::common::condition_behavior_name%]" + } + }, + "name": "Schedule is on" + } + }, "entity_component": { "_": { "name": "[%key:component::schedule::title%]", @@ -25,6 +49,12 @@ } }, "selector": { + "condition_behavior": { + "options": { + "all": "All", + "any": "Any" + } + }, "trigger_behavior": { "options": { "any": "Any", diff --git a/tests/components/schedule/test_condition.py b/tests/components/schedule/test_condition.py new file mode 100644 index 000000000000..e9eb1fcf61cc --- /dev/null +++ b/tests/components/schedule/test_condition.py @@ -0,0 +1,126 @@ +"""Test schedule conditions.""" + +from typing import Any + +import pytest + +from homeassistant.components.schedule.const import DOMAIN +from homeassistant.const import STATE_OFF, STATE_ON +from homeassistant.core import HomeAssistant + +from tests.components.common import ( + ConditionStateDescription, + assert_condition_behavior_all, + assert_condition_behavior_any, + assert_condition_gated_by_labs_flag, + parametrize_condition_states_all, + parametrize_condition_states_any, + parametrize_target_entities, + target_entities, +) + + +@pytest.fixture +async def target_schedules(hass: HomeAssistant) -> dict[str, list[str]]: + """Create multiple schedule entities associated with different targets.""" + return await target_entities(hass, DOMAIN) + + +@pytest.mark.parametrize( + "condition", + [ + "schedule.is_off", + "schedule.is_on", + ], +) +async def test_schedule_conditions_gated_by_labs_flag( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture, condition: str +) -> None: + """Test the schedule conditions are gated by the labs flag.""" + await assert_condition_gated_by_labs_flag(hass, caplog, condition) + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ("condition_target_config", "entity_id", "entities_in_target"), + parametrize_target_entities(DOMAIN), +) +@pytest.mark.parametrize( + ("condition", "condition_options", "states"), + [ + *parametrize_condition_states_any( + condition="schedule.is_on", + target_states=[STATE_ON], + other_states=[STATE_OFF], + ), + *parametrize_condition_states_any( + condition="schedule.is_off", + target_states=[STATE_OFF], + other_states=[STATE_ON], + ), + ], +) +async def test_schedule_state_condition_behavior_any( + hass: HomeAssistant, + target_schedules: dict[str, list[str]], + condition_target_config: dict, + entity_id: str, + entities_in_target: int, + condition: str, + condition_options: dict[str, Any], + states: list[ConditionStateDescription], +) -> None: + """Test the schedule state condition with the 'any' behavior.""" + await assert_condition_behavior_any( + hass, + target_entities=target_schedules, + condition_target_config=condition_target_config, + entity_id=entity_id, + entities_in_target=entities_in_target, + condition=condition, + condition_options=condition_options, + states=states, + ) + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ("condition_target_config", "entity_id", "entities_in_target"), + parametrize_target_entities(DOMAIN), +) +@pytest.mark.parametrize( + ("condition", "condition_options", "states"), + [ + *parametrize_condition_states_all( + condition="schedule.is_on", + target_states=[STATE_ON], + other_states=[STATE_OFF], + ), + *parametrize_condition_states_all( + condition="schedule.is_off", + target_states=[STATE_OFF], + other_states=[STATE_ON], + ), + ], +) +async def test_schedule_state_condition_behavior_all( + hass: HomeAssistant, + target_schedules: dict[str, list[str]], + condition_target_config: dict, + entity_id: str, + entities_in_target: int, + condition: str, + condition_options: dict[str, Any], + states: list[ConditionStateDescription], +) -> None: + """Test the schedule state condition with the 'all' behavior.""" + await assert_condition_behavior_all( + hass, + target_entities=target_schedules, + condition_target_config=condition_target_config, + entity_id=entity_id, + entities_in_target=entities_in_target, + condition=condition, + condition_options=condition_options, + states=states, + )