mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-15 13:16:29 +00:00
Update api code
This commit is contained in:
parent
ce920d3e5e
commit
2efa03450e
@ -19,6 +19,7 @@ class RestAPI(object):
|
||||
self.loop = loop
|
||||
self.webapp = web.Application(loop=self.loop)
|
||||
|
||||
# service stuff
|
||||
self._handler = None
|
||||
self.server = None
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
"""Init file for HassIO homeassistant rest api."""
|
||||
import logging
|
||||
|
||||
from aiohttp import web
|
||||
from aiohttp.web_exceptions import HTTPNotAcceptable
|
||||
from aiohttp.web_exceptions import HTTPServiceUnavailable
|
||||
|
||||
from .util import api_return_ok
|
||||
from ..const import ATTR_VERSION
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -20,10 +20,10 @@ class APIHomeAssistant(object):
|
||||
|
||||
async def info(self, request):
|
||||
"""Return host information."""
|
||||
return web.json_response({
|
||||
return api_return_ok({
|
||||
ATTR_VERSION: self.dock_hass.version,
|
||||
})
|
||||
|
||||
async def update(self, request):
|
||||
"""Update host OS."""
|
||||
raise HTTPNotAcceptable()
|
||||
raise HTTPServiceUnavailable()
|
||||
|
@ -2,8 +2,9 @@
|
||||
import logging
|
||||
|
||||
from aiohttp import web
|
||||
from aiohttp.web_exceptions import HTTPOk, HTTPNotAcceptable
|
||||
from aiohttp.web_exceptions import HTTPServiceUnavailable
|
||||
|
||||
from .util import api_return_ok, api_return_not_supported
|
||||
from ..const import ATTR_VERSION
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -20,34 +21,52 @@ class APIHost(object):
|
||||
|
||||
async def info(self, request):
|
||||
"""Return host information."""
|
||||
if not self.host_controll.active:
|
||||
raise HTTPServiceUnavailable()
|
||||
|
||||
host_info = await self.host_controll.info()
|
||||
if host_info:
|
||||
return web.json_response(host_info)
|
||||
raise HTTPNotAcceptable()
|
||||
return api_return_not_supported()
|
||||
|
||||
async def reboot(self, request):
|
||||
"""Reboot host."""
|
||||
if not self.host_controll.active:
|
||||
raise HTTPServiceUnavailable()
|
||||
|
||||
if await self.host_controll.reboot():
|
||||
raise HTTPOk()
|
||||
raise HTTPNotAcceptable()
|
||||
return api_return_ok()
|
||||
return api_return_not_supported()
|
||||
|
||||
async def shutdown(self, request):
|
||||
"""Poweroff host."""
|
||||
if not self.host_controll.active:
|
||||
raise HTTPServiceUnavailable()
|
||||
|
||||
if await self.host_controll.shutdown():
|
||||
raise HTTPOk()
|
||||
raise HTTPNotAcceptable()
|
||||
return api_return_ok()
|
||||
return api_return_not_supported()
|
||||
|
||||
async def network_info(self, request):
|
||||
"""Edit network settings."""
|
||||
raise HTTPNotAcceptable()
|
||||
if not self.host_controll.active:
|
||||
raise HTTPServiceUnavailable()
|
||||
|
||||
return api_return_not_supported()
|
||||
|
||||
async def network_update(self, request):
|
||||
"""Edit network settings."""
|
||||
raise HTTPNotAcceptable()
|
||||
if not self.host_controll.active:
|
||||
raise HTTPServiceUnavailable()
|
||||
|
||||
return api_return_not_supported()
|
||||
|
||||
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)):
|
||||
raise HTTPOk()
|
||||
raise HTTPNotAcceptable()
|
||||
return api_return_ok()
|
||||
return api_return_not_supported()
|
||||
|
@ -1,9 +1,9 @@
|
||||
"""Init file for HassIO supervisor rest api."""
|
||||
import logging
|
||||
|
||||
from aiohttp import web
|
||||
from aiohttp.web_exceptions import HTTPOk, HTTPNotAcceptable
|
||||
from aiohttp.web_exceptions import HTTPServiceUnavailable
|
||||
|
||||
from .util import api_return_ok, api_return_not_supported
|
||||
from ..const import ATTR_VERSION, HASSIO_VERSION
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -20,13 +20,16 @@ class APISupervisor(object):
|
||||
|
||||
async def info(self, request):
|
||||
"""Return host information."""
|
||||
return web.json_response({
|
||||
return api_return_ok({
|
||||
ATTR_VERSION: HASSIO_VERSION,
|
||||
})
|
||||
|
||||
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)):
|
||||
raise HTTPOk()
|
||||
raise HTTPNotAcceptable()
|
||||
return api_return_ok()
|
||||
return api_return_not_supported()
|
||||
|
30
hassio_api/hassio/api/util.py
Normal file
30
hassio_api/hassio/api/util.py
Normal file
@ -0,0 +1,30 @@
|
||||
"""Init file for HassIO util for rest api."""
|
||||
import logging
|
||||
|
||||
from aiohttp import web
|
||||
|
||||
from ..const import (
|
||||
JSON_RESULT, JSON_DATA, JSON_MESSAGE, RESULT_OK, RESULT_ERROR)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def api_return_error(message=None):
|
||||
"""Return a API error message."""
|
||||
return web.json_response({
|
||||
JSON_RESULT: RESULT_ERROR,
|
||||
JSON_MESSAGE: message,
|
||||
})
|
||||
|
||||
|
||||
def api_return_ok(data=None):
|
||||
"""Return a API ok answer."""
|
||||
return web.json_response({
|
||||
JSON_RESULT: RESULT_OK,
|
||||
JSON_DATA: data,
|
||||
})
|
||||
|
||||
|
||||
def api_return_not_supported():
|
||||
"""Return a api error with not supported."""
|
||||
return api_return_error("Function is not supported")
|
@ -24,4 +24,13 @@ HTTP_PORT = 9123
|
||||
HOMEASSISTANT_IMAGE = 'homeassistant_image'
|
||||
HOMEASSISTANT_TAG = 'homeassistant_tag'
|
||||
|
||||
JSON_RESULT = 'result'
|
||||
JSON_DATA = 'data'
|
||||
JSON_MESSAGE = 'message'
|
||||
|
||||
RESULT_ERROR = 'error'
|
||||
RESULT_OK = 'ok'
|
||||
|
||||
ATTR_VERSION = 'version'
|
||||
ATTR_NEED_UPDATE = 'need_update'
|
||||
ATTR_NEXT_VERSION = 'next_version'
|
||||
|
Loading…
x
Reference in New Issue
Block a user