Added config validator for future group platforms (#12592)
* Added cv.EntitiesDoamin(domain) validator * Check if all entities in string or list belong to domain * Added tests * Use factory function and entity_ids * Different error message * Typo * Added entity_domain validator for a single entity_id * Image_processing platform now uses cv.entity_domain for source validation
This commit is contained in:
parent
7d5c1581f1
commit
6e6ae173fd
3 changed files with 75 additions and 2 deletions
|
@ -18,7 +18,7 @@ from homeassistant.const import (
|
|||
CONF_ALIAS, CONF_ENTITY_ID, CONF_VALUE_TEMPLATE, WEEKDAYS,
|
||||
CONF_CONDITION, CONF_BELOW, CONF_ABOVE, CONF_TIMEOUT, SUN_EVENT_SUNSET,
|
||||
SUN_EVENT_SUNRISE, CONF_UNIT_SYSTEM_IMPERIAL, CONF_UNIT_SYSTEM_METRIC)
|
||||
from homeassistant.core import valid_entity_id
|
||||
from homeassistant.core import valid_entity_id, split_entity_id
|
||||
from homeassistant.exceptions import TemplateError
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.util import slugify as util_slugify
|
||||
|
@ -147,6 +147,29 @@ def entity_ids(value: Union[str, Sequence]) -> Sequence[str]:
|
|||
return [entity_id(ent_id) for ent_id in value]
|
||||
|
||||
|
||||
def entity_domain(domain: str):
|
||||
"""Validate that entity belong to domain."""
|
||||
def validate(value: Any) -> str:
|
||||
"""Test if entity domain is domain."""
|
||||
ent_domain = entities_domain(domain)
|
||||
return ent_domain(value)[0]
|
||||
return validate
|
||||
|
||||
|
||||
def entities_domain(domain: str):
|
||||
"""Validate that entities belong to domain."""
|
||||
def validate(values: Union[str, Sequence]) -> Sequence[str]:
|
||||
"""Test if entity domain is domain."""
|
||||
values = entity_ids(values)
|
||||
for ent_id in values:
|
||||
if split_entity_id(ent_id)[0] != domain:
|
||||
raise vol.Invalid(
|
||||
"Entity ID '{}' does not belong to domain '{}'"
|
||||
.format(ent_id, domain))
|
||||
return values
|
||||
return validate
|
||||
|
||||
|
||||
def enum(enumClass):
|
||||
"""Create validator for specified enum."""
|
||||
return vol.All(vol.In(enumClass.__members__), enumClass.__getitem__)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue