Add schedule conditions (#165913)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Erik Montnemery
2026-03-18 18:55:47 +01:00
committed by GitHub
co-authored by Martin Hjelmare
parent ba0804fefa
commit 04d45c8ada
6 changed files with 199 additions and 0 deletions
@@ -135,6 +135,7 @@ _EXPERIMENTAL_CONDITION_PLATFORMS = {
"motion",
"occupancy",
"person",
"schedule",
"siren",
"switch",
"vacuum",
@@ -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
@@ -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
@@ -1,4 +1,12 @@
{
"conditions": {
"is_off": {
"condition": "mdi:calendar-blank"
},
"is_on": {
"condition": "mdi:calendar-clock"
}
},
"services": {
"get_schedule": {
"service": "mdi:calendar-export"
@@ -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",
+126
View File
@@ -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,
)