Fix check config script (#23151)

* Fix check config script

* Fix typings

* Fix test
This commit is contained in:
Paulus Schoutsen 2019-04-16 17:14:26 -07:00 committed by Jason Hu
parent 1bfccd803f
commit 37ca9cabd1
3 changed files with 24 additions and 23 deletions

View File

@ -817,8 +817,7 @@ async def async_check_ha_config_file(hass: HomeAssistant) -> Optional[str]:
""" """
from homeassistant.scripts.check_config import check_ha_config_file from homeassistant.scripts.check_config import check_ha_config_file
res = await hass.async_add_executor_job( res = await check_ha_config_file(hass) # type: ignore
check_ha_config_file, hass)
if not res.errors: if not res.errors:
return None return None

View File

@ -201,7 +201,8 @@ def check(config_dir, secrets=False):
hass = core.HomeAssistant() hass = core.HomeAssistant()
hass.config.config_dir = config_dir hass.config.config_dir = config_dir
res['components'] = check_ha_config_file(hass) res['components'] = hass.loop.run_until_complete(
check_ha_config_file(hass))
res['secret_cache'] = OrderedDict(yaml.__SECRET_CACHE) res['secret_cache'] = OrderedDict(yaml.__SECRET_CACHE)
for err in res['components'].errors: for err in res['components'].errors:
@ -280,7 +281,7 @@ class HomeAssistantConfig(OrderedDict):
return self return self
def check_ha_config_file(hass): async def check_ha_config_file(hass):
"""Check if Home Assistant configuration file is valid.""" """Check if Home Assistant configuration file is valid."""
config_dir = hass.config.config_dir config_dir = hass.config.config_dir
result = HomeAssistantConfig() result = HomeAssistantConfig()
@ -300,10 +301,12 @@ def check_ha_config_file(hass):
# Load configuration.yaml # Load configuration.yaml
try: try:
config_path = find_config_file(config_dir) config_path = await hass.async_add_executor_job(
find_config_file, config_dir)
if not config_path: if not config_path:
return result.add_error("File configuration.yaml not found.") return result.add_error("File configuration.yaml not found.")
config = load_yaml_config_file(config_path) config = await hass.async_add_executor_job(
load_yaml_config_file, config_path)
except HomeAssistantError as err: except HomeAssistantError as err:
return result.add_error( return result.add_error(
"Error loading {}: {}".format(config_path, err)) "Error loading {}: {}".format(config_path, err))
@ -320,8 +323,8 @@ def check_ha_config_file(hass):
core_config = {} core_config = {}
# Merge packages # Merge packages
hass.loop.run_until_complete(merge_packages_config( await merge_packages_config(
hass, config, core_config.get(CONF_PACKAGES, {}), _pack_error)) hass, config, core_config.get(CONF_PACKAGES, {}), _pack_error)
core_config.pop(CONF_PACKAGES, None) core_config.pop(CONF_PACKAGES, None)
# Filter out repeating config sections # Filter out repeating config sections
@ -330,8 +333,7 @@ def check_ha_config_file(hass):
# Process and validate config # Process and validate config
for domain in components: for domain in components:
try: try:
integration = hass.loop.run_until_complete( integration = await loader.async_get_integration(hass, domain)
loader.async_get_integration(hass, domain))
except loader.IntegrationNotFound: except loader.IntegrationNotFound:
result.add_error("Integration not found: {}".format(domain)) result.add_error("Integration not found: {}".format(domain))
continue continue
@ -350,21 +352,19 @@ def check_ha_config_file(hass):
_comp_error(ex, domain, config) _comp_error(ex, domain, config)
continue continue
if (not hasattr(component, 'PLATFORM_SCHEMA') and component_platform_schema = getattr(
not hasattr(component, 'PLATFORM_SCHEMA_BASE')): component, 'PLATFORM_SCHEMA_BASE',
getattr(component, 'PLATFORM_SCHEMA', None))
if component_platform_schema is None:
continue continue
platforms = [] platforms = []
for p_name, p_config in config_per_platform(config, domain): for p_name, p_config in config_per_platform(config, domain):
# Validate component specific platform schema # Validate component specific platform schema
try: try:
if hasattr(component, 'PLATFORM_SCHEMA_BASE'): p_validated = component_platform_schema( # type: ignore
p_validated = \ p_config)
component.PLATFORM_SCHEMA_BASE( # type: ignore
p_config)
else:
p_validated = component.PLATFORM_SCHEMA( # type: ignore
p_config)
except vol.Invalid as ex: except vol.Invalid as ex:
_comp_error(ex, domain, config) _comp_error(ex, domain, config)
continue continue
@ -377,8 +377,8 @@ def check_ha_config_file(hass):
continue continue
try: try:
p_integration = hass.loop.run_until_complete( p_integration = await loader.async_get_integration(hass,
loader.async_get_integration(hass, p_name)) p_name)
except loader.IntegrationNotFound: except loader.IntegrationNotFound:
result.add_error( result.add_error(
"Integration {} not found when trying to verify its {} " "Integration {} not found when trying to verify its {} "

View File

@ -539,7 +539,8 @@ class TestConfig(unittest.TestCase):
assert len(self.hass.config.whitelist_external_dirs) == 1 assert len(self.hass.config.whitelist_external_dirs) == 1
assert "/test/config/www" in self.hass.config.whitelist_external_dirs assert "/test/config/www" in self.hass.config.whitelist_external_dirs
@mock.patch('homeassistant.scripts.check_config.check_ha_config_file') @asynctest.mock.patch(
'homeassistant.scripts.check_config.check_ha_config_file')
def test_check_ha_config_file_correct(self, mock_check): def test_check_ha_config_file_correct(self, mock_check):
"""Check that restart propagates to stop.""" """Check that restart propagates to stop."""
mock_check.return_value = check_config.HomeAssistantConfig() mock_check.return_value = check_config.HomeAssistantConfig()
@ -548,7 +549,8 @@ class TestConfig(unittest.TestCase):
self.hass.loop self.hass.loop
).result() is None ).result() is None
@mock.patch('homeassistant.scripts.check_config.check_ha_config_file') @asynctest.mock.patch(
'homeassistant.scripts.check_config.check_ha_config_file')
def test_check_ha_config_file_wrong(self, mock_check): def test_check_ha_config_file_wrong(self, mock_check):
"""Check that restart with a bad config doesn't propagate to stop.""" """Check that restart with a bad config doesn't propagate to stop."""
mock_check.return_value = check_config.HomeAssistantConfig() mock_check.return_value = check_config.HomeAssistantConfig()