Add check for invalid options with specific platforms (#140082)

This commit is contained in:
Petro31 2025-03-07 18:09:04 -05:00 committed by GitHub
parent 99b5adaef1
commit 293d455cba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 107 additions and 34 deletions

View File

@ -1,5 +1,6 @@
"""Template config validator."""
from collections.abc import Callable
from contextlib import suppress
import logging
@ -52,7 +53,27 @@ from .helpers import async_get_blueprints
PACKAGE_MERGE_HINT = "list"
def ensure_domains_do_not_have_trigger_or_action(*keys: str) -> Callable[[dict], dict]:
"""Validate that config does not contain trigger and action."""
domains = set(keys)
def validate(obj: dict):
options = set(obj.keys())
if found_domains := domains.intersection(options):
invalid = {CONF_TRIGGER, CONF_ACTION}
if found_invalid := invalid.intersection(set(obj.keys())):
raise vol.Invalid(
f"Unsupported option(s) found for domain {found_domains.pop()}, please remove ({', '.join(found_invalid)}) from your configuration",
)
return obj
return validate
CONFIG_SECTION_SCHEMA = vol.Schema(
vol.All(
{
vol.Optional(CONF_UNIQUE_ID): cv.string,
vol.Optional(CONF_TRIGGER): cv.TRIGGER_SCHEMA,
@ -87,6 +108,8 @@ CONFIG_SECTION_SCHEMA = vol.Schema(
cv.ensure_list, [weather_platform.WEATHER_SCHEMA]
),
},
ensure_domains_do_not_have_trigger_or_action(BUTTON_DOMAIN),
)
)
TEMPLATE_BLUEPRINT_INSTANCE_SCHEMA = vol.Schema(

View File

@ -0,0 +1,50 @@
"""Test Template config."""
from __future__ import annotations
import pytest
import voluptuous as vol
from homeassistant.components.template.config import CONFIG_SECTION_SCHEMA
from homeassistant.core import HomeAssistant
@pytest.mark.parametrize(
"config",
[
{
"trigger": {"trigger": "event", "event_type": "my_event"},
"button": {
"press": {
"service": "test.automation",
"data_template": {"caller": "{{ this.entity_id }}"},
},
"device_class": "restart",
"unique_id": "test",
"name": "test",
"icon": "mdi:test",
},
},
{
"trigger": {"trigger": "event", "event_type": "my_event"},
"action": {
"service": "test.automation",
"data_template": {"caller": "{{ this.entity_id }}"},
},
"button": {
"press": {
"service": "test.automation",
"data_template": {"caller": "{{ this.entity_id }}"},
},
"device_class": "restart",
"unique_id": "test",
"name": "test",
"icon": "mdi:test",
},
},
],
)
async def test_invalid_schema(hass: HomeAssistant, config: dict) -> None:
"""Test invalid config schemas."""
with pytest.raises(vol.Invalid):
CONFIG_SECTION_SCHEMA(config)