Check all services

This commit is contained in:
Erik 2024-10-17 11:25:09 +02:00
parent 8c9bf7e41c
commit 51e409c1c7
2 changed files with 26 additions and 3 deletions

View File

@ -2610,6 +2610,10 @@ class ServiceRegistry:
This method must be run in the event loop. This method must be run in the event loop.
""" """
# pylint: disable-next=import-outside-toplevel
from .helpers import config_validation as cv
cv.raise_on_templated_service(domain, service, schema)
domain = domain.lower() domain = domain.lower()
service = service.lower() service = service.lower()
service_obj = Service( service_obj = Service(

View File

@ -1380,12 +1380,32 @@ def _make_entity_service_schema(schema: dict, extra: int) -> VolSchemaType:
BASE_ENTITY_SCHEMA = _make_entity_service_schema({}, vol.PREVENT_EXTRA) BASE_ENTITY_SCHEMA = _make_entity_service_schema({}, vol.PREVENT_EXTRA)
def _raise_on_templated_service(schema: VolDictType | None) -> None: def _raise_on_templated_service(
domain: str, _service: str, schema: VolDictType | None
) -> None:
if not schema: if not schema:
return return
for key, val in schema.items(): for key, val in schema.items():
if val in (dynamic_template, template, template_complex): if val in (dynamic_template, template, template_complex):
raise ValueError(f"Template in service data is not allowed! {key}") raise ValueError(
f"Template in service data is not allowed! {domain}.{_service}:{key}"
)
def raise_on_templated_service(
domain: str, _service: str, schema: VolDictType | VolSchemaType | None
) -> None:
"""Raise if service schema explicitly allows templates."""
if not schema:
return
if isinstance(schema, dict):
_raise_on_templated_service(domain, _service, schema)
return
if isinstance(schema, (vol.All, vol.Any)):
for val in schema.validators:
raise_on_templated_service(domain, _service, val)
if isinstance(schema, (vol.Schema)):
raise_on_templated_service(domain, _service, schema.schema)
def make_entity_service_schema( def make_entity_service_schema(
@ -1397,7 +1417,6 @@ def make_entity_service_schema(
# the base schema and avoid compiling a new schema which is the case # the base schema and avoid compiling a new schema which is the case
# for ~50% of services. # for ~50% of services.
return BASE_ENTITY_SCHEMA return BASE_ENTITY_SCHEMA
_raise_on_templated_service(schema)
return _make_entity_service_schema(schema or {}, extra) return _make_entity_service_schema(schema or {}, extra)