Compare commits

...

1 Commits

Author SHA1 Message Date
Erik
6f532d5aec Add select triggers 2026-03-12 10:37:56 +01:00
6 changed files with 175 additions and 1 deletions

View File

@@ -155,6 +155,7 @@ _EXPERIMENTAL_TRIGGER_PLATFORMS = {
"remote",
"scene",
"schedule",
"select",
"siren",
"switch",
"text",

View File

@@ -20,5 +20,10 @@
"select_previous": {
"service": "mdi:format-list-bulleted"
}
},
"triggers": {
"selection_changed": {
"trigger": "mdi:format-list-bulleted"
}
}
}

View File

@@ -76,5 +76,11 @@
"name": "Previous"
}
},
"title": "Select"
"title": "Select",
"triggers": {
"selection_changed": {
"description": "Triggers after one or more selections change.",
"name": "Selection changed"
}
}
}

View File

@@ -0,0 +1,32 @@
"""Provides triggers for selects."""
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.core import HomeAssistant, State
from homeassistant.helpers.trigger import (
ENTITY_STATE_TRIGGER_SCHEMA,
EntityTriggerBase,
Trigger,
)
from .const import DOMAIN
class SelectionChangedTrigger(EntityTriggerBase):
"""Trigger for select entity when its selection changes."""
_domains = {DOMAIN}
_schema = ENTITY_STATE_TRIGGER_SCHEMA
def is_valid_state(self, state: State) -> bool:
"""Check if the new state is not invalid."""
return state.state not in (STATE_UNAVAILABLE, STATE_UNKNOWN)
TRIGGERS: dict[str, type[Trigger]] = {
"selection_changed": SelectionChangedTrigger,
}
async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]:
"""Return the triggers for selects."""
return TRIGGERS

View File

@@ -0,0 +1,4 @@
selection_changed:
target:
entity:
domain: select

View File

@@ -0,0 +1,126 @@
"""Test select trigger."""
import pytest
from homeassistant.const import (
ATTR_LABEL_ID,
CONF_ENTITY_ID,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.core import HomeAssistant, ServiceCall
from tests.components import (
TriggerStateDescription,
arm_trigger,
parametrize_target_entities,
set_or_remove_state,
target_entities,
)
@pytest.fixture
async def target_selects(hass: HomeAssistant) -> list[str]:
"""Create multiple select entities associated with different targets."""
return (await target_entities(hass, "select"))["included"]
@pytest.mark.parametrize("trigger_key", ["select.selection_changed"])
async def test_select_triggers_gated_by_labs_flag(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, trigger_key: str
) -> None:
"""Test the select 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("select"),
)
@pytest.mark.parametrize(
("trigger", "states"),
[
(
"select.selection_changed",
[
{"included": {"state": None, "attributes": {}}, "count": 0},
{"included": {"state": "option_a", "attributes": {}}, "count": 0},
{"included": {"state": "option_b", "attributes": {}}, "count": 1},
],
),
(
"select.selection_changed",
[
{"included": {"state": "option_a", "attributes": {}}, "count": 0},
{"included": {"state": "option_b", "attributes": {}}, "count": 1},
{"included": {"state": "option_c", "attributes": {}}, "count": 1},
],
),
(
"select.selection_changed",
[
{
"included": {"state": STATE_UNAVAILABLE, "attributes": {}},
"count": 0,
},
{"included": {"state": "option_a", "attributes": {}}, "count": 0},
{"included": {"state": "option_b", "attributes": {}}, "count": 1},
{
"included": {"state": STATE_UNAVAILABLE, "attributes": {}},
"count": 0,
},
],
),
(
"select.selection_changed",
[
{"included": {"state": STATE_UNKNOWN, "attributes": {}}, "count": 0},
{"included": {"state": "option_a", "attributes": {}}, "count": 0},
{"included": {"state": "option_b", "attributes": {}}, "count": 1},
{"included": {"state": STATE_UNKNOWN, "attributes": {}}, "count": 0},
],
),
],
)
async def test_select_state_trigger_behavior_any(
hass: HomeAssistant,
service_calls: list[ServiceCall],
target_selects: list[str],
trigger_target_config: dict,
entity_id: str,
entities_in_target: int,
trigger: str,
states: list[TriggerStateDescription],
) -> None:
"""Test that the select trigger fires when any select state changes."""
other_entity_ids = set(target_selects) - {entity_id}
# Set all selects, including the tested select, to the initial state
for eid in target_selects:
set_or_remove_state(hass, eid, states[0]["included"])
await hass.async_block_till_done()
await arm_trigger(hass, trigger, None, trigger_target_config)
for state in states[1:]:
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()
# Check if changing other selects also triggers
for other_entity_id in other_entity_ids:
set_or_remove_state(hass, other_entity_id, included_state)
await hass.async_block_till_done()
assert len(service_calls) == (entities_in_target - 1) * state["count"]
service_calls.clear()