Improve entity domain validator (#43164)
This commit is contained in:
parent
87d86026ed
commit
0a717f313e
2 changed files with 11 additions and 3 deletions
|
@ -286,9 +286,12 @@ def entity_domain(domain: Union[str, List[str]]) -> Callable[[Any], str]:
|
|||
"""Validate that entity belong to domain."""
|
||||
ent_domain = entities_domain(domain)
|
||||
|
||||
def validate(value: Any) -> str:
|
||||
def validate(value: str) -> str:
|
||||
"""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
|
||||
|
||||
|
|
|
@ -179,7 +179,12 @@ def test_entity_domain():
|
|||
"""Test entity domain validation."""
|
||||
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):
|
||||
schema(value)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue