From 453307e01ad8c865ce30767463f5b5ab7acef263 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 20 Aug 2022 19:30:38 +0200 Subject: [PATCH] Add attribute support to state selector (#77071) --- homeassistant/helpers/selector.py | 10 ++++++++-- tests/helpers/test_selector.py | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/homeassistant/helpers/selector.py b/homeassistant/helpers/selector.py index deb5cc7401c..9655f93ace5 100644 --- a/homeassistant/helpers/selector.py +++ b/homeassistant/helpers/selector.py @@ -741,10 +741,11 @@ class TargetSelectorConfig(TypedDict, total=False): device: SingleDeviceSelectorConfig -class StateSelectorConfig(TypedDict): +class StateSelectorConfig(TypedDict, total=False): """Class to represent an state selector config.""" entity_id: str + attribute: str @SELECTORS.register("state") @@ -753,7 +754,12 @@ class StateSelector(Selector): selector_type = "state" - CONFIG_SCHEMA = vol.Schema({vol.Required("entity_id"): cv.entity_id}) + CONFIG_SCHEMA = vol.Schema( + { + vol.Required("entity_id"): cv.entity_id, + vol.Optional("attribute"): str, + } + ) def __init__(self, config: StateSelectorConfig) -> None: """Instantiate a selector.""" diff --git a/tests/helpers/test_selector.py b/tests/helpers/test_selector.py index 70e17058923..2d870a85f6f 100644 --- a/tests/helpers/test_selector.py +++ b/tests/helpers/test_selector.py @@ -302,6 +302,11 @@ def test_time_selector_schema(schema, valid_selections, invalid_selections): ("on", "armed"), (None, True, 1), ), + ( + {"entity_id": "sensor.abc", "attribute": "device_class"}, + ("temperature", "humidity"), + (None,), + ), ), ) def test_state_selector_schema(schema, valid_selections, invalid_selections):