Fix zones in packages (#33027)

This commit is contained in:
Paulus Schoutsen 2020-03-19 20:45:26 -07:00
parent 6a6037790f
commit 5f0816ea25
2 changed files with 34 additions and 2 deletions

View File

@ -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]]: def _identify_config_schema(module: ModuleType) -> Tuple[Optional[str], Optional[Dict]]:
"""Extract the schema and identify list or dict based.""" """Extract the schema and identify list or dict based."""
try: try:
schema = module.CONFIG_SCHEMA.schema[module.DOMAIN] # type: ignore key = next(k for k in module.CONFIG_SCHEMA.schema if k == module.DOMAIN) # type: ignore
except (AttributeError, KeyError): except (AttributeError, StopIteration):
return None, None 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) t_schema = str(schema)
if t_schema.startswith("{") or "schema_with_slug_keys" in t_schema: if t_schema.startswith("{") or "schema_with_slug_keys" in t_schema:
return ("dict", schema) return ("dict", schema)

View File

@ -10,6 +10,7 @@ from unittest.mock import Mock
import asynctest import asynctest
from asynctest import CoroutineMock, patch from asynctest import CoroutineMock, patch
import pytest import pytest
import voluptuous as vol
from voluptuous import Invalid, MultipleInvalid from voluptuous import Invalid, MultipleInvalid
import yaml 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" "Unknown error validating config for test_platform platform for test_domain component with PLATFORM_SCHEMA"
in caplog.text 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
)