mirror of
https://github.com/home-assistant/core.git
synced 2025-07-30 00:27:19 +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(
|
vol.Schema(
|
||||||
{
|
{
|
||||||
**CONDITION_BASE_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,
|
extra=vol.ALLOW_EXTRA,
|
||||||
)(value)
|
)(value)
|
||||||
|
@ -70,11 +70,26 @@ def assert_condition_trace(expected):
|
|||||||
assert_element(condition_trace[key][index], element, path)
|
assert_element(condition_trace[key][index], element, path)
|
||||||
|
|
||||||
|
|
||||||
async def test_invalid_condition(hass: HomeAssistant) -> None:
|
@pytest.mark.parametrize(
|
||||||
"""Test if invalid condition raises."""
|
("config", "error"),
|
||||||
with pytest.raises(HomeAssistantError):
|
[
|
||||||
await condition.async_from_config(
|
(
|
||||||
hass,
|
{"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",
|
"condition": "invalid",
|
||||||
"conditions": [
|
"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:
|
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"),
|
({"wait_template": "{{ invalid"}, "invalid template"),
|
||||||
# The validation error message could be improved to explain that this is not
|
# The validation error message could be improved to explain that this is not
|
||||||
# a valid shorthand template
|
# 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"},
|
{"condition": "not", "conditions": "not a dynamic template"},
|
||||||
"Expected a dictionary",
|
"Expected a dictionary",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user