mirror of
https://github.com/home-assistant/core.git
synced 2025-09-03 03:41:40 +02:00
Add TARGET_FIELDS to config validation (#150238)
This commit is contained in:
@@ -373,6 +373,17 @@ def entity_id(value: Any) -> str:
|
|||||||
raise vol.Invalid(f"Entity ID {value} is an invalid entity ID")
|
raise vol.Invalid(f"Entity ID {value} is an invalid entity ID")
|
||||||
|
|
||||||
|
|
||||||
|
def strict_entity_id(value: Any) -> str:
|
||||||
|
"""Validate Entity ID, strictly."""
|
||||||
|
if not isinstance(value, str):
|
||||||
|
raise vol.Invalid(f"Entity ID {value} is not a string")
|
||||||
|
|
||||||
|
if valid_entity_id(value):
|
||||||
|
return value
|
||||||
|
|
||||||
|
raise vol.Invalid(f"Entity ID {value} is not a valid entity ID")
|
||||||
|
|
||||||
|
|
||||||
def entity_id_or_uuid(value: Any) -> str:
|
def entity_id_or_uuid(value: Any) -> str:
|
||||||
"""Validate Entity specified by entity_id or uuid."""
|
"""Validate Entity specified by entity_id or uuid."""
|
||||||
with contextlib.suppress(vol.Invalid):
|
with contextlib.suppress(vol.Invalid):
|
||||||
@@ -1292,6 +1303,15 @@ PLATFORM_SCHEMA = vol.Schema(
|
|||||||
|
|
||||||
PLATFORM_SCHEMA_BASE = PLATFORM_SCHEMA.extend({}, extra=vol.ALLOW_EXTRA)
|
PLATFORM_SCHEMA_BASE = PLATFORM_SCHEMA.extend({}, extra=vol.ALLOW_EXTRA)
|
||||||
|
|
||||||
|
|
||||||
|
TARGET_FIELDS: VolDictType = {
|
||||||
|
vol.Optional(ATTR_ENTITY_ID): vol.All(ensure_list, [strict_entity_id]),
|
||||||
|
vol.Optional(ATTR_DEVICE_ID): vol.All(ensure_list, [str]),
|
||||||
|
vol.Optional(ATTR_AREA_ID): vol.All(ensure_list, [str]),
|
||||||
|
vol.Optional(ATTR_FLOOR_ID): vol.All(ensure_list, [str]),
|
||||||
|
vol.Optional(ATTR_LABEL_ID): vol.All(ensure_list, [str]),
|
||||||
|
}
|
||||||
|
|
||||||
ENTITY_SERVICE_FIELDS: VolDictType = {
|
ENTITY_SERVICE_FIELDS: VolDictType = {
|
||||||
# Either accept static entity IDs, a single dynamic template or a mixed list
|
# Either accept static entity IDs, a single dynamic template or a mixed list
|
||||||
# of static and dynamic templates. While this could be solved with a single
|
# of static and dynamic templates. While this could be solved with a single
|
||||||
|
@@ -828,7 +828,7 @@ def selector_serializer(schema: Any) -> Any: # noqa: C901
|
|||||||
return {"type": "string", "enum": options}
|
return {"type": "string", "enum": options}
|
||||||
|
|
||||||
if isinstance(schema, selector.TargetSelector):
|
if isinstance(schema, selector.TargetSelector):
|
||||||
return convert(cv.TARGET_SERVICE_FIELDS)
|
return convert(cv.TARGET_FIELDS)
|
||||||
|
|
||||||
if isinstance(schema, selector.TemplateSelector):
|
if isinstance(schema, selector.TemplateSelector):
|
||||||
return {"type": "string", "format": "jinja2"}
|
return {"type": "string", "format": "jinja2"}
|
||||||
|
@@ -1216,43 +1216,14 @@ async def test_selector_serializer(
|
|||||||
selector.StateSelector({"entity_id": "sensor.test"})
|
selector.StateSelector({"entity_id": "sensor.test"})
|
||||||
) == {"type": "string"}
|
) == {"type": "string"}
|
||||||
target_schema = selector_serializer(selector.TargetSelector())
|
target_schema = selector_serializer(selector.TargetSelector())
|
||||||
target_schema["properties"]["entity_id"]["anyOf"][0][
|
|
||||||
"enum"
|
|
||||||
].sort() # Order is not deterministic
|
|
||||||
assert target_schema == {
|
assert target_schema == {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"area_id": {
|
"area_id": {"items": {"type": "string"}, "type": "array"},
|
||||||
"anyOf": [
|
"device_id": {"items": {"type": "string"}, "type": "array"},
|
||||||
{"type": "string", "enum": ["none"]},
|
"entity_id": {"items": {"type": "string"}, "type": "array"},
|
||||||
{"type": "array", "items": {"type": "string", "nullable": True}},
|
"floor_id": {"items": {"type": "string"}, "type": "array"},
|
||||||
]
|
"label_id": {"items": {"type": "string"}, "type": "array"},
|
||||||
},
|
|
||||||
"device_id": {
|
|
||||||
"anyOf": [
|
|
||||||
{"type": "string", "enum": ["none"]},
|
|
||||||
{"type": "array", "items": {"type": "string", "nullable": True}},
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"entity_id": {
|
|
||||||
"anyOf": [
|
|
||||||
{"type": "string", "enum": ["all", "none"], "format": "lower"},
|
|
||||||
{"type": "string", "nullable": True},
|
|
||||||
{"type": "array", "items": {"type": "string"}},
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"floor_id": {
|
|
||||||
"anyOf": [
|
|
||||||
{"type": "string", "enum": ["none"]},
|
|
||||||
{"type": "array", "items": {"type": "string", "nullable": True}},
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"label_id": {
|
|
||||||
"anyOf": [
|
|
||||||
{"type": "string", "enum": ["none"]},
|
|
||||||
{"type": "array", "items": {"type": "string", "nullable": True}},
|
|
||||||
]
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
"required": [],
|
"required": [],
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user