mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Reduce overhead to construct and validate entity service schema (#113920)
This commit is contained in:
parent
e015fd2440
commit
aebc95b1d2
@ -161,15 +161,15 @@ def path(value: Any) -> str:
|
|||||||
# https://github.com/alecthomas/voluptuous/issues/115#issuecomment-144464666
|
# https://github.com/alecthomas/voluptuous/issues/115#issuecomment-144464666
|
||||||
def has_at_least_one_key(*keys: Any) -> Callable[[dict], dict]:
|
def has_at_least_one_key(*keys: Any) -> Callable[[dict], dict]:
|
||||||
"""Validate that at least one key exists."""
|
"""Validate that at least one key exists."""
|
||||||
|
key_set = set(keys)
|
||||||
|
|
||||||
def validate(obj: dict) -> dict:
|
def validate(obj: dict) -> dict:
|
||||||
"""Test keys exist in dict."""
|
"""Test keys exist in dict."""
|
||||||
if not isinstance(obj, dict):
|
if not isinstance(obj, dict):
|
||||||
raise vol.Invalid("expected dictionary")
|
raise vol.Invalid("expected dictionary")
|
||||||
|
|
||||||
for k in obj:
|
if not key_set.isdisjoint(obj):
|
||||||
if k in keys:
|
return obj
|
||||||
return obj
|
|
||||||
expected = ", ".join(str(k) for k in keys)
|
expected = ", ".join(str(k) for k in keys)
|
||||||
raise vol.Invalid(f"must contain at least one of {expected}.")
|
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:
|
def _make_entity_service_schema(schema: dict, extra: int) -> vol.Schema:
|
||||||
"""Create an entity service schema."""
|
"""Create an entity service schema."""
|
||||||
return vol.Schema(
|
return vol.Schema(
|
||||||
@ -1263,7 +1266,7 @@ def _make_entity_service_schema(schema: dict, extra: int) -> vol.Schema:
|
|||||||
},
|
},
|
||||||
extra=extra,
|
extra=extra,
|
||||||
),
|
),
|
||||||
has_at_least_one_key(*ENTITY_SERVICE_FIELDS),
|
_HAS_ENTITY_SERVICE_FIELD,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user