From c3c5cc9ae7750578613e5eb52967a7bdaa5a5bd9 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 19 Mar 2020 20:45:26 -0700 Subject: [PATCH] Fix zones in packages (#33027) --- homeassistant/config.py | 18 ++++++++++++++++-- tests/test_config.py | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/homeassistant/config.py b/homeassistant/config.py index abb8511cab0..27aff8ca36b 100644 --- a/homeassistant/config.py +++ b/homeassistant/config.py @@ -565,9 +565,23 @@ def _log_pkg_error(package: str, component: str, config: Dict, message: str) -> def _identify_config_schema(module: ModuleType) -> Tuple[Optional[str], Optional[Dict]]: """Extract the schema and identify list or dict based.""" try: - schema = module.CONFIG_SCHEMA.schema[module.DOMAIN] # type: ignore - except (AttributeError, KeyError): + key = next(k for k in module.CONFIG_SCHEMA.schema if k == module.DOMAIN) # type: ignore + except (AttributeError, StopIteration): return None, None + + schema = module.CONFIG_SCHEMA.schema[key] # type: ignore + + if hasattr(key, "default"): + default_value = schema(key.default()) + + if isinstance(default_value, dict): + return "dict", schema + + if isinstance(default_value, list): + return "list", schema + + return None, None + t_schema = str(schema) if t_schema.startswith("{") or "schema_with_slug_keys" in t_schema: return ("dict", schema) diff --git a/tests/test_config.py b/tests/test_config.py index fc5ec43093b..43f1263e581 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -10,6 +10,7 @@ from unittest.mock import Mock import asynctest from asynctest import CoroutineMock, patch import pytest +import voluptuous as vol from voluptuous import Invalid, MultipleInvalid import yaml @@ -989,3 +990,20 @@ async def test_component_config_exceptions(hass, caplog): "Unknown error validating config for test_platform platform for test_domain component with PLATFORM_SCHEMA" in caplog.text ) + + +@pytest.mark.parametrize( + "domain, schema, expected", + [ + ("zone", vol.Schema({vol.Optional("zone", default=[]): list}), "list"), + ("zone", vol.Schema({vol.Optional("zone", default=dict): dict}), "dict"), + ], +) +def test_identify_config_schema(domain, schema, expected): + """Test identify config schema.""" + assert ( + config_util._identify_config_schema(Mock(DOMAIN=domain, CONFIG_SCHEMA=schema))[ + 0 + ] + == expected + )