mirror of
https://github.com/home-assistant/core.git
synced 2025-07-29 16:17:20 +00:00
Improve condition schema validation
This commit is contained in:
parent
b15c9ad130
commit
c705736739
@ -1758,7 +1758,7 @@ def _base_condition_validator(value: Any) -> Any:
|
||||
vol.Schema(
|
||||
{
|
||||
**CONDITION_BASE_SCHEMA,
|
||||
CONF_CONDITION: vol.NotIn(BUILT_IN_CONDITIONS),
|
||||
CONF_CONDITION: vol.All(str, vol.NotIn(BUILT_IN_CONDITIONS)),
|
||||
},
|
||||
extra=vol.ALLOW_EXTRA,
|
||||
)(value)
|
||||
|
@ -70,11 +70,26 @@ def assert_condition_trace(expected):
|
||||
assert_element(condition_trace[key][index], element, path)
|
||||
|
||||
|
||||
async def test_invalid_condition(hass: HomeAssistant) -> None:
|
||||
"""Test if invalid condition raises."""
|
||||
with pytest.raises(HomeAssistantError):
|
||||
await condition.async_from_config(
|
||||
hass,
|
||||
@pytest.mark.parametrize(
|
||||
("config", "error"),
|
||||
[
|
||||
(
|
||||
{"condition": 123},
|
||||
"Unexpected value for condition: '123'. Expected and, device, not, "
|
||||
"numeric_state, or, state, template, time, trigger, zone",
|
||||
)
|
||||
],
|
||||
)
|
||||
async def test_invalid_condition(hass: HomeAssistant, config: dict, error: str) -> None:
|
||||
"""Test if validating an invalid condition raises."""
|
||||
with pytest.raises(vol.Invalid, match=error):
|
||||
cv.CONDITION_SCHEMA(config)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("config", "error"),
|
||||
[
|
||||
(
|
||||
{
|
||||
"condition": "invalid",
|
||||
"conditions": [
|
||||
@ -85,7 +100,15 @@ async def test_invalid_condition(hass: HomeAssistant) -> None:
|
||||
},
|
||||
],
|
||||
},
|
||||
'Invalid condition "invalid" specified',
|
||||
)
|
||||
],
|
||||
)
|
||||
async def test_unknown_condition(hass: HomeAssistant, config: dict, error: str) -> None:
|
||||
"""Test if creating an unknown condition raises."""
|
||||
config = cv.CONDITION_SCHEMA(config)
|
||||
with pytest.raises(HomeAssistantError, match=error):
|
||||
await condition.async_from_config(hass, config)
|
||||
|
||||
|
||||
async def test_and_condition(hass: HomeAssistant) -> None:
|
||||
|
@ -1462,6 +1462,12 @@ def test_key_value_schemas_with_default() -> None:
|
||||
({"wait_template": "{{ invalid"}, "invalid template"),
|
||||
# The validation error message could be improved to explain that this is not
|
||||
# a valid shorthand template
|
||||
(
|
||||
{"condition": 123},
|
||||
"Unexpected value for condition: '123'. Expected and, device, not, "
|
||||
"numeric_state, or, state, template, time, trigger, zone, a list of "
|
||||
"conditions or a valid template",
|
||||
),
|
||||
(
|
||||
{"condition": "not", "conditions": "not a dynamic template"},
|
||||
"Expected a dictionary",
|
||||
|
Loading…
x
Reference in New Issue
Block a user