From 23278550beca21ebe2a0e47634d094c17ab97286 Mon Sep 17 00:00:00 2001 From: pvizeli Date: Fri, 7 Apr 2017 08:57:40 +0200 Subject: [PATCH] Updates for Release 0.5 --- hassio/__main__.py | 3 ++- hassio/api/__init__.py | 1 + hassio/api/host.py | 30 ++++++++++++++++-------------- hassio/api/supervisor.py | 5 +++++ hassio/api/util.py | 9 ++------- 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/hassio/__main__.py b/hassio/__main__.py index 788ab64e2..01d82b714 100644 --- a/hassio/__main__.py +++ b/hassio/__main__.py @@ -23,7 +23,7 @@ if __name__ == "__main__": loop.run_until_complete(hassio.setup()) _LOGGER.info("Start Hassio task") - loop.create_task(hassio.start()) + loop.call_soon_threadsafe(asyncio.ensure_future, hassio.start(), loop) try: loop.add_signal_handler( @@ -32,4 +32,5 @@ if __name__ == "__main__": _LOGGER.warning("Could not bind to SIGTERM") loop.run_forever() + loop.close() _LOGGER.info("Close Hassio") diff --git a/hassio/api/__init__.py b/hassio/api/__init__.py index cde4ae8ef..1bd0d577a 100644 --- a/hassio/api/__init__.py +++ b/hassio/api/__init__.py @@ -44,6 +44,7 @@ class RestAPI(object): """Register supervisor function.""" api_supervisor = APISupervisor(self.config, self.loop, host_controll) + self.webapp.router.add_get('/supervisor/ping', api_supervisor.ping) self.webapp.router.add_get('/supervisor/info', api_supervisor.info) self.webapp.router.add_get('/supervisor/update', api_supervisor.update) self.webapp.router.add_get( diff --git a/hassio/api/host.py b/hassio/api/host.py index 5fc0c0fa4..8dc3a2217 100644 --- a/hassio/api/host.py +++ b/hassio/api/host.py @@ -1,11 +1,13 @@ """Init file for HassIO host rest api.""" import logging -from .util import api_process_hostcontroll, json_loads +from .util import api_process_hostcontroll, api_process, json_loads from ..const import ATTR_VERSION _LOGGER = logging.getLogger(__name__) +UNKNOWN = 'unknown' + class APIHost(object): """Handle rest api for host functions.""" @@ -16,10 +18,20 @@ class APIHost(object): self.loop = loop self.host_controll = host_controll - @api_process_hostcontroll - def info(self, request): + @api_process + async def info(self, request): """Return host information.""" - return self.host_controll.info() + if not self.host_controll.active: + info = { + 'os': UNKNOWN, + 'version': UNKNOWN, + 'current': UNKNOWN, + 'level': 0, + 'hostname': UNKNOWN, + } + return info + + return await self.host_controll.info() @api_process_hostcontroll def reboot(self, request): @@ -31,16 +43,6 @@ class APIHost(object): """Poweroff host.""" return self.host_controll.shutdown() - @api_process_hostcontroll - def network_info(self, request): - """Edit network settings.""" - pass - - @api_process_hostcontroll - def network_update(self, request): - """Edit network settings.""" - pass - @api_process_hostcontroll async def update(self, request): """Update host OS.""" diff --git a/hassio/api/supervisor.py b/hassio/api/supervisor.py index 62925b1fe..7c57d12ed 100644 --- a/hassio/api/supervisor.py +++ b/hassio/api/supervisor.py @@ -16,6 +16,11 @@ class APISupervisor(object): self.loop = loop self.host_controll = host_controll + @api_process + async def ping(self, request): + """Return ok for signal that the api is ready.""" + return True + @api_process async def info(self, request): """Return host information.""" diff --git a/hassio/api/util.py b/hassio/api/util.py index c1225400b..f78da86e9 100644 --- a/hassio/api/util.py +++ b/hassio/api/util.py @@ -52,7 +52,7 @@ def api_process_hostcontroll(method): if isinstance(answer, dict): return api_return_ok(data=answer) elif answer is None: - return api_not_supported() + return api_return_error("Function is not supported") elif answer: return api_return_ok() return api_return_error() @@ -68,14 +68,9 @@ def api_return_error(message=None): }) -def api_return_ok(data=None): +def api_return_ok(data={}): """Return a API ok answer.""" return web.json_response({ JSON_RESULT: RESULT_OK, JSON_DATA: data, }) - - -def api_not_supported(): - """Return a api error with not supported.""" - return api_return_error("Function is not supported")