Skip to content

Commit

Permalink
Validate basic customize entries (home-assistant#13478)
Browse files Browse the repository at this point in the history
* Added schema to validate customize dictionary

* Added test
  • Loading branch information
cdce8p authored and balloob committed Mar 27, 2018
1 parent 81cf0da commit 8a0facb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
13 changes: 10 additions & 3 deletions homeassistant/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from voluptuous.humanize import humanize_error

from homeassistant.const import (
ATTR_FRIENDLY_NAME, ATTR_HIDDEN, ATTR_ASSUMED_STATE,
CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME, CONF_PACKAGES, CONF_UNIT_SYSTEM,
CONF_TIME_ZONE, CONF_ELEVATION, CONF_UNIT_SYSTEM_METRIC,
CONF_UNIT_SYSTEM_IMPERIAL, CONF_TEMPERATURE_UNIT, TEMP_CELSIUS,
Expand Down Expand Up @@ -129,13 +130,19 @@
{cv.slug: vol.Any(dict, list, None)}) # Only slugs for component names
})

CUSTOMIZE_DICT_SCHEMA = vol.Schema({
vol.Optional(ATTR_FRIENDLY_NAME): cv.string,
vol.Optional(ATTR_HIDDEN): cv.boolean,
vol.Optional(ATTR_ASSUMED_STATE): cv.boolean,
}, extra=vol.ALLOW_EXTRA)

CUSTOMIZE_CONFIG_SCHEMA = vol.Schema({
vol.Optional(CONF_CUSTOMIZE, default={}):
vol.Schema({cv.entity_id: dict}),
vol.Schema({cv.entity_id: CUSTOMIZE_DICT_SCHEMA}),
vol.Optional(CONF_CUSTOMIZE_DOMAIN, default={}):
vol.Schema({cv.string: dict}),
vol.Schema({cv.string: CUSTOMIZE_DICT_SCHEMA}),
vol.Optional(CONF_CUSTOMIZE_GLOB, default={}):
vol.Schema({cv.string: OrderedDict}),
vol.Schema({cv.string: CUSTOMIZE_DICT_SCHEMA}),
})

CORE_CONFIG_SCHEMA = CUSTOMIZE_CONFIG_SCHEMA.extend({
Expand Down
24 changes: 24 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from homeassistant.core import DOMAIN, HomeAssistantError, Config
import homeassistant.config as config_util
from homeassistant.const import (
ATTR_FRIENDLY_NAME, ATTR_HIDDEN, ATTR_ASSUMED_STATE,
CONF_LATITUDE, CONF_LONGITUDE, CONF_UNIT_SYSTEM, CONF_NAME,
CONF_TIME_ZONE, CONF_ELEVATION, CONF_CUSTOMIZE, __version__,
CONF_UNIT_SYSTEM_METRIC, CONF_UNIT_SYSTEM_IMPERIAL, CONF_TEMPERATURE_UNIT)
Expand Down Expand Up @@ -235,6 +236,29 @@ def test_core_config_schema(self):
},
})

def test_customize_dict_schema(self):
"""Test basic customize config validation."""
values = (
{ATTR_FRIENDLY_NAME: None},
{ATTR_HIDDEN: '2'},
{ATTR_ASSUMED_STATE: '2'},
)

for val in values:
print(val)
with pytest.raises(MultipleInvalid):
config_util.CUSTOMIZE_DICT_SCHEMA(val)

assert config_util.CUSTOMIZE_DICT_SCHEMA({
ATTR_FRIENDLY_NAME: 2,
ATTR_HIDDEN: '1',
ATTR_ASSUMED_STATE: '0',
}) == {
ATTR_FRIENDLY_NAME: '2',
ATTR_HIDDEN: True,
ATTR_ASSUMED_STATE: False
}

def test_customize_glob_is_ordered(self):
"""Test that customize_glob preserves order."""
conf = config_util.CORE_CONFIG_SCHEMA(
Expand Down

0 comments on commit 8a0facb

Please sign in to comment.