Improve trigger schema validation to ask for trigger
instead of platform
(#126750)
* Add check for missing trigger * Fix * Fix * Escape
This commit is contained in:
parent
f4c339db8c
commit
3810c3cbaf
3 changed files with 15 additions and 9 deletions
|
@ -484,7 +484,7 @@ async def websocket_device_automation_get_condition_capabilities(
|
||||||
# The frontend responds with `trigger` as key, while the
|
# The frontend responds with `trigger` as key, while the
|
||||||
# `DEVICE_TRIGGER_BASE_SCHEMA` expects `platform1` as key.
|
# `DEVICE_TRIGGER_BASE_SCHEMA` expects `platform1` as key.
|
||||||
vol.Required("trigger"): vol.All(
|
vol.Required("trigger"): vol.All(
|
||||||
cv._backward_compat_trigger_schema, # noqa: SLF001
|
cv._trigger_pre_validator, # noqa: SLF001
|
||||||
DEVICE_TRIGGER_BASE_SCHEMA.extend({}, extra=vol.ALLOW_EXTRA),
|
DEVICE_TRIGGER_BASE_SCHEMA.extend({}, extra=vol.ALLOW_EXTRA),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
|
@ -1771,7 +1771,7 @@ CONDITION_ACTION_SCHEMA: vol.Schema = vol.Schema(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _backward_compat_trigger_schema(value: Any | None) -> Any:
|
def _trigger_pre_validator(value: Any | None) -> Any:
|
||||||
"""Rewrite trigger `trigger` to `platform`.
|
"""Rewrite trigger `trigger` to `platform`.
|
||||||
|
|
||||||
`platform` has been renamed to `trigger` in user documentation and in the automation
|
`platform` has been renamed to `trigger` in user documentation and in the automation
|
||||||
|
@ -1790,6 +1790,8 @@ def _backward_compat_trigger_schema(value: Any | None) -> Any:
|
||||||
)
|
)
|
||||||
value = dict(value)
|
value = dict(value)
|
||||||
value[CONF_PLATFORM] = value.pop(CONF_TRIGGER)
|
value[CONF_PLATFORM] = value.pop(CONF_TRIGGER)
|
||||||
|
elif CONF_PLATFORM not in value:
|
||||||
|
raise vol.Invalid("required key not provided", [CONF_TRIGGER])
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
@ -1831,7 +1833,7 @@ def _base_trigger_validator(value: Any) -> Any:
|
||||||
TRIGGER_SCHEMA = vol.All(
|
TRIGGER_SCHEMA = vol.All(
|
||||||
ensure_list,
|
ensure_list,
|
||||||
_base_trigger_list_flatten,
|
_base_trigger_list_flatten,
|
||||||
[vol.All(_backward_compat_trigger_schema, _base_trigger_validator)],
|
[vol.All(_trigger_pre_validator, _base_trigger_validator)],
|
||||||
)
|
)
|
||||||
|
|
||||||
_SCRIPT_DELAY_SCHEMA = vol.Schema(
|
_SCRIPT_DELAY_SCHEMA = vol.Schema(
|
||||||
|
|
|
@ -6,6 +6,7 @@ import enum
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
from socket import _GLOBAL_DEFAULT_TIMEOUT
|
from socket import _GLOBAL_DEFAULT_TIMEOUT
|
||||||
import threading
|
import threading
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
@ -1911,16 +1912,19 @@ async def test_nested_trigger_list_extra() -> None:
|
||||||
async def test_trigger_backwards_compatibility() -> None:
|
async def test_trigger_backwards_compatibility() -> None:
|
||||||
"""Test triggers with backwards compatibility."""
|
"""Test triggers with backwards compatibility."""
|
||||||
|
|
||||||
assert cv._backward_compat_trigger_schema("str") == "str"
|
assert cv._trigger_pre_validator("str") == "str"
|
||||||
assert cv._backward_compat_trigger_schema({"platform": "abc"}) == {
|
assert cv._trigger_pre_validator({"platform": "abc"}) == {"platform": "abc"}
|
||||||
"platform": "abc"
|
assert cv._trigger_pre_validator({"trigger": "abc"}) == {"platform": "abc"}
|
||||||
}
|
|
||||||
assert cv._backward_compat_trigger_schema({"trigger": "abc"}) == {"platform": "abc"}
|
|
||||||
with pytest.raises(
|
with pytest.raises(
|
||||||
vol.Invalid,
|
vol.Invalid,
|
||||||
match="Cannot specify both 'platform' and 'trigger'. Please use 'trigger' only.",
|
match="Cannot specify both 'platform' and 'trigger'. Please use 'trigger' only.",
|
||||||
):
|
):
|
||||||
cv._backward_compat_trigger_schema({"trigger": "abc", "platform": "def"})
|
cv._trigger_pre_validator({"trigger": "abc", "platform": "def"})
|
||||||
|
with pytest.raises(
|
||||||
|
vol.Invalid,
|
||||||
|
match=re.escape("required key not provided @ data['trigger']"),
|
||||||
|
):
|
||||||
|
cv._trigger_pre_validator({})
|
||||||
|
|
||||||
|
|
||||||
async def test_is_entity_service_schema(
|
async def test_is_entity_service_schema(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue