add logoutbut (#29)

* add logoutbut

* Protect host update

* schedule a hostcontrol update task

* change log level

* cleanup names
This commit is contained in:
Pascal Vizeli 2017-05-02 23:16:58 +02:00 committed by GitHub
parent f6048467ad
commit 19b72b1a79
5 changed files with 17 additions and 10 deletions

View File

@ -1,4 +1,5 @@
"""Init file for HassIO host rest api."""
import asyncio
import logging
import voluptuous as vol
@ -30,7 +31,7 @@ class APIHost(object):
return {
ATTR_TYPE: self.host_control.type,
ATTR_VERSION: self.host_control.version,
ATTR_LAST_VERSION: self.host_control.last,
ATTR_LAST_VERSION: self.host_control.last_version,
ATTR_FEATURES: self.host_control.features,
ATTR_HOSTNAME: self.host_control.hostname,
ATTR_OS: self.host_control.os_info,
@ -50,9 +51,10 @@ class APIHost(object):
async def update(self, request):
"""Update host OS."""
body = await api_validate(SCHEMA_VERSION, request)
version = body.get(ATTR_VERSION)
version = body.get(ATTR_VERSION, self.host_control.last_version)
if version == self.host_control.version:
raise RuntimeError("Version is already in use")
return await self.host_control.update(version=version)
return await asyncio.shield(
self.host_control.update(version=version), loop=self.loop)

View File

@ -9,7 +9,7 @@ from voluptuous.humanize import humanize_error
from .const import FILE_HASSIO_CONFIG, HASSIO_SHARE
from .tools import (
fetch_current_versions, write_json_file, read_json_file)
fetch_last_versions, write_json_file, read_json_file)
_LOGGER = logging.getLogger(__name__)
@ -87,7 +87,7 @@ class CoreConfig(Config):
async def fetch_update_infos(self):
"""Read current versions from web."""
last = await fetch_current_versions(
last = await fetch_last_versions(
self.websession, beta=self.upstream_beta)
if last:

View File

@ -63,6 +63,10 @@ class HassIO(object):
self.host_control.version, self.host_control.hostname,
self.host_control.features)
# schedule update info tasks
self.scheduler.register_task(
self.host_control.load(), RUN_UPDATE_INFO_TASKS)
# rest api views
self.api.register_host(self.host_control)
self.api.register_network(self.host_control)

View File

@ -29,7 +29,7 @@ class HostControl(object):
self.loop = loop
self.active = False
self.version = UNKNOWN
self.last = UNKNOWN
self.last_version = UNKNOWN
self.type = UNKNOWN
self.features = []
self.hostname = UNKNOWN
@ -58,7 +58,7 @@ class HostControl(object):
data = await reader.readline()
response = data.decode()
_LOGGER.debug("Receive from HostControl: %s.", response)
_LOGGER.info("Receive from HostControl: %s.", response)
if response == "OK":
return True
@ -70,7 +70,8 @@ class HostControl(object):
try:
return json.loads(response)
except json.JSONDecodeError:
_LOGGER.warning("Json parse error from HostControl.")
_LOGGER.warning("Json parse error from HostControl '%s'.",
response)
except asyncio.TimeoutError:
_LOGGER.error("Timeout from HostControl!")
@ -88,7 +89,7 @@ class HostControl(object):
return
self.version = info.get(ATTR_VERSION, UNKNOWN)
self.last = info.get(ATTR_LAST_VERSION, UNKNOWN)
self.last_version = info.get(ATTR_LAST_VERSION, UNKNOWN)
self.type = info.get(ATTR_TYPE, UNKNOWN)
self.features = info.get(ATTR_FEATURES, [])
self.hostname = info.get(ATTR_HOSTNAME, UNKNOWN)

View File

@ -16,7 +16,7 @@ _RE_VERSION = re.compile(r"VERSION=(.*)")
_IMAGE_ARCH = re.compile(r".*/([a-z0-9]*)-hassio-supervisor")
async def fetch_current_versions(websession, beta=False):
async def fetch_last_versions(websession, beta=False):
"""Fetch current versions from github.
Is a coroutine.