mirror of
https://github.com/home-assistant/core.git
synced 2025-07-29 18:28:14 +02:00
Improve validation of entity service schemas (#124102)
* Improve validation of entity service schemas * Update tests/helpers/test_entity_platform.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
@ -1305,9 +1305,28 @@ TARGET_SERVICE_FIELDS = {
|
||||
_HAS_ENTITY_SERVICE_FIELD = has_at_least_one_key(*ENTITY_SERVICE_FIELDS)
|
||||
|
||||
|
||||
def is_entity_service_schema(validator: VolSchemaType) -> bool:
|
||||
"""Check if the passed validator is an entity schema validator.
|
||||
|
||||
The validator must be either of:
|
||||
- A validator returned by cv._make_entity_service_schema
|
||||
- A validator returned by cv._make_entity_service_schema, wrapped in a vol.Schema
|
||||
- A validator returned by cv._make_entity_service_schema, wrapped in a vol.All
|
||||
Nesting is allowed.
|
||||
"""
|
||||
if hasattr(validator, "_entity_service_schema"):
|
||||
return True
|
||||
if isinstance(validator, (vol.All)):
|
||||
return any(is_entity_service_schema(val) for val in validator.validators)
|
||||
if isinstance(validator, (vol.Schema)):
|
||||
return is_entity_service_schema(validator.schema)
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def _make_entity_service_schema(schema: dict, extra: int) -> VolSchemaType:
|
||||
"""Create an entity service schema."""
|
||||
return vol.All(
|
||||
validator = vol.All(
|
||||
vol.Schema(
|
||||
{
|
||||
# The frontend stores data here. Don't use in core.
|
||||
@ -1319,6 +1338,8 @@ def _make_entity_service_schema(schema: dict, extra: int) -> VolSchemaType:
|
||||
),
|
||||
_HAS_ENTITY_SERVICE_FIELD,
|
||||
)
|
||||
setattr(validator, "_entity_service_schema", True)
|
||||
return validator
|
||||
|
||||
|
||||
BASE_ENTITY_SCHEMA = _make_entity_service_schema({}, vol.PREVENT_EXTRA)
|
||||
|
Reference in New Issue
Block a user