From 02366116769d6015def8a161888c2d50c3d93473 Mon Sep 17 00:00:00 2001 From: pvizeli Date: Fri, 31 Mar 2017 12:28:30 +0200 Subject: [PATCH] Cleanup api code --- hassio_api/README.md | 9 ++- hassio_api/hassio/api/host.py | 60 ++++++------------- hassio_api/hassio/api/supervisor.py | 13 ++-- hassio_api/hassio/api/util.py | 21 ++++++- hassio_api/hassio/host_controll.py | 2 + .../hassio-host-controll/hassio-hc | 8 +-- 6 files changed, 54 insertions(+), 59 deletions(-) diff --git a/hassio_api/README.md b/hassio_api/README.md index f57fe1f4c..ef0f92b62 100644 --- a/hassio_api/README.md +++ b/hassio_api/README.md @@ -28,11 +28,16 @@ level: - 4: host update - 8: network functions -- Answer +Answer: ``` -{}|OK|ERROR +{}|OK|ERROR|WRONG ``` +- {}: json +- OK: call was successfully +- ERROR: error on call +- WRONG: not supported + ## HassIO REST API Interface for HomeAssistant to controll things from supervisor. diff --git a/hassio_api/hassio/api/host.py b/hassio_api/hassio/api/host.py index e2fdbbb03..3c0431571 100644 --- a/hassio_api/hassio/api/host.py +++ b/hassio_api/hassio/api/host.py @@ -1,10 +1,7 @@ """Init file for HassIO host rest api.""" import logging -from aiohttp import web -from aiohttp.web_exceptions import HTTPServiceUnavailable - -from .util import api_return_ok, api_return_not_supported +from .util import api_process_hostcontroll from ..const import ATTR_VERSION _LOGGER = logging.getLogger(__name__) @@ -19,54 +16,33 @@ class APIHost(object): self.loop = loop self.host_controll = host_controll - async def info(self, request): + @api_process_hostcontroll + def info(self, request): """Return host information.""" - if not self.host_controll.active: - raise HTTPServiceUnavailable() + return self.host_controll.info() - host_info = await self.host_controll.info() - if host_info: - return web.json_response(host_info) - return api_return_not_supported() - - async def reboot(self, request): + @api_process_hostcontroll + def reboot(self, request): """Reboot host.""" - if not self.host_controll.active: - raise HTTPServiceUnavailable() + return self.host_controll.reboot(): - if await self.host_controll.reboot(): - return api_return_ok() - return api_return_not_supported() - - async def shutdown(self, request): + @api_process_hostcontroll + def shutdown(self, request): """Poweroff host.""" - if not self.host_controll.active: - raise HTTPServiceUnavailable() + return self.host_controll.shutdown(): - if await self.host_controll.shutdown(): - return api_return_ok() - return api_return_not_supported() - - async def network_info(self, request): + @api_process_hostcontroll + def network_info(self, request): """Edit network settings.""" - if not self.host_controll.active: - raise HTTPServiceUnavailable() + pass - return api_return_not_supported() - - async def network_update(self, request): + @api_process_hostcontroll + def network_update(self, request): """Edit network settings.""" - if not self.host_controll.active: - raise HTTPServiceUnavailable() - - return api_return_not_supported() + pass + @api_process_hostcontroll async def update(self, request): """Update host OS.""" - if not self.host_controll.active: - raise HTTPServiceUnavailable() - body = await request.json() or {} - if await self.host_controll.host_update(body.get(ATTR_VERSION)): - return api_return_ok() - return api_return_not_supported() + return await self.host_controll.host_update(body.get(ATTR_VERSION)) diff --git a/hassio_api/hassio/api/supervisor.py b/hassio_api/hassio/api/supervisor.py index c03eb59af..0cfef1276 100644 --- a/hassio_api/hassio/api/supervisor.py +++ b/hassio_api/hassio/api/supervisor.py @@ -1,9 +1,7 @@ """Init file for HassIO supervisor rest api.""" import logging -from aiohttp.web_exceptions import HTTPServiceUnavailable - -from .util import api_return_ok, api_return_not_supported +from .util import api_return_ok, api_process_hostcontroll from ..const import ATTR_VERSION, HASSIO_VERSION _LOGGER = logging.getLogger(__name__) @@ -24,12 +22,9 @@ class APISupervisor(object): ATTR_VERSION: HASSIO_VERSION, }) + @api_process_hostcontroll async def update(self, request): """Update host OS.""" - if not self.host_controll.active: - raise HTTPServiceUnavailable() - body = await request.json() or {} - if await self.host_controll.supervisor_update(body.get(ATTR_VERSION)): - return api_return_ok() - return api_return_not_supported() + return await self.host_controll.supervisor_update( + body.get(ATTR_VERSION)) diff --git a/hassio_api/hassio/api/util.py b/hassio_api/hassio/api/util.py index d530269c0..7a1b5ec8c 100644 --- a/hassio_api/hassio/api/util.py +++ b/hassio_api/hassio/api/util.py @@ -2,6 +2,7 @@ import logging from aiohttp import web +from aiohttp.web_exceptions import HTTPServiceUnavailable from ..const import ( JSON_RESULT, JSON_DATA, JSON_MESSAGE, RESULT_OK, RESULT_ERROR) @@ -9,6 +10,24 @@ from ..const import ( _LOGGER = logging.getLogger(__name__) +def api_process_hostcontroll(method): + async def wrap_hostcontroll(api, *args, **kwargs): + """Return host information.""" + if not api.host_controll.active: + raise HTTPServiceUnavailable() + + answer = await method(api, *args, **kwargs) + if isinstance(answer, dict): + return web.json_response(answer) + elif answer is None: + return api_not_supported() + elif answer: + return api_return_ok() + return api_return_error() + + return wrap_hostcontroll + + def api_return_error(message=None): """Return a API error message.""" return web.json_response({ @@ -25,6 +44,6 @@ def api_return_ok(data=None): }) -def api_return_not_supported(): +def api_not_supported(): """Return a api error with not supported.""" return api_return_error("Function is not supported") diff --git a/hassio_api/hassio/host_controll.py b/hassio_api/hassio/host_controll.py index 55dda28aa..81c64b1b0 100644 --- a/hassio_api/hassio/host_controll.py +++ b/hassio_api/hassio/host_controll.py @@ -57,6 +57,8 @@ class HostControll(object): return True elif response == "ERROR": return False + elif response == "WRONG": + return None else: try: return json.loads(response) diff --git a/meta-hassio/recipes-support/hassio-host-controll/hassio-host-controll/hassio-hc b/meta-hassio/recipes-support/hassio-host-controll/hassio-host-controll/hassio-hc index 29e2d9e6f..5509eed84 100644 --- a/meta-hassio/recipes-support/hassio-host-controll/hassio-host-controll/hassio-hc +++ b/meta-hassio/recipes-support/hassio-host-controll/hassio-host-controll/hassio-hc @@ -21,13 +21,11 @@ do continue fi if [ ${parse[0]} == "reboot" ]; then - systemctl reboot || true - echo "OK" + systemctl reboot && echo "OK" || echo "ERROR" continue fi if [ ${parse[0]} == "shutdown" ]; then - systemctl poweroff || true - echo "OK" + systemctl poweroff && echo "OK" || echo "ERROR" continue fi if [ ${parse[0]} == "host-update" ]; then @@ -49,5 +47,5 @@ do continue fi - echo "ERROR" + echo "WRONG" done