mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 00:07:10 +00:00
[core.config] Support customize in packages (#5543)
* Support customize in packages * GMT * Update test_config.py
This commit is contained in:
parent
923431110a
commit
f2870c3103
@ -4,7 +4,6 @@ from collections import OrderedDict
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
from types import MappingProxyType
|
||||
# pylint: disable=unused-import
|
||||
from typing import Any, List, Tuple # NOQA
|
||||
|
||||
@ -121,9 +120,8 @@ CORE_CONFIG_SCHEMA = vol.Schema({
|
||||
vol.Optional(CONF_TEMPERATURE_UNIT): cv.temperature_unit,
|
||||
CONF_UNIT_SYSTEM: cv.unit_system,
|
||||
CONF_TIME_ZONE: cv.time_zone,
|
||||
vol.Required(CONF_CUSTOMIZE,
|
||||
default=MappingProxyType({})): vol.All(
|
||||
_convert_old_config, [CUSTOMIZE_SCHEMA_ENTRY]),
|
||||
vol.Optional(CONF_CUSTOMIZE, default=[]): vol.All(
|
||||
_convert_old_config, [CUSTOMIZE_SCHEMA_ENTRY]),
|
||||
vol.Optional(CONF_PACKAGES, default={}): PACKAGES_CONFIG_SCHEMA,
|
||||
})
|
||||
|
||||
@ -308,7 +306,9 @@ def async_process_ha_core_config(hass, config):
|
||||
if CONF_TIME_ZONE in config:
|
||||
set_time_zone(config.get(CONF_TIME_ZONE))
|
||||
|
||||
set_customize(hass, config.get(CONF_CUSTOMIZE) or {})
|
||||
customize = merge_packages_customize(
|
||||
config[CONF_CUSTOMIZE], config[CONF_PACKAGES])
|
||||
set_customize(hass, customize)
|
||||
|
||||
if CONF_UNIT_SYSTEM in config:
|
||||
if config[CONF_UNIT_SYSTEM] == CONF_UNIT_SYSTEM_IMPERIAL:
|
||||
@ -407,6 +407,8 @@ def merge_packages_config(config, packages):
|
||||
PACKAGES_CONFIG_SCHEMA(packages)
|
||||
for pack_name, pack_conf in packages.items():
|
||||
for comp_name, comp_conf in pack_conf.items():
|
||||
if comp_name == CONF_CORE:
|
||||
continue
|
||||
component = get_component(comp_name)
|
||||
|
||||
if component is None:
|
||||
@ -459,3 +461,18 @@ def merge_packages_config(config, packages):
|
||||
config[comp_name] = comp_conf
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def merge_packages_customize(customize, packages):
|
||||
"""Merge customize from packages."""
|
||||
schema = vol.Schema({
|
||||
vol.Optional(CONF_CORE): vol.Schema({
|
||||
CONF_CUSTOMIZE: vol.All(
|
||||
_convert_old_config, [CUSTOMIZE_SCHEMA_ENTRY])})
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
cust = list(customize)
|
||||
for pkg in packages.values():
|
||||
conf = schema(pkg)
|
||||
cust.extend(conf.get(CONF_CORE, {}).get(CONF_CUSTOMIZE, []))
|
||||
return cust
|
||||
|
@ -473,7 +473,6 @@ def test_merge_type_mismatch(merge_log_err):
|
||||
def test_merge_once_only(merge_log_err):
|
||||
"""Test if we have a merge for a comp that may occur only once."""
|
||||
packages = {
|
||||
'pack_1': {'homeassistant': {}},
|
||||
'pack_2': {
|
||||
'mqtt': {},
|
||||
'api': {}, # No config schema
|
||||
@ -484,7 +483,7 @@ def test_merge_once_only(merge_log_err):
|
||||
'mqtt': {}, 'api': {}
|
||||
}
|
||||
config_util.merge_packages_config(config, packages)
|
||||
assert merge_log_err.call_count == 3
|
||||
assert merge_log_err.call_count == 2
|
||||
assert len(config) == 3
|
||||
|
||||
|
||||
@ -519,3 +518,29 @@ def test_merge_duplicate_keys(merge_log_err):
|
||||
assert merge_log_err.call_count == 1
|
||||
assert len(config) == 2
|
||||
assert len(config['input_select']) == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
def test_merge_customize(hass):
|
||||
"""Test loading core config onto hass object."""
|
||||
core_config = {
|
||||
'latitude': 60,
|
||||
'longitude': 50,
|
||||
'elevation': 25,
|
||||
'name': 'Huis',
|
||||
CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_IMPERIAL,
|
||||
'time_zone': 'GMT',
|
||||
'customize': {'a.a': {'friendly_name': 'A'}},
|
||||
'packages': {'pkg1': {'homeassistant': {'customize': {
|
||||
'b.b': {'friendly_name': 'BB'}}}}},
|
||||
}
|
||||
yield from config_util.async_process_ha_core_config(hass, core_config)
|
||||
|
||||
entity = Entity()
|
||||
entity.entity_id = 'b.b'
|
||||
entity.hass = hass
|
||||
yield from entity.async_update_ha_state()
|
||||
|
||||
state = hass.states.get('b.b')
|
||||
assert state is not None
|
||||
assert state.attributes['friendly_name'] == 'BB'
|
||||
|
Loading…
x
Reference in New Issue
Block a user