mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 04:37:06 +00:00
Improve entity domain validator (#43164)
This commit is contained in:
parent
87d86026ed
commit
0a717f313e
@ -286,9 +286,12 @@ def entity_domain(domain: Union[str, List[str]]) -> Callable[[Any], str]:
|
|||||||
"""Validate that entity belong to domain."""
|
"""Validate that entity belong to domain."""
|
||||||
ent_domain = entities_domain(domain)
|
ent_domain = entities_domain(domain)
|
||||||
|
|
||||||
def validate(value: Any) -> str:
|
def validate(value: str) -> str:
|
||||||
"""Test if entity domain is domain."""
|
"""Test if entity domain is domain."""
|
||||||
return ent_domain(value)[0]
|
validated = ent_domain(value)
|
||||||
|
if len(validated) != 1:
|
||||||
|
raise vol.Invalid(f"Expected exactly 1 entity, got {len(validated)}")
|
||||||
|
return validated[0]
|
||||||
|
|
||||||
return validate
|
return validate
|
||||||
|
|
||||||
|
@ -179,7 +179,12 @@ def test_entity_domain():
|
|||||||
"""Test entity domain validation."""
|
"""Test entity domain validation."""
|
||||||
schema = vol.Schema(cv.entity_domain("sensor"))
|
schema = vol.Schema(cv.entity_domain("sensor"))
|
||||||
|
|
||||||
for value in ("invalid_entity", "cover.demo"):
|
for value in (
|
||||||
|
"invalid_entity",
|
||||||
|
"cover.demo",
|
||||||
|
"cover.demo,sensor.another_entity",
|
||||||
|
"",
|
||||||
|
):
|
||||||
with pytest.raises(vol.MultipleInvalid):
|
with pytest.raises(vol.MultipleInvalid):
|
||||||
schema(value)
|
schema(value)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user