mirror of
https://github.com/home-assistant/core.git
synced 2026-04-02 16:36:08 +01:00
61 lines
1.6 KiB
Python
61 lines
1.6 KiB
Python
"""Provides conditions for texts."""
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.components.input_text import DOMAIN as INPUT_TEXT_DOMAIN
|
|
from homeassistant.const import CONF_OPTIONS
|
|
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 (
|
|
ENTITY_STATE_CONDITION_SCHEMA_ANY_ALL,
|
|
Condition,
|
|
ConditionConfig,
|
|
EntityConditionBase,
|
|
)
|
|
|
|
from .const import DOMAIN
|
|
|
|
CONF_VALUE = "value"
|
|
|
|
_TEXT_CONDITION_SCHEMA = ENTITY_STATE_CONDITION_SCHEMA_ANY_ALL.extend(
|
|
{
|
|
vol.Required(CONF_OPTIONS): {
|
|
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)
|
|
if TYPE_CHECKING:
|
|
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
|