mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-23 09:06:29 +00:00
Cleanup api code
This commit is contained in:
parent
d8ab85986d
commit
0236611676
@ -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.
|
||||
|
@ -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))
|
||||
|
@ -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))
|
||||
|
@ -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")
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user