8000 Added config validator for future group platforms by cdce8p · Pull Request #12592 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Added config validator for future group platforms #12592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/image_processing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
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,
})

Expand Down
25 changes: 24 additions & 1 deletion 8000 homeassistant/helpers/config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
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
Expand Down Expand Up @@ -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__)
Expand Down
50 changes: 50 additions & 0 deletions tests/helpers/test_config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,55 @@ def test_entity_ids():
]


def test_entity_domain():
"""Test entity domain validation."""
schema = vol.Schema(cv.entity_domain('sensor'))

options = (
'invalid_entity',
'cover.demo',
)

for value in options:
with pytest.raises(vol.MultipleInvalid):
print(value)
schema(value)

assert schema('sensor.LIGHT') == 'sensor.light'


def test_entities_domain():
"""Test entities domain validation."""
schema = vol.Schema(cv.entities_domain('sensor'))

options = (
None,
'',
'invalid_entity',
['sensor.light', 'cover.demo'],
['sensor.light', 'sensor_invalid'],
)

for value in options:
with pytest.raises(vol.MultipleInvalid):
schema(value)

options = (
'sensor.light',
['SENSOR.light'],
['sensor.light', 'sensor.demo']
)
for value in options:
schema(value)

assert schema('sensor.LIGHT, sensor.demo ') == [
'sensor.light', 'sensor.demo'
]
assert schema(['sensor.light', 'SENSOR.demo']) == [
'sensor.light', 'sensor.demo'
]


def test_ensure_list_csv():
"""Test ensure_list_csv."""
schema = vol.Schema(cv.ensure_list_csv)
Expand Down Expand Up @@ -453,6 +502,7 @@ def test_deprecated(caplog):
)

deprecated_schema({'venus': True})
# pylint: disable=len-as-condition
assert len(caplog.records) == 0

deprecated_schema({'mars': True})
Expand Down
0