From 951775bea69d67c3a4a2d4679501f0d66b0a7245 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 12 Mar 2026 10:18:42 +0100 Subject: [PATCH] Add window triggers (#165230) --- CODEOWNERS | 2 + homeassistant/bootstrap.py | 1 + .../components/automation/__init__.py | 1 + homeassistant/components/window/__init__.py | 17 + homeassistant/components/window/icons.json | 10 + homeassistant/components/window/manifest.json | 8 + homeassistant/components/window/strings.json | 38 ++ homeassistant/components/window/trigger.py | 36 + homeassistant/components/window/triggers.yaml | 29 + script/hassfest/manifest.py | 1 + script/hassfest/quality_scale.py | 1 + tests/components/window/__init__.py | 1 + tests/components/window/test_trigger.py | 646 ++++++++++++++++++ tests/snapshots/test_bootstrap.ambr | 2 + 14 files changed, 793 insertions(+) create mode 100644 homeassistant/components/window/__init__.py create mode 100644 homeassistant/components/window/icons.json create mode 100644 homeassistant/components/window/manifest.json create mode 100644 homeassistant/components/window/strings.json create mode 100644 homeassistant/components/window/trigger.py create mode 100644 homeassistant/components/window/triggers.yaml create mode 100644 tests/components/window/__init__.py create mode 100644 tests/components/window/test_trigger.py diff --git a/CODEOWNERS b/CODEOWNERS index 939d0adbc3c..7fe7458bea6 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -1905,6 +1905,8 @@ build.json @home-assistant/supervisor /tests/components/wiffi/ @mampfes /homeassistant/components/wilight/ @leofig-rj /tests/components/wilight/ @leofig-rj +/homeassistant/components/window/ @home-assistant/core +/tests/components/window/ @home-assistant/core /homeassistant/components/wirelesstag/ @sergeymaysak /homeassistant/components/withings/ @joostlek /tests/components/withings/ @joostlek diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index fede20375c0..0efef6cf9ab 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -245,6 +245,7 @@ DEFAULT_INTEGRATIONS = { "garage_door", "gate", "humidity", + "window", } DEFAULT_INTEGRATIONS_RECOVERY_MODE = { # These integrations are set up if recovery mode is activated. diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 9792b2e41db..39020a80d45 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -161,6 +161,7 @@ _EXPERIMENTAL_TRIGGER_PLATFORMS = { "text", "update", "vacuum", + "window", } diff --git a/homeassistant/components/window/__init__.py b/homeassistant/components/window/__init__.py new file mode 100644 index 00000000000..b4577fd370e --- /dev/null +++ b/homeassistant/components/window/__init__.py @@ -0,0 +1,17 @@ +"""Integration for window triggers.""" + +from __future__ import annotations + +from homeassistant.core import HomeAssistant +from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.typing import ConfigType + +DOMAIN = "window" +CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) + +__all__ = [] + + +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: + """Set up the component.""" + return True diff --git a/homeassistant/components/window/icons.json b/homeassistant/components/window/icons.json new file mode 100644 index 00000000000..0b3235bc138 --- /dev/null +++ b/homeassistant/components/window/icons.json @@ -0,0 +1,10 @@ +{ + "triggers": { + "closed": { + "trigger": "mdi:window-closed" + }, + "opened": { + "trigger": "mdi:window-open" + } + } +} diff --git a/homeassistant/components/window/manifest.json b/homeassistant/components/window/manifest.json new file mode 100644 index 00000000000..f378cffc0c9 --- /dev/null +++ b/homeassistant/components/window/manifest.json @@ -0,0 +1,8 @@ +{ + "domain": "window", + "name": "Window", + "codeowners": ["@home-assistant/core"], + "documentation": "https://www.home-assistant.io/integrations/window", + "integration_type": "system", + "quality_scale": "internal" +} diff --git a/homeassistant/components/window/strings.json b/homeassistant/components/window/strings.json new file mode 100644 index 00000000000..14adf2062ca --- /dev/null +++ b/homeassistant/components/window/strings.json @@ -0,0 +1,38 @@ +{ + "common": { + "trigger_behavior_description": "The behavior of the targeted windows to trigger on.", + "trigger_behavior_name": "Behavior" + }, + "selector": { + "trigger_behavior": { + "options": { + "any": "Any", + "first": "First", + "last": "Last" + } + } + }, + "title": "Window", + "triggers": { + "closed": { + "description": "Triggers after one or more windows close.", + "fields": { + "behavior": { + "description": "[%key:component::window::common::trigger_behavior_description%]", + "name": "[%key:component::window::common::trigger_behavior_name%]" + } + }, + "name": "Window closed" + }, + "opened": { + "description": "Triggers after one or more windows open.", + "fields": { + "behavior": { + "description": "[%key:component::window::common::trigger_behavior_description%]", + "name": "[%key:component::window::common::trigger_behavior_name%]" + } + }, + "name": "Window opened" + } + } +} diff --git a/homeassistant/components/window/trigger.py b/homeassistant/components/window/trigger.py new file mode 100644 index 00000000000..71ee204a2b0 --- /dev/null +++ b/homeassistant/components/window/trigger.py @@ -0,0 +1,36 @@ +"""Provides triggers for windows.""" + +from homeassistant.components.binary_sensor import ( + DOMAIN as BINARY_SENSOR_DOMAIN, + BinarySensorDeviceClass, +) +from homeassistant.components.cover import ( + DOMAIN as COVER_DOMAIN, + CoverDeviceClass, + make_cover_closed_trigger, + make_cover_opened_trigger, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers.trigger import Trigger + +DEVICE_CLASSES_WINDOW: dict[str, str] = { + BINARY_SENSOR_DOMAIN: BinarySensorDeviceClass.WINDOW, + COVER_DOMAIN: CoverDeviceClass.WINDOW, +} + + +TRIGGERS: dict[str, type[Trigger]] = { + "opened": make_cover_opened_trigger( + device_classes=DEVICE_CLASSES_WINDOW, + domains={BINARY_SENSOR_DOMAIN, COVER_DOMAIN}, + ), + "closed": make_cover_closed_trigger( + device_classes=DEVICE_CLASSES_WINDOW, + domains={BINARY_SENSOR_DOMAIN, COVER_DOMAIN}, + ), +} + + +async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]: + """Return the triggers for windows.""" + return TRIGGERS diff --git a/homeassistant/components/window/triggers.yaml b/homeassistant/components/window/triggers.yaml new file mode 100644 index 00000000000..4d770a85d2c --- /dev/null +++ b/homeassistant/components/window/triggers.yaml @@ -0,0 +1,29 @@ +.trigger_common_fields: &trigger_common_fields + behavior: + required: true + default: any + selector: + select: + translation_key: trigger_behavior + options: + - first + - last + - any + +closed: + fields: *trigger_common_fields + target: + entity: + - domain: binary_sensor + device_class: window + - domain: cover + device_class: window + +opened: + fields: *trigger_common_fields + target: + entity: + - domain: binary_sensor + device_class: window + - domain: cover + device_class: window diff --git a/script/hassfest/manifest.py b/script/hassfest/manifest.py index 538524696c1..344264c1e54 100644 --- a/script/hassfest/manifest.py +++ b/script/hassfest/manifest.py @@ -123,6 +123,7 @@ NO_IOT_CLASS = [ "web_rtc", "webhook", "websocket_api", + "window", "zone", ] diff --git a/script/hassfest/quality_scale.py b/script/hassfest/quality_scale.py index 0681953dd3f..5a7f717fbc1 100644 --- a/script/hassfest/quality_scale.py +++ b/script/hassfest/quality_scale.py @@ -2157,6 +2157,7 @@ NO_QUALITY_SCALE = [ "web_rtc", "webhook", "websocket_api", + "window", "zone", ] diff --git a/tests/components/window/__init__.py b/tests/components/window/__init__.py new file mode 100644 index 00000000000..bd812bd6bfe --- /dev/null +++ b/tests/components/window/__init__.py @@ -0,0 +1 @@ +"""Tests for the window integration.""" diff --git a/tests/components/window/test_trigger.py b/tests/components/window/test_trigger.py new file mode 100644 index 00000000000..3a8965b2bdd --- /dev/null +++ b/tests/components/window/test_trigger.py @@ -0,0 +1,646 @@ +"""Test window trigger.""" + +from typing import Any + +import pytest + +from homeassistant.components.cover import ATTR_IS_CLOSED, CoverState +from homeassistant.const import ( + ATTR_DEVICE_CLASS, + ATTR_LABEL_ID, + CONF_ENTITY_ID, + STATE_OFF, + STATE_ON, +) +from homeassistant.core import HomeAssistant, ServiceCall + +from tests.components import ( + TriggerStateDescription, + arm_trigger, + parametrize_target_entities, + parametrize_trigger_states, + set_or_remove_state, + target_entities, +) + + +@pytest.fixture +async def target_binary_sensors(hass: HomeAssistant) -> dict[str, list[str]]: + """Create multiple binary sensor entities associated with different targets.""" + return await target_entities(hass, "binary_sensor") + + +@pytest.fixture +async def target_covers(hass: HomeAssistant) -> dict[str, list[str]]: + """Create multiple cover entities associated with different targets.""" + return await target_entities(hass, "cover") + + +@pytest.mark.parametrize( + "trigger_key", + [ + "window.opened", + "window.closed", + ], +) +async def test_window_triggers_gated_by_labs_flag( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture, trigger_key: str +) -> None: + """Test the window triggers are gated by the labs flag.""" + await arm_trigger(hass, trigger_key, None, {ATTR_LABEL_ID: "test_label"}) + assert ( + "Unnamed automation failed to setup triggers and has been disabled: Trigger " + f"'{trigger_key}' requires the experimental 'New triggers and conditions' " + "feature to be enabled in Home Assistant Labs settings (feature flag: " + "'new_triggers_conditions')" + ) in caplog.text + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ("trigger_target_config", "entity_id", "entities_in_target"), + parametrize_target_entities("binary_sensor"), +) +@pytest.mark.parametrize( + ("trigger", "trigger_options", "states"), + [ + *parametrize_trigger_states( + trigger="window.opened", + target_states=[STATE_ON], + other_states=[STATE_OFF], + additional_attributes={ATTR_DEVICE_CLASS: "window"}, + trigger_from_none=False, + ), + *parametrize_trigger_states( + trigger="window.closed", + target_states=[STATE_OFF], + other_states=[STATE_ON], + additional_attributes={ATTR_DEVICE_CLASS: "window"}, + trigger_from_none=False, + ), + ], +) +async def test_window_trigger_binary_sensor_behavior_any( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_binary_sensors: dict[str, list[str]], + trigger_target_config: dict, + entity_id: str, + entities_in_target: int, + trigger: str, + trigger_options: dict[str, Any], + states: list[TriggerStateDescription], +) -> None: + """Test window trigger fires for binary_sensor entities with device_class window.""" + other_entity_ids = set(target_binary_sensors["included"]) - {entity_id} + excluded_entity_ids = set(target_binary_sensors["excluded"]) - {entity_id} + + for eid in target_binary_sensors["included"]: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + for eid in excluded_entity_ids: + set_or_remove_state(hass, eid, states[0]["excluded"]) + await hass.async_block_till_done() + + await arm_trigger(hass, trigger, {}, trigger_target_config) + + for state in states[1:]: + excluded_state = state["excluded"] + included_state = state["included"] + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, included_state) + await hass.async_block_till_done() + for excluded_entity_id in excluded_entity_ids: + set_or_remove_state(hass, excluded_entity_id, excluded_state) + await hass.async_block_till_done() + assert len(service_calls) == (entities_in_target - 1) * state["count"] + service_calls.clear() + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ("trigger_target_config", "entity_id", "entities_in_target"), + parametrize_target_entities("cover"), +) +@pytest.mark.parametrize( + ("trigger", "trigger_options", "states"), + [ + *parametrize_trigger_states( + trigger="window.opened", + target_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: False}), + (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + ], + other_states=[ + (CoverState.CLOSED, {ATTR_IS_CLOSED: True}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: True}), + ], + extra_invalid_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: None}), + (CoverState.OPEN, {}), + ], + additional_attributes={ATTR_DEVICE_CLASS: "window"}, + trigger_from_none=False, + ), + *parametrize_trigger_states( + trigger="window.closed", + target_states=[ + (CoverState.CLOSED, {ATTR_IS_CLOSED: True}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: True}), + ], + other_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: False}), + (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: False}), + ], + extra_invalid_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: None}), + (CoverState.OPEN, {}), + ], + additional_attributes={ATTR_DEVICE_CLASS: "window"}, + trigger_from_none=False, + ), + ], +) +async def test_window_trigger_cover_behavior_any( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_covers: dict[str, list[str]], + trigger_target_config: dict, + entity_id: str, + entities_in_target: int, + trigger: str, + trigger_options: dict[str, Any], + states: list[TriggerStateDescription], +) -> None: + """Test window trigger fires for cover entities with device_class window.""" + other_entity_ids = set(target_covers["included"]) - {entity_id} + excluded_entity_ids = set(target_covers["excluded"]) - {entity_id} + + for eid in target_covers["included"]: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + for eid in excluded_entity_ids: + set_or_remove_state(hass, eid, states[0]["excluded"]) + await hass.async_block_till_done() + + await arm_trigger(hass, trigger, {}, trigger_target_config) + + for state in states[1:]: + excluded_state = state["excluded"] + included_state = state["included"] + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, included_state) + await hass.async_block_till_done() + for excluded_entity_id in excluded_entity_ids: + set_or_remove_state(hass, excluded_entity_id, excluded_state) + await hass.async_block_till_done() + assert len(service_calls) == (entities_in_target - 1) * state["count"] + service_calls.clear() + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ("trigger_target_config", "entity_id", "entities_in_target"), + parametrize_target_entities("binary_sensor"), +) +@pytest.mark.parametrize( + ("trigger", "trigger_options", "states"), + [ + *parametrize_trigger_states( + trigger="window.opened", + target_states=[STATE_ON], + other_states=[STATE_OFF], + additional_attributes={ATTR_DEVICE_CLASS: "window"}, + trigger_from_none=False, + ), + *parametrize_trigger_states( + trigger="window.closed", + target_states=[STATE_OFF], + other_states=[STATE_ON], + additional_attributes={ATTR_DEVICE_CLASS: "window"}, + trigger_from_none=False, + ), + ], +) +async def test_window_trigger_binary_sensor_behavior_first( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_binary_sensors: dict[str, list[str]], + trigger_target_config: dict, + entity_id: str, + entities_in_target: int, + trigger: str, + trigger_options: dict[str, Any], + states: list[TriggerStateDescription], +) -> None: + """Test window trigger fires on the first binary_sensor state change.""" + other_entity_ids = set(target_binary_sensors["included"]) - {entity_id} + excluded_entity_ids = set(target_binary_sensors["excluded"]) - {entity_id} + + for eid in target_binary_sensors["included"]: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + for eid in excluded_entity_ids: + set_or_remove_state(hass, eid, states[0]["excluded"]) + await hass.async_block_till_done() + + await arm_trigger(hass, trigger, {"behavior": "first"}, trigger_target_config) + + for state in states[1:]: + excluded_state = state["excluded"] + included_state = state["included"] + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, excluded_state) + await hass.async_block_till_done() + for excluded_entity_id in excluded_entity_ids: + set_or_remove_state(hass, excluded_entity_id, excluded_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ("trigger_target_config", "entity_id", "entities_in_target"), + parametrize_target_entities("binary_sensor"), +) +@pytest.mark.parametrize( + ("trigger", "trigger_options", "states"), + [ + *parametrize_trigger_states( + trigger="window.opened", + target_states=[STATE_ON], + other_states=[STATE_OFF], + additional_attributes={ATTR_DEVICE_CLASS: "window"}, + trigger_from_none=False, + ), + *parametrize_trigger_states( + trigger="window.closed", + target_states=[STATE_OFF], + other_states=[STATE_ON], + additional_attributes={ATTR_DEVICE_CLASS: "window"}, + trigger_from_none=False, + ), + ], +) +async def test_window_trigger_binary_sensor_behavior_last( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_binary_sensors: dict[str, list[str]], + trigger_target_config: dict, + entity_id: str, + entities_in_target: int, + trigger: str, + trigger_options: dict[str, Any], + states: list[TriggerStateDescription], +) -> None: + """Test window trigger fires when the last binary_sensor changes state.""" + other_entity_ids = set(target_binary_sensors["included"]) - {entity_id} + excluded_entity_ids = set(target_binary_sensors["excluded"]) - {entity_id} + + for eid in target_binary_sensors["included"]: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + for eid in excluded_entity_ids: + set_or_remove_state(hass, eid, states[0]["excluded"]) + await hass.async_block_till_done() + + await arm_trigger(hass, trigger, {"behavior": "last"}, trigger_target_config) + + for state in states[1:]: + excluded_state = state["excluded"] + included_state = state["included"] + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, excluded_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for excluded_entity_id in excluded_entity_ids: + set_or_remove_state(hass, excluded_entity_id, excluded_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ("trigger_target_config", "entity_id", "entities_in_target"), + parametrize_target_entities("cover"), +) +@pytest.mark.parametrize( + ("trigger", "trigger_options", "states"), + [ + *parametrize_trigger_states( + trigger="window.opened", + target_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: False}), + (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + ], + other_states=[ + (CoverState.CLOSED, {ATTR_IS_CLOSED: True}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: True}), + ], + extra_invalid_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: None}), + (CoverState.OPEN, {}), + ], + additional_attributes={ATTR_DEVICE_CLASS: "window"}, + trigger_from_none=False, + ), + *parametrize_trigger_states( + trigger="window.closed", + target_states=[ + (CoverState.CLOSED, {ATTR_IS_CLOSED: True}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: True}), + ], + other_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: False}), + (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: False}), + ], + extra_invalid_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: None}), + (CoverState.OPEN, {}), + ], + additional_attributes={ATTR_DEVICE_CLASS: "window"}, + trigger_from_none=False, + ), + ], +) +async def test_window_trigger_cover_behavior_first( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_covers: dict[str, list[str]], + trigger_target_config: dict, + entity_id: str, + entities_in_target: int, + trigger: str, + trigger_options: dict[str, Any], + states: list[TriggerStateDescription], +) -> None: + """Test window trigger fires on the first cover state change.""" + other_entity_ids = set(target_covers["included"]) - {entity_id} + excluded_entity_ids = set(target_covers["excluded"]) - {entity_id} + + for eid in target_covers["included"]: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + for eid in excluded_entity_ids: + set_or_remove_state(hass, eid, states[0]["excluded"]) + await hass.async_block_till_done() + + await arm_trigger(hass, trigger, {"behavior": "first"}, trigger_target_config) + + for state in states[1:]: + excluded_state = state["excluded"] + included_state = state["included"] + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, excluded_state) + await hass.async_block_till_done() + for excluded_entity_id in excluded_entity_ids: + set_or_remove_state(hass, excluded_entity_id, excluded_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ("trigger_target_config", "entity_id", "entities_in_target"), + parametrize_target_entities("cover"), +) +@pytest.mark.parametrize( + ("trigger", "trigger_options", "states"), + [ + *parametrize_trigger_states( + trigger="window.opened", + target_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: False}), + (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + ], + other_states=[ + (CoverState.CLOSED, {ATTR_IS_CLOSED: True}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: True}), + ], + extra_invalid_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: None}), + (CoverState.OPEN, {}), + ], + additional_attributes={ATTR_DEVICE_CLASS: "window"}, + trigger_from_none=False, + ), + *parametrize_trigger_states( + trigger="window.closed", + target_states=[ + (CoverState.CLOSED, {ATTR_IS_CLOSED: True}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: True}), + ], + other_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: False}), + (CoverState.OPENING, {ATTR_IS_CLOSED: False}), + (CoverState.CLOSING, {ATTR_IS_CLOSED: False}), + ], + extra_invalid_states=[ + (CoverState.OPEN, {ATTR_IS_CLOSED: None}), + (CoverState.OPEN, {}), + ], + additional_attributes={ATTR_DEVICE_CLASS: "window"}, + trigger_from_none=False, + ), + ], +) +async def test_window_trigger_cover_behavior_last( + hass: HomeAssistant, + service_calls: list[ServiceCall], + target_covers: dict[str, list[str]], + trigger_target_config: dict, + entity_id: str, + entities_in_target: int, + trigger: str, + trigger_options: dict[str, Any], + states: list[TriggerStateDescription], +) -> None: + """Test window trigger fires when the last cover changes state.""" + other_entity_ids = set(target_covers["included"]) - {entity_id} + excluded_entity_ids = set(target_covers["excluded"]) - {entity_id} + + for eid in target_covers["included"]: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + for eid in excluded_entity_ids: + set_or_remove_state(hass, eid, states[0]["excluded"]) + await hass.async_block_till_done() + + await arm_trigger(hass, trigger, {"behavior": "last"}, trigger_target_config) + + for state in states[1:]: + excluded_state = state["excluded"] + included_state = state["included"] + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, excluded_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert len(service_calls) == state["count"] + for service_call in service_calls: + assert service_call.data[CONF_ENTITY_ID] == entity_id + service_calls.clear() + + for excluded_entity_id in excluded_entity_ids: + set_or_remove_state(hass, excluded_entity_id, excluded_state) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + +@pytest.mark.usefixtures("enable_labs_preview_features") +@pytest.mark.parametrize( + ( + "trigger_key", + "binary_sensor_initial", + "binary_sensor_target", + "cover_initial", + "cover_initial_is_closed", + "cover_target", + "cover_target_is_closed", + ), + [ + ( + "window.opened", + STATE_OFF, + STATE_ON, + CoverState.CLOSED, + True, + CoverState.OPEN, + False, + ), + ( + "window.closed", + STATE_ON, + STATE_OFF, + CoverState.OPEN, + False, + CoverState.CLOSED, + True, + ), + ], +) +async def test_window_trigger_excludes_non_window_device_class( + hass: HomeAssistant, + service_calls: list[ServiceCall], + trigger_key: str, + binary_sensor_initial: str, + binary_sensor_target: str, + cover_initial: str, + cover_initial_is_closed: bool, + cover_target: str, + cover_target_is_closed: bool, +) -> None: + """Test window trigger does not fire for entities without device_class window.""" + entity_id_window = "binary_sensor.test_window" + entity_id_door = "binary_sensor.test_door" + entity_id_cover_window = "cover.test_window" + entity_id_cover_door = "cover.test_door" + + # Set initial states + hass.states.async_set( + entity_id_window, binary_sensor_initial, {ATTR_DEVICE_CLASS: "window"} + ) + hass.states.async_set( + entity_id_door, binary_sensor_initial, {ATTR_DEVICE_CLASS: "door"} + ) + hass.states.async_set( + entity_id_cover_window, + cover_initial, + {ATTR_DEVICE_CLASS: "window", ATTR_IS_CLOSED: cover_initial_is_closed}, + ) + hass.states.async_set( + entity_id_cover_door, + cover_initial, + {ATTR_DEVICE_CLASS: "door", ATTR_IS_CLOSED: cover_initial_is_closed}, + ) + await hass.async_block_till_done() + + await arm_trigger( + hass, + trigger_key, + {}, + { + CONF_ENTITY_ID: [ + entity_id_window, + entity_id_door, + entity_id_cover_window, + entity_id_cover_door, + ] + }, + ) + + # Window binary_sensor changes - should trigger + hass.states.async_set( + entity_id_window, binary_sensor_target, {ATTR_DEVICE_CLASS: "window"} + ) + await hass.async_block_till_done() + assert len(service_calls) == 1 + assert service_calls[0].data[CONF_ENTITY_ID] == entity_id_window + service_calls.clear() + + # Door binary_sensor changes - should NOT trigger (wrong device class) + hass.states.async_set( + entity_id_door, binary_sensor_target, {ATTR_DEVICE_CLASS: "door"} + ) + await hass.async_block_till_done() + assert len(service_calls) == 0 + + # Cover window changes - should trigger + hass.states.async_set( + entity_id_cover_window, + cover_target, + {ATTR_DEVICE_CLASS: "window", ATTR_IS_CLOSED: cover_target_is_closed}, + ) + await hass.async_block_till_done() + assert len(service_calls) == 1 + assert service_calls[0].data[CONF_ENTITY_ID] == entity_id_cover_window + service_calls.clear() + + # Door cover changes - should NOT trigger (wrong device class) + hass.states.async_set( + entity_id_cover_door, + cover_target, + {ATTR_DEVICE_CLASS: "door", ATTR_IS_CLOSED: cover_target_is_closed}, + ) + await hass.async_block_till_done() + assert len(service_calls) == 0 diff --git a/tests/snapshots/test_bootstrap.ambr b/tests/snapshots/test_bootstrap.ambr index 93faf22bfdf..a8368b1eae2 100644 --- a/tests/snapshots/test_bootstrap.ambr +++ b/tests/snapshots/test_bootstrap.ambr @@ -98,6 +98,7 @@ 'weather', 'web_rtc', 'websocket_api', + 'window', 'zone', }) # --- @@ -199,6 +200,7 @@ 'weather', 'web_rtc', 'websocket_api', + 'window', 'zone', }) # ---