Compare commits

...

24 Commits

Author SHA1 Message Date
Erik
a0cb699e15 Update snapshots 2026-03-05 20:21:12 +01:00
Erik
bee7b1da7d Remove door as dependency of garage_door and window 2026-03-05 17:57:42 +01:00
Erik
ac684b95dc Revert "Remove door as dependency of garage_door and window"
This reverts commit f114717b97.
2026-03-05 17:57:04 +01:00
Erik
f9baa1c7e2 Revert "Update cloud tests"
This reverts commit 90d7f8f779.
2026-03-05 17:38:25 +01:00
Erik
a767ddcfc0 Revert "Update tcp tests"
This reverts commit 707e8852fa.
2026-03-05 17:37:54 +01:00
Erik
6cdd97075b Revert "Update trend tests"
This reverts commit dab919bae3.
2026-03-05 17:37:47 +01:00
Erik
4bda34dfdb Revert "Update trigger helper tests"
This reverts commit b976c5bd16.
2026-03-05 17:37:11 +01:00
Erik
f114717b97 Remove door as dependency of garage_door and window 2026-03-05 17:34:23 +01:00
Erik Montnemery
743d841446 Merge branch 'dev' into add_trigger_door_opened 2026-03-05 17:30:54 +01:00
Erik
b976c5bd16 Update trigger helper tests 2026-03-05 17:04:39 +01:00
Erik
80724e507e Revert "Update trigger helper tests"
This reverts commit 79b4626a62.
2026-03-05 17:03:46 +01:00
Erik
79b4626a62 Update trigger helper tests 2026-03-05 10:03:21 +01:00
Erik
dab919bae3 Update trend tests 2026-03-05 10:03:02 +01:00
Erik
707e8852fa Update tcp tests 2026-03-05 10:02:40 +01:00
Erik
90d7f8f779 Update cloud tests 2026-03-05 10:01:34 +01:00
Erik
b024c4811a Add async_setup 2026-03-05 09:09:26 +01:00
Erik
9312791164 Make binary_sensor and cover depend on door 2026-03-05 08:46:22 +01:00
Erik
bd22e526ad Address review comments 2026-03-05 08:33:59 +01:00
Erik
ae8f9b8e3a Address comments 2026-03-04 19:18:34 +01:00
Erik
45145e6cc0 Adjust strings 2026-03-04 17:05:22 +01:00
Erik
ee4aac8747 Add tests 2026-03-04 17:02:59 +01:00
Erik Montnemery
2c836a782e Merge branch 'dev' into add_trigger_door_opened 2026-03-04 16:55:27 +01:00
Erik
3a58771bcc Use is_closed state attribute for cover 2026-03-04 16:44:17 +01:00
Erik
a855e0018b Add trigger door.opened 2026-03-04 11:38:28 +01:00
15 changed files with 747 additions and 21 deletions

2
CODEOWNERS generated
View File

@@ -383,6 +383,8 @@ build.json @home-assistant/supervisor
/tests/components/dlna_dms/ @chishm
/homeassistant/components/dnsip/ @gjohansson-ST
/tests/components/dnsip/ @gjohansson-ST
/homeassistant/components/door/ @home-assistant/core
/tests/components/door/ @home-assistant/core
/homeassistant/components/doorbird/ @oblogic7 @bdraco @flacjacket
/tests/components/doorbird/ @oblogic7 @bdraco @flacjacket
/homeassistant/components/dormakaba_dkey/ @emontnemery

View File

@@ -239,6 +239,9 @@ DEFAULT_INTEGRATIONS = {
#
# Base platforms:
*BASE_PLATFORMS,
#
# Integrations providing triggers and conditions for base platforms:
"door",
}
DEFAULT_INTEGRATIONS_RECOVERY_MODE = {
# These integrations are set up if recovery mode is activated.

View File

@@ -142,6 +142,7 @@ _EXPERIMENTAL_TRIGGER_PLATFORMS = {
"climate",
"cover",
"device_tracker",
"door",
"fan",
"humidifier",
"lawn_mower",

View File

@@ -0,0 +1,15 @@
"""Integration for door 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 = "door"
CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the component."""
return True

View File

@@ -0,0 +1,7 @@
{
"triggers": {
"opened": {
"trigger": "mdi:door-open"
}
}
}

View File

@@ -0,0 +1,8 @@
{
"domain": "door",
"name": "Door",
"codeowners": ["@home-assistant/core"],
"documentation": "https://www.home-assistant.io/integrations/door",
"integration_type": "system",
"quality_scale": "internal"
}

View File

@@ -0,0 +1,28 @@
{
"common": {
"trigger_behavior_description": "The behavior of the targeted doors to trigger on.",
"trigger_behavior_name": "Behavior"
},
"selector": {
"trigger_behavior": {
"options": {
"any": "Any",
"first": "First",
"last": "Last"
}
}
},
"title": "Door",
"triggers": {
"opened": {
"description": "Triggers after one or more doors open.",
"fields": {
"behavior": {
"description": "[%key:component::door::common::trigger_behavior_description%]",
"name": "[%key:component::door::common::trigger_behavior_name%]"
}
},
"name": "Door opened"
}
}
}

View File

@@ -0,0 +1,75 @@
"""Provides triggers for doors."""
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.cover import ATTR_IS_CLOSED, DOMAIN as COVER_DOMAIN
from homeassistant.const import STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.core import HomeAssistant, State, split_entity_id
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity import get_device_class
from homeassistant.helpers.trigger import EntityTriggerBase, Trigger
from homeassistant.helpers.typing import UNDEFINED, UndefinedType
DEVICE_CLASS_DOOR = "door"
def get_device_class_or_undefined(
hass: HomeAssistant, entity_id: str
) -> str | None | UndefinedType:
"""Get the device class of an entity or UNDEFINED if not found."""
try:
return get_device_class(hass, entity_id)
except HomeAssistantError:
return UNDEFINED
class DoorTriggerBase(EntityTriggerBase):
"""Base trigger for door state changes."""
_domains = {BINARY_SENSOR_DOMAIN, COVER_DOMAIN}
_binary_sensor_target_state: str
_cover_is_closed_target_value: bool
def entity_filter(self, entities: set[str]) -> set[str]:
"""Filter entities by door device class."""
entities = super().entity_filter(entities)
return {
entity_id
for entity_id in entities
if get_device_class_or_undefined(self._hass, entity_id) == DEVICE_CLASS_DOOR
}
def is_valid_state(self, state: State) -> bool:
"""Check if the state matches the target door state."""
if split_entity_id(state.entity_id)[0] == COVER_DOMAIN:
return (
state.attributes.get(ATTR_IS_CLOSED)
== self._cover_is_closed_target_value
)
return state.state == self._binary_sensor_target_state
def is_valid_transition(self, from_state: State, to_state: State) -> bool:
"""Check if the transition is valid for a door state change."""
if from_state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN):
return False
if split_entity_id(from_state.entity_id)[0] == COVER_DOMAIN:
if (from_is_closed := from_state.attributes.get(ATTR_IS_CLOSED)) is None:
return False
return from_is_closed != to_state.attributes.get(ATTR_IS_CLOSED)
return from_state.state != to_state.state
class DoorOpenedTrigger(DoorTriggerBase):
"""Trigger for door opened state changes."""
_binary_sensor_target_state = STATE_ON
_cover_is_closed_target_value = False
TRIGGERS: dict[str, type[Trigger]] = {
"opened": DoorOpenedTrigger,
}
async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]:
"""Return the triggers for doors."""
return TRIGGERS

View File

@@ -0,0 +1,20 @@
.trigger_common_fields: &trigger_common_fields
behavior:
required: true
default: any
selector:
select:
translation_key: trigger_behavior
options:
- first
- last
- any
opened:
fields: *trigger_common_fields
target:
entity:
- domain: binary_sensor
device_class: door
- domain: cover
device_class: door

View File

@@ -71,6 +71,7 @@ NO_IOT_CLASS = [
"device_automation",
"device_tracker",
"diagnostics",
"door",
"downloader",
"ffmpeg",
"file_upload",

View File

@@ -2110,6 +2110,7 @@ NO_QUALITY_SCALE = [
"device_automation",
"device_tracker",
"diagnostics",
"door",
"ffmpeg",
"file_upload",
"frontend",

View File

@@ -355,17 +355,20 @@ def parametrize_trigger_states(
trigger_options: dict[str, Any] | None = None,
target_states: list[str | None | tuple[str | None, dict]],
other_states: list[str | None | tuple[str | None, dict]],
extra_invalid_states: list[str | None | tuple[str | None, dict]] | None = None,
additional_attributes: dict | None = None,
trigger_from_none: bool = True,
retrigger_on_target_state: bool = False,
) -> list[tuple[str, dict[str, Any], list[TriggerStateDescription]]]:
"""Parametrize states and expected service call counts.
The target_states and other_states iterables are either iterables of
states or iterables of (state, attributes) tuples.
The target_states, other_states, and extra_invalid_states iterables are
either iterables of states or iterables of (state, attributes) tuples.
Set `trigger_from_none` to False if the trigger is not expected to fire
when the initial state is None.
when the initial state is None, this is relevant for triggers that limit
entities to a certain device class because the device class can't be
determined when the state is None.
Set `retrigger_on_target_state` to True if the trigger is expected to fire
when the state changes to another target state.
@@ -374,6 +377,8 @@ def parametrize_trigger_states(
where states is a list of TriggerStateDescription dicts.
"""
extra_invalid_states = extra_invalid_states or []
invalid_states = [STATE_UNAVAILABLE, STATE_UNKNOWN, *extra_invalid_states]
additional_attributes = additional_attributes or {}
trigger_options = trigger_options or {}
@@ -463,34 +468,19 @@ def parametrize_trigger_states(
)
),
),
# Initial state unavailable / unknown
# Initial state unavailable / unknown + extra invalid states
(
trigger,
trigger_options,
list(
itertools.chain.from_iterable(
(
state_with_attributes(STATE_UNAVAILABLE, 0),
state_with_attributes(target_state, 0),
state_with_attributes(other_state, 0),
state_with_attributes(target_state, 1),
)
for target_state in target_states
for other_state in other_states
)
),
),
(
trigger,
trigger_options,
list(
itertools.chain.from_iterable(
(
state_with_attributes(STATE_UNKNOWN, 0),
state_with_attributes(invalid_state, 0),
state_with_attributes(target_state, 0),
state_with_attributes(other_state, 0),
state_with_attributes(target_state, 1),
)
for invalid_state in invalid_states
for target_state in target_states
for other_state in other_states
)

View File

@@ -0,0 +1 @@
"""Tests for the door integration."""

View File

@@ -0,0 +1,572 @@
"""Test door trigger."""
from typing import Any
import pytest
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from homeassistant.components.cover import ATTR_IS_CLOSED, CoverDeviceClass, CoverState
from homeassistant.components.door.trigger import DEVICE_CLASS_DOOR
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",
[
"door.opened",
],
)
async def test_door_triggers_gated_by_labs_flag(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, trigger_key: str
) -> None:
"""Test the door 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="door.opened",
target_states=[STATE_ON],
other_states=[STATE_OFF],
additional_attributes={ATTR_DEVICE_CLASS: "door"},
trigger_from_none=False,
),
],
)
async def test_door_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 door trigger fires for binary_sensor entities with device_class door."""
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="door.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}),
(CoverState.OPEN, {ATTR_IS_CLOSED: True}),
],
extra_invalid_states=[
(CoverState.OPEN, {ATTR_IS_CLOSED: None}),
(CoverState.OPEN, {}),
],
additional_attributes={ATTR_DEVICE_CLASS: "door"},
trigger_from_none=False,
),
],
)
async def test_door_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 door trigger fires for cover entities with device_class door."""
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="door.opened",
target_states=[STATE_ON],
other_states=[STATE_OFF],
additional_attributes={ATTR_DEVICE_CLASS: "door"},
trigger_from_none=False,
),
],
)
async def test_door_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 door 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="door.opened",
target_states=[STATE_ON],
other_states=[STATE_OFF],
additional_attributes={ATTR_DEVICE_CLASS: "door"},
trigger_from_none=False,
),
],
)
async def test_door_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 door 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="door.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}),
(CoverState.OPEN, {ATTR_IS_CLOSED: True}),
],
extra_invalid_states=[
(CoverState.OPEN, {ATTR_IS_CLOSED: None}),
(CoverState.OPEN, {}),
],
additional_attributes={ATTR_DEVICE_CLASS: "door"},
trigger_from_none=False,
),
],
)
async def test_door_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 door 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="door.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}),
(CoverState.OPEN, {ATTR_IS_CLOSED: True}),
],
extra_invalid_states=[
(CoverState.OPEN, {ATTR_IS_CLOSED: None}),
(CoverState.OPEN, {}),
],
additional_attributes={ATTR_DEVICE_CLASS: "door"},
trigger_from_none=False,
),
],
)
async def test_door_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 door 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",
),
[
(
"door.opened",
STATE_OFF,
STATE_ON,
CoverState.CLOSED,
True,
CoverState.OPEN,
False,
),
],
)
async def test_door_trigger_excludes_non_door_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 door trigger does not fire for entities without device_class door."""
entity_id_door = "binary_sensor.test_door"
entity_id_window = "binary_sensor.test_window"
entity_id_cover_door = "cover.test_door"
entity_id_cover_garage = "cover.test_garage"
# Set initial states
hass.states.async_set(
entity_id_door, binary_sensor_initial, {ATTR_DEVICE_CLASS: "door"}
)
hass.states.async_set(
entity_id_window, binary_sensor_initial, {ATTR_DEVICE_CLASS: "window"}
)
hass.states.async_set(
entity_id_cover_door,
cover_initial,
{ATTR_DEVICE_CLASS: "door", ATTR_IS_CLOSED: cover_initial_is_closed},
)
hass.states.async_set(
entity_id_cover_garage,
cover_initial,
{ATTR_DEVICE_CLASS: "garage", ATTR_IS_CLOSED: cover_initial_is_closed},
)
await hass.async_block_till_done()
await arm_trigger(
hass,
trigger_key,
{},
{
CONF_ENTITY_ID: [
entity_id_door,
entity_id_window,
entity_id_cover_door,
entity_id_cover_garage,
]
},
)
# Door binary_sensor changes - should trigger
hass.states.async_set(
entity_id_door, binary_sensor_target, {ATTR_DEVICE_CLASS: "door"}
)
await hass.async_block_till_done()
assert len(service_calls) == 1
assert service_calls[0].data[CONF_ENTITY_ID] == entity_id_door
service_calls.clear()
# Window binary_sensor changes - should NOT trigger (wrong device class)
hass.states.async_set(
entity_id_window, binary_sensor_target, {ATTR_DEVICE_CLASS: "window"}
)
await hass.async_block_till_done()
assert len(service_calls) == 0
# Cover door changes - should trigger
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) == 1
assert service_calls[0].data[CONF_ENTITY_ID] == entity_id_cover_door
service_calls.clear()
# Garage cover changes - should NOT trigger (wrong device class)
hass.states.async_set(
entity_id_cover_garage,
cover_target,
{ATTR_DEVICE_CLASS: "garage", ATTR_IS_CLOSED: cover_target_is_closed},
)
await hass.async_block_till_done()
assert len(service_calls) == 0
def test_door_device_class() -> None:
"""Test the door trigger device class."""
assert BinarySensorDeviceClass.DOOR == DEVICE_CLASS_DOOR
assert CoverDeviceClass.DOOR == DEVICE_CLASS_DOOR

View File

@@ -30,6 +30,7 @@
'device_automation',
'device_tracker',
'diagnostics',
'door',
'event',
'fan',
'ffmpeg',
@@ -128,6 +129,7 @@
'device_automation',
'device_tracker',
'diagnostics',
'door',
'event',
'fan',
'ffmpeg',