From da6eca7b68b9894fc8cffcfb4f43600b1880885f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 3 Mar 2024 07:50:04 -1000 Subject: [PATCH] Avoid compiling entity service schema when passed defaults (#112099) * Avoid compiling entity service schema when passed defaults * dry --- homeassistant/helpers/config_validation.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index 59e4f09d26f..7bf760c3136 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -1235,9 +1235,7 @@ TARGET_SERVICE_FIELDS = { } -def make_entity_service_schema( - schema: dict, *, extra: int = vol.PREVENT_EXTRA -) -> vol.Schema: +def _make_entity_service_schema(schema: dict, extra: int) -> vol.Schema: """Create an entity service schema.""" return vol.Schema( vol.All( @@ -1255,6 +1253,21 @@ def make_entity_service_schema( ) +BASE_ENTITY_SCHEMA = _make_entity_service_schema({}, vol.PREVENT_EXTRA) + + +def make_entity_service_schema( + schema: dict, *, extra: int = vol.PREVENT_EXTRA +) -> vol.Schema: + """Create an entity service schema.""" + if not schema and extra == vol.PREVENT_EXTRA: + # If the schema is empty and we don't allow extra keys, we can return + # the base schema and avoid compiling a new schema which is the case + # for ~50% of services. + return BASE_ENTITY_SCHEMA + return _make_entity_service_schema(schema, extra) + + SCRIPT_CONVERSATION_RESPONSE_SCHEMA = vol.Any(template, None)