From 9afbbbf3fecc38bf23810d0682ab71e56c822c09 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen <paulus@paulusschoutsen.nl> Date: Sun, 30 Apr 2017 10:55:03 -0700 Subject: [PATCH] Remove ordered_dict validator (#7375) * Remove ordered_dict validator * Lint * Update test_config_validation.py --- homeassistant/components/group.py | 2 +- .../components/media_player/dunehd.py | 2 +- homeassistant/components/rss_feed_template.py | 6 +- homeassistant/components/zwave/__init__.py | 2 +- homeassistant/config.py | 4 +- homeassistant/helpers/config_validation.py | 24 -------- tests/helpers/test_config_validation.py | 58 ------------------- 7 files changed, 8 insertions(+), 90 deletions(-) diff --git a/homeassistant/components/group.py b/homeassistant/components/group.py index 5dde5d6fea8..41c3f7e269b 100644 --- a/homeassistant/components/group.py +++ b/homeassistant/components/group.py @@ -64,7 +64,7 @@ GROUP_SCHEMA = vol.Schema({ }) CONFIG_SCHEMA = vol.Schema({ - DOMAIN: cv.ordered_dict(vol.All(_conf_preprocess, GROUP_SCHEMA)) + DOMAIN: vol.Schema({cv.match_all: vol.All(_conf_preprocess, GROUP_SCHEMA)}) }, extra=vol.ALLOW_EXTRA) # List of ON/OFF state tuples for groupable states diff --git a/homeassistant/components/media_player/dunehd.py b/homeassistant/components/media_player/dunehd.py index 1facb523da6..4ff1d57c558 100644 --- a/homeassistant/components/media_player/dunehd.py +++ b/homeassistant/components/media_player/dunehd.py @@ -22,7 +22,7 @@ CONF_SOURCES = 'sources' PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_HOST): cv.string, - vol.Optional(CONF_SOURCES): cv.ordered_dict(cv.string, cv.string), + vol.Optional(CONF_SOURCES): vol.Schema({cv.string: cv.string}), vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, }) diff --git a/homeassistant/components/rss_feed_template.py b/homeassistant/components/rss_feed_template.py index e6af34a9b5e..1441a98c0a8 100644 --- a/homeassistant/components/rss_feed_template.py +++ b/homeassistant/components/rss_feed_template.py @@ -20,8 +20,8 @@ DEPENDENCIES = ['http'] DOMAIN = 'rss_feed_template' CONFIG_SCHEMA = vol.Schema({ - DOMAIN: cv.ordered_dict( - vol.Schema({ + DOMAIN: vol.Schema({ + cv.match_all: vol.Schema({ vol.Optional('requires_api_password', default=True): cv.boolean, vol.Optional('title'): cv.template, vol.Required('items'): vol.All( @@ -32,7 +32,7 @@ CONFIG_SCHEMA = vol.Schema({ }] ) }) - ) + }) }, extra=vol.ALLOW_EXTRA) diff --git a/homeassistant/components/zwave/__init__.py b/homeassistant/components/zwave/__init__.py index 0a32a664dc3..bf7eaa87e68 100755 --- a/homeassistant/components/zwave/__init__.py +++ b/homeassistant/components/zwave/__init__.py @@ -125,7 +125,7 @@ CONFIG_SCHEMA = vol.Schema({ vol.Optional(CONF_DEVICE_CONFIG, default={}): vol.Schema({cv.entity_id: DEVICE_CONFIG_SCHEMA_ENTRY}), vol.Optional(CONF_DEVICE_CONFIG_GLOB, default={}): - cv.ordered_dict(DEVICE_CONFIG_SCHEMA_ENTRY, cv.string), + vol.Schema({cv.string: DEVICE_CONFIG_SCHEMA_ENTRY}), vol.Optional(CONF_DEVICE_CONFIG_DOMAIN, default={}): vol.Schema({cv.string: DEVICE_CONFIG_SCHEMA_ENTRY}), vol.Optional(CONF_DEBUG, default=DEFAULT_DEBUG): cv.boolean, diff --git a/homeassistant/config.py b/homeassistant/config.py index 3d1aee6c4f0..39a6d3304ac 100644 --- a/homeassistant/config.py +++ b/homeassistant/config.py @@ -112,7 +112,7 @@ CUSTOMIZE_CONFIG_SCHEMA = vol.Schema({ vol.Optional(CONF_CUSTOMIZE_DOMAIN, default={}): vol.Schema({cv.string: dict}), vol.Optional(CONF_CUSTOMIZE_GLOB, default={}): - cv.ordered_dict(OrderedDict, cv.string), + vol.Schema({cv.string: OrderedDict}), }) CORE_CONFIG_SCHEMA = CUSTOMIZE_CONFIG_SCHEMA.extend({ @@ -454,7 +454,7 @@ def _identify_config_schema(module): except (AttributeError, KeyError): return (None, None) t_schema = str(schema) - if t_schema.startswith(('{', '<function ordered_dict')): + if t_schema.startswith('{'): return ('dict', schema) if t_schema.startswith(('[', 'All(<function ensure_list')): return ('list', schema) diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index a3011ceb318..32eeeaff5ab 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -1,5 +1,4 @@ """Helpers for config validation using voluptuous.""" -from collections import OrderedDict from datetime import timedelta, datetime as datetime_sys import os import re @@ -373,29 +372,6 @@ def x10_address(value): return str(value).lower() -def ordered_dict(value_validator, key_validator=match_all): - """Validate an ordered dict validator that maintains ordering. - - value_validator will be applied to each value of the dictionary. - key_validator (optional) will be applied to each key of the dictionary. - """ - item_validator = vol.Schema({key_validator: value_validator}) - - def validator(value): - """Validate ordered dict.""" - config = OrderedDict() - - if not isinstance(value, dict): - raise vol.Invalid('Value {} is not a dictionary'.format(value)) - for key, val in value.items(): - v_res = item_validator({key: val}) - config.update(v_res) - - return config - - return validator - - def ensure_list_csv(value: Any) -> Sequence: """Ensure that input is a list or make one from comma-separated string.""" if isinstance(value, str): diff --git a/tests/helpers/test_config_validation.py b/tests/helpers/test_config_validation.py index 7255447cd49..ac652e29833 100644 --- a/tests/helpers/test_config_validation.py +++ b/tests/helpers/test_config_validation.py @@ -1,5 +1,4 @@ """Test config validators.""" -from collections import OrderedDict from datetime import timedelta, datetime, date import enum import os @@ -448,63 +447,6 @@ def test_has_at_least_one_key(): schema(value) -def test_ordered_dict_only_dict(): - """Test ordered_dict validator.""" - schema = vol.Schema(cv.ordered_dict(cv.match_all, cv.match_all)) - - for value in (None, [], 100, 'hello'): - with pytest.raises(vol.MultipleInvalid): - schema(value) - - -def test_ordered_dict_order(): - """Test ordered_dict validator.""" - schema = vol.Schema(cv.ordered_dict(int, cv.string)) - - val = OrderedDict() - val['first'] = 1 - val['second'] = 2 - - validated = schema(val) - - assert isinstance(validated, OrderedDict) - assert ['first', 'second'] == list(validated.keys()) - - -def test_ordered_dict_key_validator(): - """Test ordered_dict key validator.""" - schema = vol.Schema(cv.ordered_dict(cv.match_all, cv.string)) - - with pytest.raises(vol.Invalid): - schema({None: 1}) - - schema({'hello': 'world'}) - - schema = vol.Schema(cv.ordered_dict(cv.match_all, int)) - - with pytest.raises(vol.Invalid): - schema({'hello': 1}) - - schema({1: 'works'}) - - -def test_ordered_dict_value_validator(): # pylint: disable=invalid-name - """Test ordered_dict validator.""" - schema = vol.Schema(cv.ordered_dict(cv.string)) - - with pytest.raises(vol.Invalid): - schema({'hello': None}) - - schema({'hello': 'world'}) - - schema = vol.Schema(cv.ordered_dict(int)) - - with pytest.raises(vol.Invalid): - schema({'hello': 'world'}) - - schema({'hello': 5}) - - def test_enum(): """Test enum validator.""" class TestEnum(enum.Enum): -- GitLab