From aebc95b1d25be3c996e7f6f663ab987145184f26 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 20 Mar 2024 16:07:17 -1000 Subject: [PATCH] Reduce overhead to construct and validate entity service schema (#113920) --- homeassistant/helpers/config_validation.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index cf65da5917c..3f5af582424 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -161,15 +161,15 @@ def path(value: Any) -> str: # https://github.com/alecthomas/voluptuous/issues/115#issuecomment-144464666 def has_at_least_one_key(*keys: Any) -> Callable[[dict], dict]: """Validate that at least one key exists.""" + key_set = set(keys) def validate(obj: dict) -> dict: """Test keys exist in dict.""" if not isinstance(obj, dict): raise vol.Invalid("expected dictionary") - for k in obj: - if k in keys: - return obj + if not key_set.isdisjoint(obj): + return obj expected = ", ".join(str(k) for k in keys) raise vol.Invalid(f"must contain at least one of {expected}.") @@ -1250,6 +1250,9 @@ TARGET_SERVICE_FIELDS = { } +_HAS_ENTITY_SERVICE_FIELD = has_at_least_one_key(*ENTITY_SERVICE_FIELDS) + + def _make_entity_service_schema(schema: dict, extra: int) -> vol.Schema: """Create an entity service schema.""" return vol.Schema( @@ -1263,7 +1266,7 @@ def _make_entity_service_schema(schema: dict, extra: int) -> vol.Schema: }, extra=extra, ), - has_at_least_one_key(*ENTITY_SERVICE_FIELDS), + _HAS_ENTITY_SERVICE_FIELD, ) )