Compare commits

...

3 Commits

Author SHA1 Message Date
Bram Kragten
5f6b60cb78 use mode instead 2026-03-25 15:59:13 +01:00
Bram Kragten
6b61984342 Update selector.py 2026-03-25 15:32:10 +01:00
Bram Kragten
bad52f0824 Add automation behavior selector 2026-03-25 14:48:44 +01:00
62 changed files with 223 additions and 271 deletions

View File

@@ -4,11 +4,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
# --- Number or entity selectors ---

View File

@@ -3,12 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger
.number_or_entity_co: &number_or_entity_co
required: false

View File

@@ -7,11 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_armed: *condition_common

View File

@@ -7,12 +7,9 @@
required: true
default: any
selector:
select:
options:
- first
- last
- any
automation_behavior:
translation_key: trigger_behavior
mode: trigger
armed: *trigger_common

View File

@@ -7,11 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_idle: *condition_common
is_listening: *condition_common

View File

@@ -7,12 +7,9 @@
required: true
default: any
selector:
select:
options:
- first
- last
- any
automation_behavior:
translation_key: trigger_behavior
mode: trigger
idle: *trigger_common
listening: *trigger_common

View File

@@ -8,11 +8,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
.number_or_entity: &number_or_entity
required: false

View File

@@ -7,11 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
.number_or_entity_humidity: &number_or_entity_humidity
required: false

View File

@@ -7,12 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger
.number_or_entity_humidity: &number_or_entity_humidity
required: false

View File

@@ -3,11 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
awning_is_closed:
fields: *condition_common_fields

View File

@@ -3,12 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger
awning_closed:
fields: *trigger_common_fields

View File

@@ -7,11 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_home: *condition_common
is_not_home: *condition_common

View File

@@ -7,12 +7,9 @@
required: true
default: any
selector:
select:
options:
- first
- last
- any
automation_behavior:
translation_key: trigger_behavior
mode: trigger
entered_home: *trigger_common
left_home: *trigger_common

View File

@@ -3,11 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_closed:
fields: *condition_common_fields

View File

@@ -3,12 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger
closed:
fields: *trigger_common_fields

View File

@@ -7,11 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_off: *condition_common
is_on: *condition_common

View File

@@ -7,12 +7,9 @@
required: true
default: any
selector:
select:
options:
- first
- last
- any
automation_behavior:
translation_key: trigger_behavior
mode: trigger
turned_on: *trigger_common
turned_off: *trigger_common

View File

@@ -3,11 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_closed:
fields: *condition_common_fields

View File

@@ -3,12 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger
closed:
fields: *trigger_common_fields

View File

@@ -3,11 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_closed:
fields: *condition_common_fields

View File

@@ -3,12 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger
closed:
fields: *trigger_common_fields

View File

@@ -7,11 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
.number_or_entity: &number_or_entity
required: false

View File

@@ -7,12 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger
started_drying: *trigger_common
started_humidifying: *trigger_common

View File

@@ -34,10 +34,8 @@ is_value:
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
above: *number_or_entity
below: *number_or_entity

View File

@@ -3,12 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger
.number_or_entity: &number_or_entity
required: false

View File

@@ -8,11 +8,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
.number_or_entity: &number_or_entity
required: false

View File

@@ -3,12 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger
.number_or_entity: &number_or_entity
required: false

View File

@@ -7,11 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_docked: *condition_common
is_encountering_an_error: *condition_common

View File

@@ -7,12 +7,9 @@
required: true
default: any
selector:
select:
options:
- first
- last
- any
automation_behavior:
translation_key: trigger_behavior
mode: trigger
docked: *trigger_common
errored: *trigger_common

View File

@@ -7,11 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_off: *condition_common
is_on: *condition_common

View File

@@ -7,12 +7,9 @@
required: true
default: any
selector:
select:
options:
- first
- last
- any
automation_behavior:
translation_key: trigger_behavior
mode: trigger
.number_or_entity: &number_or_entity
required: false

View File

@@ -7,11 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_jammed: *condition_common
is_locked: *condition_common

View File

@@ -7,12 +7,9 @@
required: true
default: any
selector:
select:
options:
- first
- last
- any
automation_behavior:
translation_key: trigger_behavior
mode: trigger
jammed: *trigger_common
locked: *trigger_common

View File

@@ -7,11 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_off: *condition_common
is_on: *condition_common

View File

@@ -7,9 +7,6 @@ stopped_playing:
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger

View File

@@ -3,12 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger
.number_or_entity: &number_or_entity
required: false

View File

@@ -3,11 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_detected:
fields: *condition_common_fields

View File

@@ -3,12 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger
detected:
fields: *trigger_common_fields

View File

@@ -3,11 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_detected:
fields: *condition_common_fields

View File

@@ -3,12 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger
detected:
fields: *trigger_common_fields

View File

@@ -7,11 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_home: *condition_common
is_not_home: *condition_common

View File

@@ -7,12 +7,9 @@
required: true
default: any
selector:
select:
options:
- first
- last
- any
automation_behavior:
translation_key: trigger_behavior
mode: trigger
entered_home: *trigger_common
left_home: *trigger_common

View File

@@ -51,11 +51,9 @@ is_value:
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
above: *number_or_entity_power
below: *number_or_entity_power
unit: *condition_unit_power

View File

@@ -3,12 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger
.number_or_entity: &number_or_entity
required: false

View File

@@ -7,12 +7,9 @@
required: true
default: any
selector:
select:
options:
- first
- last
- any
automation_behavior:
translation_key: trigger_behavior
mode: trigger
turned_off: *trigger_common
turned_on: *trigger_common

View File

@@ -7,11 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_off: *condition_common
is_on: *condition_common

View File

@@ -7,12 +7,9 @@
required: true
default: any
selector:
select:
options:
- first
- last
- any
automation_behavior:
translation_key: trigger_behavior
mode: trigger
turned_off: *trigger_common
turned_on: *trigger_common

View File

@@ -7,11 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_off: *condition_common
is_on: *condition_common

View File

@@ -7,12 +7,9 @@
required: true
default: any
selector:
select:
options:
- first
- last
- any
automation_behavior:
translation_key: trigger_behavior
mode: trigger
turned_off: *trigger_common
turned_on: *trigger_common

View File

@@ -7,11 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_off: *condition_common
is_on: *condition_common

View File

@@ -8,12 +8,9 @@
required: true
default: any
selector:
select:
options:
- first
- last
- any
automation_behavior:
translation_key: trigger_behavior
mode: trigger
turned_off: *trigger_common
turned_on: *trigger_common

View File

@@ -3,12 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger
.number_or_entity: &number_or_entity
required: false

View File

@@ -8,11 +8,9 @@ is_equal_to:
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
value:
required: true
selector:

View File

@@ -7,11 +7,8 @@
required: true
default: any
selector:
select:
options:
- first
- last
- any
automation_behavior:
translation_key: trigger_behavior
mode: trigger
update_became_available: *trigger_common

View File

@@ -7,11 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_cleaning: *condition_common
is_docked: *condition_common

View File

@@ -7,12 +7,9 @@
required: true
default: any
selector:
select:
options:
- first
- last
- any
automation_behavior:
translation_key: trigger_behavior
mode: trigger
docked: *trigger_common
errored: *trigger_common

View File

@@ -7,11 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
.number_or_entity_temperature: &number_or_entity_temperature
required: false

View File

@@ -7,12 +7,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger
.trigger_threshold_type: &trigger_threshold_type
required: true

View File

@@ -3,11 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: condition_behavior
options:
- all
- any
mode: condition
is_closed:
fields: *condition_common_fields

View File

@@ -3,12 +3,9 @@
required: true
default: any
selector:
select:
automation_behavior:
translation_key: trigger_behavior
options:
- first
- last
- any
mode: trigger
closed:
fields: *trigger_common_fields

View File

@@ -243,6 +243,69 @@ class DeviceFilterSelectorConfig(TypedDict, total=False):
model_id: str
class AutomationBehavior(StrEnum):
"""Possible behaviors for an automation behavior selector."""
ALL = "all"
FIRST = "first"
LAST = "last"
ANY = "any"
class AutomationBehaviorSelectorMode(StrEnum):
"""Possible modes for an automation behavior selector."""
TRIGGER = "trigger"
CONDITION = "condition"
_AUTOMATION_BEHAVIOR_MODES: dict[AutomationBehaviorSelectorMode, list[str]] = {
AutomationBehaviorSelectorMode.TRIGGER: [
AutomationBehavior.FIRST,
AutomationBehavior.LAST,
AutomationBehavior.ANY,
],
AutomationBehaviorSelectorMode.CONDITION: [
AutomationBehavior.ALL,
AutomationBehavior.ANY,
],
}
class AutomationBehaviorConfig(BaseSelectorConfig, total=False):
"""Class to represent an automation behavior selector config."""
mode: Required[AutomationBehaviorSelectorMode]
translation_key: str
@SELECTORS.register("automation_behavior")
class AutomationBehaviorSelector(Selector[AutomationBehaviorConfig]):
"""Selector of an automation behavior."""
selector_type = "automation_behavior"
CONFIG_SCHEMA = make_selector_config_schema(
{
vol.Required("mode"): vol.All(
vol.Coerce(AutomationBehaviorSelectorMode), lambda val: val.value
),
vol.Optional("translation_key"): cv.string,
},
)
def __init__(self, config: AutomationBehaviorConfig | None = None) -> None:
"""Instantiate a selector."""
super().__init__(config)
def __call__(self, data: Any) -> Any:
"""Validate the passed selection."""
if not isinstance(data, str):
raise vol.Invalid("Value should be a string")
mode = AutomationBehaviorSelectorMode(self.config["mode"])
return vol.In(_AUTOMATION_BEHAVIOR_MODES[mode])(data)
class ActionSelectorConfig(BaseSelectorConfig):
"""Class to represent an action selector config."""

View File

@@ -1183,6 +1183,46 @@ def test_action_selector_schema(schema, valid_selections, invalid_selections) ->
_test_selector("action", schema, valid_selections, invalid_selections)
@pytest.mark.parametrize(
("schema", "valid_selections", "invalid_selections"),
[
(
{"mode": "trigger"},
("first", "last", "any"),
("all", "invalid", None),
),
(
{"mode": "condition"},
("all", "any"),
("first", "last", "invalid", None),
),
(
{"mode": "trigger", "translation_key": "trigger_behavior"},
("first", "last", "any"),
("all", "invalid", None),
),
],
)
def test_automation_behavior_selector_schema(
schema, valid_selections, invalid_selections
) -> None:
"""Test automation behavior selector."""
_test_selector("automation_behavior", schema, valid_selections, invalid_selections)
@pytest.mark.parametrize(
"schema",
[
{},
{"mode": "invalid_mode"},
],
)
def test_automation_behavior_selector_schema_error(schema) -> None:
"""Test automation behavior selector config schema errors."""
with pytest.raises(vol.Invalid):
selector.validate_selector({"automation_behavior": schema})
@pytest.mark.parametrize(
("schema", "valid_selections", "invalid_selections"),
[