mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Align selectors with frontend updates (#67906)
* Align selectors with frontend updates * Drop metadata from MediaSelector selection * Adjust blueprint tests * Address review comments * Add tests for new selectors * Don't stringify input * Require min+max for number selector in slider mode * vol.Schema does not like static methods * Tweak
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
"""Test selectors."""
|
||||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
@@ -206,6 +208,7 @@ def test_area_selector_schema(schema, valid_selections, invalid_selections):
|
||||
(),
|
||||
),
|
||||
({"min": 10, "max": 1000, "mode": "slider", "step": 0.5}, (), ()),
|
||||
({"mode": "box"}, (10,), ()),
|
||||
),
|
||||
)
|
||||
def test_number_selector_schema(schema, valid_selections, invalid_selections):
|
||||
@@ -213,6 +216,19 @@ def test_number_selector_schema(schema, valid_selections, invalid_selections):
|
||||
_test_selector("number", schema, valid_selections, invalid_selections)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"schema",
|
||||
(
|
||||
{}, # Must have mandatory fields
|
||||
{"mode": "slider"}, # Must have min+max in slider mode
|
||||
),
|
||||
)
|
||||
def test_number_selector_schema_error(schema):
|
||||
"""Test select selector."""
|
||||
with pytest.raises(vol.Invalid):
|
||||
selector.validate_selector({"number": schema})
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"schema,valid_selections,invalid_selections",
|
||||
(({}, ("abc123",), (None,)),),
|
||||
@@ -315,6 +331,16 @@ def test_text_selector_schema(schema, valid_selections, invalid_selections):
|
||||
("red", "green", "blue"),
|
||||
("cat", 0, None),
|
||||
),
|
||||
(
|
||||
{
|
||||
"options": [
|
||||
{"value": "red", "label": "Ruby Red"},
|
||||
{"value": "green", "label": "Emerald Green"},
|
||||
]
|
||||
},
|
||||
("red", "green"),
|
||||
("cat", 0, None),
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_select_selector_schema(schema, valid_selections, invalid_selections):
|
||||
@@ -325,12 +351,148 @@ def test_select_selector_schema(schema, valid_selections, invalid_selections):
|
||||
@pytest.mark.parametrize(
|
||||
"schema",
|
||||
(
|
||||
{},
|
||||
{"options": {"hello": "World"}},
|
||||
{"options": []},
|
||||
{}, # Must have options
|
||||
{"options": {"hello": "World"}}, # Options must be a list
|
||||
{"options": []}, # Must have at least option
|
||||
# Options must be strings or value / label pairs
|
||||
{"options": [{"hello": "World"}]},
|
||||
# Options must all be of the same type
|
||||
{"options": ["red", {"value": "green", "label": "Emerald Green"}]},
|
||||
),
|
||||
)
|
||||
def test_select_selector_schema_error(schema):
|
||||
"""Test select selector."""
|
||||
with pytest.raises(vol.Invalid):
|
||||
selector.validate_selector({"select": schema})
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"schema,valid_selections,invalid_selections",
|
||||
(
|
||||
(
|
||||
{"entity_id": "sensor.abc"},
|
||||
("friendly_name", "device_class"),
|
||||
(None,),
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_attribute_selector_schema(schema, valid_selections, invalid_selections):
|
||||
"""Test attribute selector."""
|
||||
_test_selector("attribute", schema, valid_selections, invalid_selections)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"schema,valid_selections,invalid_selections",
|
||||
(
|
||||
(
|
||||
{},
|
||||
({"seconds": 10},),
|
||||
(None, {}),
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_duration_selector_schema(schema, valid_selections, invalid_selections):
|
||||
"""Test duration selector."""
|
||||
_test_selector(
|
||||
"duration",
|
||||
schema,
|
||||
valid_selections,
|
||||
invalid_selections,
|
||||
lambda x: timedelta(**x),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"schema,valid_selections,invalid_selections",
|
||||
(
|
||||
(
|
||||
{},
|
||||
("mdi:abc",),
|
||||
(None,),
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_icon_selector_schema(schema, valid_selections, invalid_selections):
|
||||
"""Test icon selector."""
|
||||
_test_selector("icon", schema, valid_selections, invalid_selections)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"schema,valid_selections,invalid_selections",
|
||||
(
|
||||
(
|
||||
{},
|
||||
("abc",),
|
||||
(None,),
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_theme_selector_schema(schema, valid_selections, invalid_selections):
|
||||
"""Test theme selector."""
|
||||
_test_selector("theme", schema, valid_selections, invalid_selections)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"schema,valid_selections,invalid_selections",
|
||||
(
|
||||
(
|
||||
{},
|
||||
(
|
||||
{
|
||||
"entity_id": "sensor.abc",
|
||||
"media_content_id": "abc",
|
||||
"media_content_type": "def",
|
||||
},
|
||||
{
|
||||
"entity_id": "sensor.abc",
|
||||
"media_content_id": "abc",
|
||||
"media_content_type": "def",
|
||||
"metadata": {},
|
||||
},
|
||||
),
|
||||
(None, "abc", {}),
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_media_selector_schema(schema, valid_selections, invalid_selections):
|
||||
"""Test media selector."""
|
||||
|
||||
def drop_metadata(data):
|
||||
"""Drop metadata key from the input."""
|
||||
data.pop("metadata", None)
|
||||
return data
|
||||
|
||||
_test_selector("media", schema, valid_selections, invalid_selections, drop_metadata)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"schema,valid_selections,invalid_selections",
|
||||
(
|
||||
(
|
||||
{},
|
||||
(
|
||||
{
|
||||
"latitude": 1.0,
|
||||
"longitude": 2.0,
|
||||
},
|
||||
{
|
||||
"latitude": 1.0,
|
||||
"longitude": 2.0,
|
||||
"radius": 3.0,
|
||||
},
|
||||
),
|
||||
(
|
||||
None,
|
||||
"abc",
|
||||
{},
|
||||
{"latitude": 1.0},
|
||||
{"longitude": 1.0},
|
||||
{"latitude": 1.0, "longitude": "1.0"},
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_location_selector_schema(schema, valid_selections, invalid_selections):
|
||||
"""Test location selector."""
|
||||
|
||||
_test_selector("location", schema, valid_selections, invalid_selections)
|
||||
|
||||
Reference in New Issue
Block a user