mirror of
https://github.com/elisspace/core.git
synced 2026-10-02 07:19:59 +00:00
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:
@@ -43,7 +43,7 @@ DEFAULT_TIMEOUT = 10
|
||||
DEFAULT_CONFIDENCE = 80
|
||||
|
||||
SOURCE_SCHEMA = vol.Schema({
|
||||
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
||||
vol.Required(CONF_ENTITY_ID): cv.entity_domain('camera'),
|
||||
vol.Optional(CONF_NAME): cv.string,
|
||||
})
|
||||
|
||||
|
||||
@@ -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__)
|
||||
|
||||
Reference in New Issue
Block a user