Fix zones in packages (#33027)

This commit is contained in:
Paulus Schoutsen 2020-03-19 20:45:26 -07:00 committed by GitHub
parent 5db1a67c20
commit c3c5cc9ae7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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]]:
"""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)

View File

@ -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
)