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
async def check(self, request):
"""Check config of homeassistant."""
code, message = await self._homeassistant.check_config()
if not code:
raise RuntimeError(message)
result = await self._homeassistant.check_config()
if not result.valid:
raise RuntimeError(result.log)
return True

View File

@ -1,5 +1,6 @@
"""Init file for HassIO docker object."""
from contextlib import suppress
from collections import namedtuple
import logging
import docker
@ -9,6 +10,8 @@ from ..const import SOCKET_DOCKER
_LOGGER = logging.getLogger(__name__)
CommandReturn = namedtuple('CommandReturn', ['exit_code', 'output'])
class DockerAPI(object):
"""Docker hassio wrapper.
@ -97,15 +100,15 @@ class DockerAPI(object):
)
# wait until command is done
exit_code = container.wait()
result = container.wait()
output = container.logs(stdout=stdout, stderr=stderr)
except docker.errors.DockerException as err:
_LOGGER.error("Can't execute command: %s", err)
return (None, b"")
return CommandReturn(None, b"")
# cleanup container
with suppress(docker.errors.DockerException):
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,
stderr=True,
environment={
'TZ': self._config.timezone,
ENV_TIME: self._config.timezone,
},
volumes={
str(self._config.path_extern_config):

View File

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