2020-11-13 21:53:55 +00:00
|
|
|
"""Test selectors."""
|
|
|
|
import pytest
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.helpers import selector
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2020-11-25 14:10:04 +00:00
|
|
|
"schema",
|
|
|
|
(
|
|
|
|
{"device": None},
|
|
|
|
{"entity": None},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_valid_base_schema(schema):
|
|
|
|
"""Test base schema validation."""
|
|
|
|
selector.validate_selector(schema)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"schema",
|
|
|
|
(
|
|
|
|
{},
|
|
|
|
{"non_existing": {}},
|
|
|
|
# Two keys
|
|
|
|
{"device": {}, "entity": {}},
|
|
|
|
),
|
2020-11-13 21:53:55 +00:00
|
|
|
)
|
|
|
|
def test_invalid_base_schema(schema):
|
|
|
|
"""Test base schema validation."""
|
|
|
|
with pytest.raises(vol.Invalid):
|
|
|
|
selector.validate_selector(schema)
|
|
|
|
|
|
|
|
|
2020-11-19 15:48:43 +00:00
|
|
|
def test_validate_selector():
|
|
|
|
"""Test return is the same as input."""
|
|
|
|
schema = {"device": {"manufacturer": "mock-manuf", "model": "mock-model"}}
|
|
|
|
assert schema == selector.validate_selector(schema)
|
|
|
|
|
|
|
|
|
2020-11-13 21:53:55 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"schema",
|
|
|
|
(
|
|
|
|
{},
|
|
|
|
{"integration": "zha"},
|
|
|
|
{"manufacturer": "mock-manuf"},
|
|
|
|
{"model": "mock-model"},
|
|
|
|
{"manufacturer": "mock-manuf", "model": "mock-model"},
|
|
|
|
{"integration": "zha", "manufacturer": "mock-manuf", "model": "mock-model"},
|
2020-11-26 14:44:25 +00:00
|
|
|
{"entity": {"device_class": "motion"}},
|
|
|
|
{
|
|
|
|
"integration": "zha",
|
|
|
|
"manufacturer": "mock-manuf",
|
|
|
|
"model": "mock-model",
|
|
|
|
"entity": {"domain": "binary_sensor", "device_class": "motion"},
|
|
|
|
},
|
2020-11-13 21:53:55 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_device_selector_schema(schema):
|
|
|
|
"""Test device selector."""
|
|
|
|
selector.validate_selector({"device": schema})
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"schema",
|
|
|
|
(
|
|
|
|
{},
|
|
|
|
{"integration": "zha"},
|
|
|
|
{"domain": "light"},
|
2020-11-26 14:44:25 +00:00
|
|
|
{"device_class": "motion"},
|
2020-11-13 21:53:55 +00:00
|
|
|
{"integration": "zha", "domain": "light"},
|
2020-11-26 14:44:25 +00:00
|
|
|
{"integration": "zha", "domain": "binary_sensor", "device_class": "motion"},
|
2020-11-13 21:53:55 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_entity_selector_schema(schema):
|
2020-11-19 15:48:43 +00:00
|
|
|
"""Test entity selector."""
|
2020-11-13 21:53:55 +00:00
|
|
|
selector.validate_selector({"entity": schema})
|
2020-11-25 19:03:56 +00:00
|
|
|
|
|
|
|
|
2020-11-26 14:44:25 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"schema",
|
2020-12-02 08:30:49 +00:00
|
|
|
(
|
|
|
|
{},
|
|
|
|
{"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"},
|
|
|
|
},
|
|
|
|
),
|
2020-11-26 14:44:25 +00:00
|
|
|
)
|
|
|
|
def test_area_selector_schema(schema):
|
|
|
|
"""Test area selector."""
|
|
|
|
selector.validate_selector({"area": schema})
|
|
|
|
|
|
|
|
|
2020-11-25 19:03:56 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"schema",
|
|
|
|
(
|
|
|
|
{"min": 10, "max": 50},
|
|
|
|
{"min": -100, "max": 100, "step": 5},
|
|
|
|
{"min": -20, "max": -10, "mode": "box"},
|
|
|
|
{"min": 0, "max": 100, "unit_of_measurement": "seconds", "mode": "slider"},
|
|
|
|
{"min": 10, "max": 1000, "mode": "slider", "step": 0.5},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_number_selector_schema(schema):
|
|
|
|
"""Test number selector."""
|
|
|
|
selector.validate_selector({"number": schema})
|
|
|
|
|
|
|
|
|
2021-02-24 16:02:48 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"schema",
|
|
|
|
({},),
|
|
|
|
)
|
|
|
|
def test_addon_selector_schema(schema):
|
|
|
|
"""Test add-on selector."""
|
|
|
|
selector.validate_selector({"addon": schema})
|
|
|
|
|
|
|
|
|
2020-11-25 19:03:56 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"schema",
|
|
|
|
({},),
|
|
|
|
)
|
|
|
|
def test_boolean_selector_schema(schema):
|
|
|
|
"""Test boolean selector."""
|
|
|
|
selector.validate_selector({"boolean": schema})
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"schema",
|
2020-11-26 14:44:25 +00:00
|
|
|
({},),
|
2020-11-25 19:03:56 +00:00
|
|
|
)
|
2020-11-26 14:44:25 +00:00
|
|
|
def test_time_selector_schema(schema):
|
|
|
|
"""Test time selector."""
|
|
|
|
selector.validate_selector({"time": schema})
|
2020-11-28 22:33:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"schema",
|
|
|
|
(
|
|
|
|
{},
|
|
|
|
{"entity": {}},
|
|
|
|
{"entity": {"domain": "light"}},
|
2020-11-30 22:35:24 +00:00
|
|
|
{"entity": {"domain": "binary_sensor", "device_class": "motion"}},
|
2020-12-02 08:30:49 +00:00
|
|
|
{
|
|
|
|
"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"},
|
|
|
|
},
|
2020-11-28 22:33:32 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_target_selector_schema(schema):
|
2020-12-02 08:30:49 +00:00
|
|
|
"""Test target selector."""
|
2020-11-28 22:33:32 +00:00
|
|
|
selector.validate_selector({"target": schema})
|
2020-12-02 08:30:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"schema",
|
|
|
|
({},),
|
|
|
|
)
|
|
|
|
def test_action_selector_schema(schema):
|
|
|
|
"""Test action sequence selector."""
|
|
|
|
selector.validate_selector({"action": schema})
|
2021-01-27 08:20:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"schema",
|
|
|
|
({},),
|
|
|
|
)
|
|
|
|
def test_object_selector_schema(schema):
|
|
|
|
"""Test object selector."""
|
|
|
|
selector.validate_selector({"object": schema})
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"schema",
|
|
|
|
({}, {"multiline": True}, {"multiline": False}),
|
|
|
|
)
|
|
|
|
def test_text_selector_schema(schema):
|
|
|
|
"""Test text selector."""
|
|
|
|
selector.validate_selector({"text": schema})
|
2021-02-08 13:03:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"schema",
|
|
|
|
({"options": ["red", "green", "blue"]},),
|
|
|
|
)
|
|
|
|
def test_select_selector_schema(schema):
|
|
|
|
"""Test select selector."""
|
|
|
|
selector.validate_selector({"select": schema})
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"schema",
|
|
|
|
(
|
|
|
|
{},
|
|
|
|
{"options": {"hello": "World"}},
|
|
|
|
{"options": []},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_select_selector_schema_error(schema):
|
|
|
|
"""Test select selector."""
|
|
|
|
with pytest.raises(vol.Invalid):
|
|
|
|
selector.validate_selector({"select": schema})
|