Add state selector (#77024)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Franck Nijhof 2022-08-19 15:24:53 +02:00 committed by GitHub
parent d8392ef6ba
commit 2d197fd59e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 0 deletions

View File

@ -741,6 +741,30 @@ class TargetSelectorConfig(TypedDict, total=False):
device: SingleDeviceSelectorConfig
class StateSelectorConfig(TypedDict):
"""Class to represent an state selector config."""
entity_id: str
@SELECTORS.register("state")
class StateSelector(Selector):
"""Selector for an entity state."""
selector_type = "state"
CONFIG_SCHEMA = vol.Schema({vol.Required("entity_id"): cv.entity_id})
def __init__(self, config: StateSelectorConfig) -> None:
"""Instantiate a selector."""
super().__init__(config)
def __call__(self, data: Any) -> str:
"""Validate the passed selection."""
state: str = vol.Schema(str)(data)
return state
@SELECTORS.register("target")
class TargetSelector(Selector):
"""Selector of a target value (area ID, device ID, entity ID etc).

View File

@ -294,6 +294,21 @@ def test_time_selector_schema(schema, valid_selections, invalid_selections):
_test_selector("time", schema, valid_selections, invalid_selections)
@pytest.mark.parametrize(
"schema,valid_selections,invalid_selections",
(
(
{"entity_id": "sensor.abc"},
("on", "armed"),
(None, True, 1),
),
),
)
def test_state_selector_schema(schema, valid_selections, invalid_selections):
"""Test state selector."""
_test_selector("state", schema, valid_selections, invalid_selections)
@pytest.mark.parametrize(
"schema,valid_selections,invalid_selections",
(