From ad057352c0c53dd1efc5b1da65d4e35ae8a8c795 Mon Sep 17 00:00:00 2001 From: pvizeli Date: Thu, 30 Mar 2017 16:54:13 +0200 Subject: [PATCH] Part 3 of rest api --- hassio_api/hassio/api/__init__.py | 12 ++++++++++-- hassio_api/hassio/api/host.py | 13 ++++++++----- hassio_api/hassio/const.py | 4 ++++ hassio_api/hassio/core.py | 3 ++- hassio_api/hassio/host_controll.py | 2 +- hassio_api/setup.py | 5 +++-- 6 files changed, 28 insertions(+), 11 deletions(-) diff --git a/hassio_api/hassio/api/__init__.py b/hassio_api/hassio/api/__init__.py index a2bb2a4c5..432f5e9b7 100644 --- a/hassio_api/hassio/api/__init__.py +++ b/hassio_api/hassio/api/__init__.py @@ -3,8 +3,9 @@ import logging from aiohttp import web -from .homeassistant import APIHomeAssistant from .host import APIHost +from .supervisor import APISupervisor +from .homeassistant import APIHomeAssistant _LOGGER = logging.getLogger(__name__) @@ -28,12 +29,19 @@ class RestAPI(object): self.webapp.router.add_get('/host/info', api_host.info) self.webapp.router.add_get('/host/reboot', api_host.reboot) self.webapp.router.add_get('/host/shutdown', api_host.shutdown) - self.webapp.router.add_get('/host/update', api_host.update_host) + self.webapp.router.add_get('/host/update', api_host.update) self.webapp.router.add_get( '/host/network/info', api_host.network_info) self.webapp.router.add_get( '/host/network/update', api_host.network_update) + def registerSupervisor(self, host_controll): + """Register supervisor function.""" + api_supervisor = APISupervisor(self.config, self.loop, host_controll) + + self.webapp.router.add_get('/supervisor/info', api_supervisor.info) + self.webapp.router.add_get('/supervisor/update', api_supervisor.update) + def registerHomeAssistant(self, dock_homeassistant): """Register homeassistant function.""" api_hass = APIHomeAssistant(self.config, self.loop, dock_homeassistant) diff --git a/hassio_api/hassio/api/host.py b/hassio_api/hassio/api/host.py index 4e25d5adb..8a665d7bc 100644 --- a/hassio_api/hassio/api/host.py +++ b/hassio_api/hassio/api/host.py @@ -1,9 +1,11 @@ -"""Init file for HassIO rest api.""" +"""Init file for HassIO host rest api.""" import logging from aiohttp import web from aiohttp.web_exceptions import HTTPOk, HTTPMethodNotAllowed +from ..const import ATTR_VERSION + _LOGGER = logging.getLogger(__name__) @@ -11,10 +13,10 @@ class APIHost(object): """Handle rest api for host functions.""" def __init__(self, config, loop, host_controll): - """Initialize docker base wrapper.""" + """Initialize host rest api part.""" self.config = config self.loop = loop - self.host_controll + self.host_controll = host_controll async def info(self, request): """Return host information.""" @@ -43,8 +45,9 @@ class APIHost(object): """Edit network settings.""" raise HTTPMethodNotAllowed() - async def update_host(self, request): + async def update(self, request): """Update host OS.""" - if await self.host_controll.host_update(): + body = await request.json() or {} + if await self.host_controll.host_update(body.get(ATTR_VERSION)): raise HTTPOk() raise HTTPMethodNotAllowed() diff --git a/hassio_api/hassio/const.py b/hassio_api/hassio/const.py index 97239ae84..f66e40223 100644 --- a/hassio_api/hassio/const.py +++ b/hassio_api/hassio/const.py @@ -1,6 +1,8 @@ """Const file for HassIO.""" import os +HASSIO_VERSION = '0.3' + URL_HASSIO_VERSION = \ 'https://raw.githubusercontent.com/pvizeli/hassio/master/version.json' @@ -25,3 +27,5 @@ HTTP_PORT = 9123 HOMEASSISTANT_IMAGE = 'homeassistant_image' HOMEASSISTANT_TAG = 'homeassistant_tag' + +ATTR_VERSION = 'version' diff --git a/hassio_api/hassio/core.py b/hassio_api/hassio/core.py index 044274acd..05dfebb43 100644 --- a/hassio_api/hassio/core.py +++ b/hassio_api/hassio/core.py @@ -53,8 +53,9 @@ class HassIO(object): host_info.get('version'), host_info.get('hostname'), host_info.get('level')) - # api views + # rest api views self.api.registerHost(self.host_controll) + self.api.registerSupervisor(self.host_controll) self.api.registerHomeAssistant(self.homeassistant) # first start of supervisor? diff --git a/hassio_api/hassio/host_controll.py b/hassio_api/hassio/host_controll.py index 96ee4035d..55dda28aa 100644 --- a/hassio_api/hassio/host_controll.py +++ b/hassio_api/hassio/host_controll.py @@ -51,7 +51,7 @@ class HostControll(object): data = await reader.readline() response = data.decode() - _LOGGER.info("Receive from HostControll: %s.", response) + _LOGGER.debug("Receive from HostControll: %s.", response) if response == "OK": return True diff --git a/hassio_api/setup.py b/hassio_api/setup.py index 508fb60b1..b90ec8344 100644 --- a/hassio_api/setup.py +++ b/hassio_api/setup.py @@ -1,10 +1,11 @@ from setuptools import setup -VERSION = "0.2" +from .hassio.const import HASSIO_VERSION + setup( name='HassIO', - version=VERSION, + version=HASSIO_VERSION, license='BSD License', author='The Home Assistant Authors', author_email='hello@home-assistant.io',