check_config: Add support for packages (#5574)
This commit is contained in:
parent
a465a45588
commit
3f2fdb97a0
2 changed files with 45 additions and 14 deletions
|
@ -27,7 +27,9 @@ MOCKS = {
|
||||||
'get': ("homeassistant.loader.get_component", loader.get_component),
|
'get': ("homeassistant.loader.get_component", loader.get_component),
|
||||||
'secrets': ("homeassistant.util.yaml._secret_yaml", yaml._secret_yaml),
|
'secrets': ("homeassistant.util.yaml._secret_yaml", yaml._secret_yaml),
|
||||||
'except': ("homeassistant.bootstrap.async_log_exception",
|
'except': ("homeassistant.bootstrap.async_log_exception",
|
||||||
bootstrap.async_log_exception)
|
bootstrap.async_log_exception),
|
||||||
|
'package_error': ("homeassistant.config._log_pkg_error",
|
||||||
|
config_util._log_pkg_error),
|
||||||
}
|
}
|
||||||
SILENCE = (
|
SILENCE = (
|
||||||
'homeassistant.bootstrap.clear_secret_cache',
|
'homeassistant.bootstrap.clear_secret_cache',
|
||||||
|
@ -213,6 +215,15 @@ def check(config_path):
|
||||||
MOCKS['except'][1](ex, domain, config, hass)
|
MOCKS['except'][1](ex, domain, config, hass)
|
||||||
res['except'][domain] = config.get(domain, config)
|
res['except'][domain] = config.get(domain, config)
|
||||||
|
|
||||||
|
def mock_package_error( # pylint: disable=unused-variable
|
||||||
|
package, component, config, message):
|
||||||
|
"""Mock config_util._log_pkg_error."""
|
||||||
|
MOCKS['package_error'][1](package, component, config, message)
|
||||||
|
|
||||||
|
pkg_key = 'homeassistant.packages.{}'.format(package)
|
||||||
|
res['except'][pkg_key] = config.get('homeassistant', {}) \
|
||||||
|
.get('packages', {}).get(package)
|
||||||
|
|
||||||
# Patches to skip functions
|
# Patches to skip functions
|
||||||
for sil in SILENCE:
|
for sil in SILENCE:
|
||||||
PATCHES[sil] = patch(sil)
|
PATCHES[sil] = patch(sil)
|
||||||
|
@ -247,25 +258,24 @@ def check(config_path):
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def line_info(obj, **kwargs):
|
||||||
|
"""Display line config source."""
|
||||||
|
if hasattr(obj, '__config_file__'):
|
||||||
|
return color('cyan', "[source {}:{}]"
|
||||||
|
.format(obj.__config_file__, obj.__line__ or '?'),
|
||||||
|
**kwargs)
|
||||||
|
return '?'
|
||||||
|
|
||||||
|
|
||||||
def dump_dict(layer, indent_count=3, listi=False, **kwargs):
|
def dump_dict(layer, indent_count=3, listi=False, **kwargs):
|
||||||
"""Display a dict.
|
"""Display a dict.
|
||||||
|
|
||||||
A friendly version of print yaml.yaml.dump(config).
|
A friendly version of print yaml.yaml.dump(config).
|
||||||
"""
|
"""
|
||||||
def line_src(this):
|
|
||||||
"""Display line config source."""
|
|
||||||
if hasattr(this, '__config_file__'):
|
|
||||||
return color('cyan', "[source {}:{}]"
|
|
||||||
.format(this.__config_file__, this.__line__ or '?'),
|
|
||||||
**kwargs)
|
|
||||||
return ''
|
|
||||||
|
|
||||||
def sort_dict_key(val):
|
def sort_dict_key(val):
|
||||||
"""Return the dict key for sorting."""
|
"""Return the dict key for sorting."""
|
||||||
skey = str.lower(val[0])
|
key = str.lower(val[0])
|
||||||
if str(skey) == 'platform':
|
return '0' if key == 'platform' else key
|
||||||
skey = '0'
|
|
||||||
return skey
|
|
||||||
|
|
||||||
indent_str = indent_count * ' '
|
indent_str = indent_count * ' '
|
||||||
if listi or isinstance(layer, list):
|
if listi or isinstance(layer, list):
|
||||||
|
@ -273,7 +283,7 @@ def dump_dict(layer, indent_count=3, listi=False, **kwargs):
|
||||||
if isinstance(layer, Dict):
|
if isinstance(layer, Dict):
|
||||||
for key, value in sorted(layer.items(), key=sort_dict_key):
|
for key, value in sorted(layer.items(), key=sort_dict_key):
|
||||||
if isinstance(value, dict) or isinstance(value, list):
|
if isinstance(value, dict) or isinstance(value, list):
|
||||||
print(indent_str, key + ':', line_src(value))
|
print(indent_str, key + ':', line_info(value, **kwargs))
|
||||||
dump_dict(value, indent_count + 2)
|
dump_dict(value, indent_count + 2)
|
||||||
else:
|
else:
|
||||||
print(indent_str, key + ':', value)
|
print(indent_str, key + ':', value)
|
||||||
|
|
|
@ -180,3 +180,24 @@ class TestCheckConfig(unittest.TestCase):
|
||||||
'secrets': {'http_pw': 'abc123'},
|
'secrets': {'http_pw': 'abc123'},
|
||||||
'yaml_files': ['.../secret.yaml', '.../secrets.yaml']
|
'yaml_files': ['.../secret.yaml', '.../secrets.yaml']
|
||||||
}, res)
|
}, res)
|
||||||
|
|
||||||
|
def test_package_invalid(self): \
|
||||||
|
# pylint: disable=no-self-use,invalid-name
|
||||||
|
"""Test a valid platform setup."""
|
||||||
|
files = {
|
||||||
|
'bad.yaml': BASE_CONFIG + (' packages:\n'
|
||||||
|
' p1:\n'
|
||||||
|
' group: ["a"]'),
|
||||||
|
}
|
||||||
|
with patch_yaml_files(files):
|
||||||
|
res = check_config.check(get_test_config_dir('bad.yaml'))
|
||||||
|
change_yaml_files(res)
|
||||||
|
|
||||||
|
err = res['except'].pop('homeassistant.packages.p1')
|
||||||
|
assert res['except'] == {}
|
||||||
|
assert err == {'group': ['a']}
|
||||||
|
assert res['yaml_files'] == ['.../bad.yaml']
|
||||||
|
|
||||||
|
assert res['components'] == {}
|
||||||
|
assert res['secret_cache'] == {}
|
||||||
|
assert res['secrets'] == {}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue