Bugfix Check Config for Home-Assistant (#350)

* add logger

* Bugfix config check
This commit is contained in:
Pascal Vizeli 2018-02-10 00:10:30 +01:00 committed by GitHub
parent e939c29efa
commit 23c35d4c80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 14 deletions

View File

@ -136,8 +136,8 @@ class APIHomeAssistant(CoreSysAttributes):
@api_process @api_process
async def check(self, request): async def check(self, request):
"""Check config of homeassistant.""" """Check config of homeassistant."""
code, message = await self._homeassistant.check_config() result = await self._homeassistant.check_config()
if not code: if not result.valid:
raise RuntimeError(message) raise RuntimeError(result.log)
return True return True

View File

@ -1,5 +1,6 @@
"""Init file for HassIO docker object.""" """Init file for HassIO docker object."""
from contextlib import suppress from contextlib import suppress
from collections import namedtuple
import logging import logging
import docker import docker
@ -9,6 +10,8 @@ from ..const import SOCKET_DOCKER
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CommandReturn = namedtuple('CommandReturn', ['exit_code', 'output'])
class DockerAPI(object): class DockerAPI(object):
"""Docker hassio wrapper. """Docker hassio wrapper.
@ -97,15 +100,15 @@ class DockerAPI(object):
) )
# wait until command is done # wait until command is done
exit_code = container.wait() result = container.wait()
output = container.logs(stdout=stdout, stderr=stderr) output = container.logs(stdout=stdout, stderr=stderr)
except docker.errors.DockerException as err: except docker.errors.DockerException as err:
_LOGGER.error("Can't execute command: %s", err) _LOGGER.error("Can't execute command: %s", err)
return (None, b"") return CommandReturn(None, b"")
# cleanup container # cleanup container
with suppress(docker.errors.DockerException): with suppress(docker.errors.DockerException):
container.remove(force=True) container.remove(force=True)
return (exit_code, output) return CommandReturn(result.get('StatusCode'), output)

View File

@ -85,7 +85,7 @@ class DockerHomeAssistant(DockerInterface):
stdout=True, stdout=True,
stderr=True, stderr=True,
environment={ environment={
'TZ': self._config.timezone, ENV_TIME: self._config.timezone,
}, },
volumes={ volumes={
str(self._config.path_extern_config): str(self._config.path_extern_config):

View File

@ -1,5 +1,6 @@
"""HomeAssistant control object.""" """HomeAssistant control object."""
import asyncio import asyncio
from collections import namedtuple
import logging import logging
import os import os
import re import re
@ -23,6 +24,8 @@ _LOGGER = logging.getLogger(__name__)
RE_YAML_ERROR = re.compile(r"homeassistant\.util\.yaml") RE_YAML_ERROR = re.compile(r"homeassistant\.util\.yaml")
ConfigResult = namedtuple('ConfigResult', ['valid', 'log'])
class HomeAssistant(JsonConfig, CoreSysAttributes): class HomeAssistant(JsonConfig, CoreSysAttributes):
"""Hass core object for handle it.""" """Hass core object for handle it."""
@ -263,19 +266,19 @@ class HomeAssistant(JsonConfig, CoreSysAttributes):
async def check_config(self): async def check_config(self):
"""Run homeassistant config check.""" """Run homeassistant config check."""
exit_code, log = await self.instance.execute_command( result = await self.instance.execute_command(
"python3 -m homeassistant -c /config --script check_config" "python3 -m homeassistant -c /config --script check_config"
) )
# if not valid # if not valid
if exit_code is None: if result.exit_code is None:
return (False, "") return ConfigResult(False, "")
# parse output # parse output
log = convert_to_ascii(log) log = convert_to_ascii(result.output)
if exit_code != 0 or RE_YAML_ERROR.search(log): if result.exit_code != 0 or RE_YAML_ERROR.search(log):
return (False, log) return ConfigResult(False, log)
return (True, log) return ConfigResult(True, log)
async def check_api_state(self): async def check_api_state(self):
"""Check if Home-Assistant up and running.""" """Check if Home-Assistant up and running."""