From d93687b5ac58c1009d270b766cab9bc76597edb1 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 2 Dec 2020 09:30:49 +0100 Subject: [PATCH] Update area and target selectors add sequence selector (#43831) Co-authored-by: Paulus Schoutsen --- .../automation/blueprints/motion_light.yaml | 1 + homeassistant/helpers/selector.py | 45 ++++++++++++++++++- tests/helpers/test_selector.py | 42 ++++++++++++++++- 3 files changed, 84 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/automation/blueprints/motion_light.yaml b/homeassistant/components/automation/blueprints/motion_light.yaml index cd6e5c7b281..8b17fbe91fe 100644 --- a/homeassistant/components/automation/blueprints/motion_light.yaml +++ b/homeassistant/components/automation/blueprints/motion_light.yaml @@ -23,6 +23,7 @@ blueprint: number: min: 0 max: 3600 + unit_of_measurement: seconds # If motion is detected within the delay, # we restart the script. diff --git a/homeassistant/helpers/selector.py b/homeassistant/helpers/selector.py index b4a90212bce..5cc7ada1bc5 100644 --- a/homeassistant/helpers/selector.py +++ b/homeassistant/helpers/selector.py @@ -79,7 +79,24 @@ class DeviceSelector(Selector): class AreaSelector(Selector): """Selector of a single area.""" - CONFIG_SCHEMA = vol.Schema({}) + CONFIG_SCHEMA = vol.Schema( + { + vol.Optional("entity"): vol.Schema( + { + vol.Optional("domain"): str, + vol.Optional("device_class"): str, + vol.Optional("integration"): str, + } + ), + vol.Optional("device"): vol.Schema( + { + vol.Optional("integration"): str, + vol.Optional("manufacturer"): str, + vol.Optional("model"): str, + } + ), + } + ) @SELECTORS.register("number") @@ -120,4 +137,28 @@ class TargetSelector(Selector): Value should follow cv.ENTITY_SERVICE_FIELDS format. """ - CONFIG_SCHEMA = vol.Schema({"entity": {"domain": str, "device_class": str}}) + CONFIG_SCHEMA = vol.Schema( + { + vol.Optional("entity"): vol.Schema( + { + vol.Optional("domain"): str, + vol.Optional("device_class"): str, + vol.Optional("integration"): str, + } + ), + vol.Optional("device"): vol.Schema( + { + vol.Optional("integration"): str, + vol.Optional("manufacturer"): str, + vol.Optional("model"): str, + } + ), + } + ) + + +@SELECTORS.register("action") +class ActionSelector(Selector): + """Selector of an action sequence (script syntax).""" + + CONFIG_SCHEMA = vol.Schema({}) diff --git a/tests/helpers/test_selector.py b/tests/helpers/test_selector.py index 531d36e4f50..86ee6078e87 100644 --- a/tests/helpers/test_selector.py +++ b/tests/helpers/test_selector.py @@ -79,7 +79,24 @@ def test_entity_selector_schema(schema): @pytest.mark.parametrize( "schema", - ({},), + ( + {}, + {"entity": {}}, + {"entity": {"domain": "light"}}, + {"entity": {"domain": "binary_sensor", "device_class": "motion"}}, + { + "entity": { + "domain": "binary_sensor", + "device_class": "motion", + "integration": "demo", + } + }, + {"device": {"integration": "demo", "model": "mock-model"}}, + { + "entity": {"domain": "binary_sensor", "device_class": "motion"}, + "device": {"integration": "demo", "model": "mock-model"}, + }, + ), ) def test_area_selector_schema(schema): """Test area selector.""" @@ -126,8 +143,29 @@ def test_time_selector_schema(schema): {"entity": {}}, {"entity": {"domain": "light"}}, {"entity": {"domain": "binary_sensor", "device_class": "motion"}}, + { + "entity": { + "domain": "binary_sensor", + "device_class": "motion", + "integration": "demo", + } + }, + {"device": {"integration": "demo", "model": "mock-model"}}, + { + "entity": {"domain": "binary_sensor", "device_class": "motion"}, + "device": {"integration": "demo", "model": "mock-model"}, + }, ), ) def test_target_selector_schema(schema): - """Test entity selector.""" + """Test target selector.""" selector.validate_selector({"target": schema}) + + +@pytest.mark.parametrize( + "schema", + ({},), +) +def test_action_selector_schema(schema): + """Test action sequence selector.""" + selector.validate_selector({"action": schema})