Compare commits

...

1 Commits

Author SHA1 Message Date
Erik
c0d6b5f5b4 Add text conditions 2026-03-18 15:15:50 +01:00
7 changed files with 345 additions and 1 deletions

View File

@@ -132,6 +132,7 @@ _EXPERIMENTAL_CONDITION_PLATFORMS = {
"person",
"siren",
"switch",
"text",
"vacuum",
}

View File

@@ -0,0 +1,63 @@
"""Provides conditions for texts."""
import voluptuous as vol
from homeassistant.components.input_text import DOMAIN as INPUT_TEXT_DOMAIN
from homeassistant.const import CONF_OPTIONS, CONF_TARGET
from homeassistant.core import HomeAssistant, State
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.automation import DomainSpec
from homeassistant.helpers.condition import (
ATTR_BEHAVIOR,
BEHAVIOR_ALL,
BEHAVIOR_ANY,
Condition,
ConditionConfig,
EntityConditionBase,
)
from .const import DOMAIN
CONF_VALUE = "value"
_TEXT_CONDITION_SCHEMA = vol.Schema(
{
vol.Required(CONF_TARGET): cv.TARGET_FIELDS,
vol.Required(CONF_OPTIONS): {
vol.Required(ATTR_BEHAVIOR, default=BEHAVIOR_ANY): vol.In(
[BEHAVIOR_ANY, BEHAVIOR_ALL]
),
vol.Required(CONF_VALUE): cv.string,
},
}
)
class TextIsEqualToCondition(EntityConditionBase):
"""Condition for text entity value matching."""
_domain_specs = {
DOMAIN: DomainSpec(),
INPUT_TEXT_DOMAIN: DomainSpec(),
}
_schema = _TEXT_CONDITION_SCHEMA
def __init__(self, hass: HomeAssistant, config: ConditionConfig) -> None:
"""Initialize condition."""
super().__init__(hass, config)
assert config.options
self._value: str = config.options[CONF_VALUE]
def is_valid_state(self, entity_state: State) -> bool:
"""Check if the state matches the expected value."""
return entity_state.state == self._value
CONDITIONS: dict[str, type[Condition]] = {
"is_equal_to": TextIsEqualToCondition,
}
async def async_get_conditions(hass: HomeAssistant) -> dict[str, type[Condition]]:
"""Return the text conditions."""
return CONDITIONS

View File

@@ -0,0 +1,19 @@
is_equal_to:
target:
entity:
- domain: text
- domain: input_text
fields:
behavior:
required: true
default: any
selector:
select:
translation_key: condition_behavior
options:
- all
- any
value:
required: true
selector:
text:

View File

@@ -1,4 +1,9 @@
{
"conditions": {
"is_equal_to": {
"condition": "mdi:form-textbox"
}
},
"entity_component": {
"_": {
"default": "mdi:form-textbox"

View File

@@ -1,4 +1,24 @@
{
"common": {
"condition_behavior_description": "The behavior of the targeted texts to check.",
"condition_behavior_name": "Behavior"
},
"conditions": {
"is_equal_to": {
"description": "Tests if one or more texts are equal to a specified value.",
"fields": {
"behavior": {
"description": "[%key:component::text::common::condition_behavior_description%]",
"name": "[%key:component::text::common::condition_behavior_name%]"
},
"value": {
"description": "The value to compare the text to.",
"name": "Value"
}
},
"name": "Text is equal to"
}
},
"device_automation": {
"action_type": {
"set_value": "Set value for {entity_name}"
@@ -30,6 +50,14 @@
}
}
},
"selector": {
"condition_behavior": {
"options": {
"all": "All",
"any": "Any"
}
}
},
"services": {
"set_value": {
"description": "Sets the value.",

View File

@@ -776,6 +776,7 @@ async def create_target_condition(
condition: str,
target: dict,
behavior: str,
condition_options: dict[str, Any] | None = None,
) -> ConditionCheckerTypeOptional:
"""Create a target condition."""
return await async_condition_from_config(
@@ -783,7 +784,7 @@ async def create_target_condition(
{
CONF_CONDITION: condition,
CONF_TARGET: target,
CONF_OPTIONS: {"behavior": behavior},
CONF_OPTIONS: {"behavior": behavior, **(condition_options or {})},
},
)
@@ -891,6 +892,7 @@ async def assert_condition_behavior_any(
condition=condition,
target=condition_target_config,
behavior="any",
condition_options=condition_options,
)
for state in states:
@@ -936,6 +938,7 @@ async def assert_condition_behavior_all(
condition=condition,
target=condition_target_config,
behavior="all",
condition_options=condition_options,
)
for state in states:

View File

@@ -0,0 +1,225 @@
"""Test text conditions."""
from typing import Any
import pytest
from homeassistant.components.text.condition import CONF_VALUE
from homeassistant.const import (
CONF_CONDITION,
CONF_ENTITY_ID,
CONF_OPTIONS,
CONF_TARGET,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.condition import (
async_from_config as async_condition_from_config,
)
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_texts(hass: HomeAssistant) -> dict[str, list[str]]:
"""Create multiple text entities associated with different targets."""
return await target_entities(hass, "text")
@pytest.fixture
async def target_input_texts(hass: HomeAssistant) -> dict[str, list[str]]:
"""Create multiple input_text entities associated with different targets."""
return await target_entities(hass, "input_text")
@pytest.mark.parametrize("condition", ["text.is_equal_to"])
async def test_text_conditions_gated_by_labs_flag(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, condition: str
) -> None:
"""Test the text conditions are gated by the labs flag."""
await assert_condition_gated_by_labs_flag(hass, caplog, condition)
CONDITION_STATES_ANY = [
*parametrize_condition_states_any(
condition="text.is_equal_to",
condition_options={CONF_VALUE: "hello"},
target_states=["hello"],
other_states=["world"],
),
]
CONDITION_STATES_ALL = [
*parametrize_condition_states_all(
condition="text.is_equal_to",
condition_options={CONF_VALUE: "hello"},
target_states=["hello"],
other_states=["world"],
),
]
@pytest.mark.usefixtures("enable_labs_preview_features")
@pytest.mark.parametrize(
("condition_target_config", "entity_id", "entities_in_target"),
parametrize_target_entities("text"),
)
@pytest.mark.parametrize(
("condition", "condition_options", "states"), CONDITION_STATES_ANY
)
async def test_text_condition_behavior_any(
hass: HomeAssistant,
target_texts: 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 text is_equal_to condition with the 'any' behavior."""
await assert_condition_behavior_any(
hass,
target_entities=target_texts,
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("input_text"),
)
@pytest.mark.parametrize(
("condition", "condition_options", "states"), CONDITION_STATES_ANY
)
async def test_input_text_condition_behavior_any(
hass: HomeAssistant,
target_input_texts: 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 text is_equal_to condition with input_text and the 'any' behavior."""
await assert_condition_behavior_any(
hass,
target_entities=target_input_texts,
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("text"),
)
@pytest.mark.parametrize(
("condition", "condition_options", "states"), CONDITION_STATES_ALL
)
async def test_text_condition_behavior_all(
hass: HomeAssistant,
target_texts: 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 text is_equal_to condition with the 'all' behavior."""
await assert_condition_behavior_all(
hass,
target_entities=target_texts,
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("input_text"),
)
@pytest.mark.parametrize(
("condition", "condition_options", "states"), CONDITION_STATES_ALL
)
async def test_input_text_condition_behavior_all(
hass: HomeAssistant,
target_input_texts: 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 text is_equal_to condition with input_text and the 'all' behavior."""
await assert_condition_behavior_all(
hass,
target_entities=target_input_texts,
condition_target_config=condition_target_config,
entity_id=entity_id,
entities_in_target=entities_in_target,
condition=condition,
condition_options=condition_options,
states=states,
)
# --- Cross-domain test ---
@pytest.mark.usefixtures("enable_labs_preview_features")
async def test_text_condition_fires_for_both_domains(
hass: HomeAssistant,
) -> None:
"""Test that the text condition works for both text and input_text entities."""
entity_id_text = "text.test_text"
entity_id_input_text = "input_text.test_input_text"
hass.states.async_set(entity_id_text, "hello")
hass.states.async_set(entity_id_input_text, "hello")
await hass.async_block_till_done()
checker = await async_condition_from_config(
hass,
{
CONF_CONDITION: "text.is_equal_to",
CONF_TARGET: {
CONF_ENTITY_ID: [entity_id_text, entity_id_input_text],
},
CONF_OPTIONS: {"behavior": "all", CONF_VALUE: "hello"},
},
)
assert checker(hass) is True
# Change input_text to non-matching - all behavior should fail
hass.states.async_set(entity_id_input_text, "world")
await hass.async_block_till_done()
assert checker(hass) is False