Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve trigger schema validation to ask for trigger instead of platform #126750

Merged
merged 4 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/device_automation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ async def websocket_device_automation_get_condition_capabilities(
# The frontend responds with `trigger` as key, while the
# `DEVICE_TRIGGER_BASE_SCHEMA` expects `platform1` as key.
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),
),
}
Expand Down
6 changes: 4 additions & 2 deletions homeassistant/helpers/config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,7 @@ def STATE_CONDITION_SCHEMA(value: Any) -> dict[str, Any]:
)


def _backward_compat_trigger_schema(value: Any | None) -> Any:
def _trigger_pre_validator(value: Any | None) -> Any:
"""Rewrite trigger `trigger` to `platform`.

`platform` has been renamed to `trigger` in user documentation and in the automation
Expand All @@ -1790,6 +1790,8 @@ def _backward_compat_trigger_schema(value: Any | None) -> Any:
)
value = dict(value)
value[CONF_PLATFORM] = value.pop(CONF_TRIGGER)
elif CONF_PLATFORM not in value:
raise vol.Invalid("required key not provided", [CONF_TRIGGER])

return value

Expand Down Expand Up @@ -1831,7 +1833,7 @@ def _base_trigger_validator(value: Any) -> Any:
TRIGGER_SCHEMA = vol.All(
ensure_list,
_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(
Expand Down
16 changes: 10 additions & 6 deletions tests/helpers/test_config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from functools import partial
import logging
import os
import re
from socket import _GLOBAL_DEFAULT_TIMEOUT
import threading
from typing import Any
Expand Down Expand Up @@ -1911,16 +1912,19 @@ async def test_nested_trigger_list_extra() -> None:
async def test_trigger_backwards_compatibility() -> None:
"""Test triggers with backwards compatibility."""

assert cv._backward_compat_trigger_schema("str") == "str"
assert cv._backward_compat_trigger_schema({"platform": "abc"}) == {
"platform": "abc"
}
assert cv._backward_compat_trigger_schema({"trigger": "abc"}) == {"platform": "abc"}
assert cv._trigger_pre_validator("str") == "str"
assert cv._trigger_pre_validator({"platform": "abc"}) == {"platform": "abc"}
assert cv._trigger_pre_validator({"trigger": "abc"}) == {"platform": "abc"}
with pytest.raises(
vol.Invalid,
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(
Expand Down