Packages to support config platforms (#54085)

This commit is contained in:
Paulus Schoutsen 2021-08-05 13:11:01 -07:00 committed by GitHub
parent fcc3d24904
commit 8377b557da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 5 deletions

View File

@ -71,9 +71,6 @@ from homeassistant.loader import bind_hass
from homeassistant.util.dt import parse_datetime
from .config import AutomationConfig, async_validate_config_item
# Not used except by packages to check config structure
from .config import PLATFORM_SCHEMA # noqa: F401
from .const import (
CONF_ACTION,
CONF_INITIAL_STATE,

View File

@ -37,6 +37,8 @@ from .helpers import async_get_blueprints
# mypy: allow-untyped-calls, allow-untyped-defs
# mypy: no-check-untyped-defs, no-warn-return-any
PACKAGE_MERGE_HINT = "list"
_CONDITION_SCHEMA = vol.All(cv.ensure_list, [cv.CONDITION_SCHEMA])
PLATFORM_SCHEMA = vol.All(

View File

@ -39,6 +39,8 @@ from .const import (
)
from .helpers import async_get_blueprints
PACKAGE_MERGE_HINT = "dict"
SCRIPT_ENTITY_SCHEMA = make_script_schema(
{
vol.Optional(CONF_ALIAS): cv.string,

View File

@ -13,6 +13,8 @@ from homeassistant.helpers.trigger import async_validate_trigger_config
from . import binary_sensor as binary_sensor_platform, sensor as sensor_platform
from .const import CONF_TRIGGER, DOMAIN
PACKAGE_MERGE_HINT = "list"
CONFIG_SECTION_SCHEMA = vol.Schema(
{
vol.Optional(CONF_UNIQUE_ID): cv.string,

View File

@ -723,7 +723,22 @@ async def merge_packages_config(
_log_pkg_error(pack_name, comp_name, config, str(ex))
continue
merge_list = hasattr(component, "PLATFORM_SCHEMA")
try:
config_platform: ModuleType | None = integration.get_platform("config")
# Test if config platform has a config validator
if not hasattr(config_platform, "async_validate_config"):
config_platform = None
except ImportError:
config_platform = None
merge_list = False
# If integration has a custom config validator, it needs to provide a hint.
if config_platform is not None:
merge_list = config_platform.PACKAGE_MERGE_HINT == "list" # type: ignore[attr-defined]
if not merge_list:
merge_list = hasattr(component, "PLATFORM_SCHEMA")
if not merge_list and hasattr(component, "CONFIG_SCHEMA"):
merge_list = _identify_config_schema(component) == "list"

View File

@ -650,19 +650,30 @@ async def test_merge(merge_log_err, hass):
"pack_list": {"light": {"platform": "test"}},
"pack_list2": {"light": [{"platform": "test"}]},
"pack_none": {"wake_on_lan": None},
"pack_special": {
"automation": [{"some": "yay"}],
"script": {"a_script": "yay"},
"template": [{"some": "yay"}],
},
}
config = {
config_util.CONF_CORE: {config_util.CONF_PACKAGES: packages},
"input_boolean": {"ib2": None},
"light": {"platform": "test"},
"automation": [],
"script": {},
"template": [],
}
await config_util.merge_packages_config(hass, config, packages)
assert merge_log_err.call_count == 0
assert len(config) == 5
assert len(config) == 8
assert len(config["input_boolean"]) == 2
assert len(config["input_select"]) == 1
assert len(config["light"]) == 3
assert len(config["automation"]) == 1
assert len(config["script"]) == 1
assert len(config["template"]) == 1
assert isinstance(config["wake_on_lan"], OrderedDict)