mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-21 08:06:30 +00:00
commit
fe4e1a1933
@ -23,7 +23,7 @@ if __name__ == "__main__":
|
|||||||
loop.run_until_complete(hassio.setup())
|
loop.run_until_complete(hassio.setup())
|
||||||
|
|
||||||
_LOGGER.info("Start Hassio task")
|
_LOGGER.info("Start Hassio task")
|
||||||
loop.create_task(hassio.start())
|
loop.call_soon_threadsafe(asyncio.ensure_future, hassio.start(), loop)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
loop.add_signal_handler(
|
loop.add_signal_handler(
|
||||||
@ -32,4 +32,5 @@ if __name__ == "__main__":
|
|||||||
_LOGGER.warning("Could not bind to SIGTERM")
|
_LOGGER.warning("Could not bind to SIGTERM")
|
||||||
|
|
||||||
loop.run_forever()
|
loop.run_forever()
|
||||||
|
loop.close()
|
||||||
_LOGGER.info("Close Hassio")
|
_LOGGER.info("Close Hassio")
|
||||||
|
@ -44,6 +44,7 @@ class RestAPI(object):
|
|||||||
"""Register supervisor function."""
|
"""Register supervisor function."""
|
||||||
api_supervisor = APISupervisor(self.config, self.loop, host_controll)
|
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/info', api_supervisor.info)
|
||||||
self.webapp.router.add_get('/supervisor/update', api_supervisor.update)
|
self.webapp.router.add_get('/supervisor/update', api_supervisor.update)
|
||||||
self.webapp.router.add_get(
|
self.webapp.router.add_get(
|
||||||
|
@ -2,11 +2,17 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from .util import api_process, json_loads
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from .util import api_process, api_validate
|
||||||
from ..const import ATTR_VERSION, ATTR_CURRENT
|
from ..const import ATTR_VERSION, ATTR_CURRENT
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
SCHEMA_VERSION = vol.Schema({
|
||||||
|
vol.Optional(ATTR_VERSION): vol.Coerce(str),
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
class APIHomeAssistant(object):
|
class APIHomeAssistant(object):
|
||||||
"""Handle rest api for homeassistant functions."""
|
"""Handle rest api for homeassistant functions."""
|
||||||
@ -30,7 +36,7 @@ class APIHomeAssistant(object):
|
|||||||
@api_process
|
@api_process
|
||||||
async def update(self, request):
|
async def update(self, request):
|
||||||
"""Update host OS."""
|
"""Update host OS."""
|
||||||
body = await request.json(loads=json_loads)
|
body = await api_validate(SCHEMA_VERSION, request)
|
||||||
version = body.get(ATTR_VERSION, self.config.current_homeassistant)
|
version = body.get(ATTR_VERSION, self.config.current_homeassistant)
|
||||||
|
|
||||||
if self.dock_hass.in_progress:
|
if self.dock_hass.in_progress:
|
||||||
|
@ -1,11 +1,19 @@
|
|||||||
"""Init file for HassIO host rest api."""
|
"""Init file for HassIO host rest api."""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from .util import api_process_hostcontroll, json_loads
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from .util import api_process_hostcontroll, api_process, api_validate
|
||||||
from ..const import ATTR_VERSION
|
from ..const import ATTR_VERSION
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
UNKNOWN = 'unknown'
|
||||||
|
|
||||||
|
SCHEMA_VERSION = vol.Schema({
|
||||||
|
vol.Optional(ATTR_VERSION): vol.Coerce(str),
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
class APIHost(object):
|
class APIHost(object):
|
||||||
"""Handle rest api for host functions."""
|
"""Handle rest api for host functions."""
|
||||||
@ -16,10 +24,20 @@ class APIHost(object):
|
|||||||
self.loop = loop
|
self.loop = loop
|
||||||
self.host_controll = host_controll
|
self.host_controll = host_controll
|
||||||
|
|
||||||
@api_process_hostcontroll
|
@api_process
|
||||||
def info(self, request):
|
async def info(self, request):
|
||||||
"""Return host information."""
|
"""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
|
@api_process_hostcontroll
|
||||||
def reboot(self, request):
|
def reboot(self, request):
|
||||||
@ -31,20 +49,10 @@ class APIHost(object):
|
|||||||
"""Poweroff host."""
|
"""Poweroff host."""
|
||||||
return self.host_controll.shutdown()
|
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
|
@api_process_hostcontroll
|
||||||
async def update(self, request):
|
async def update(self, request):
|
||||||
"""Update host OS."""
|
"""Update host OS."""
|
||||||
body = await request.json(loads=json_loads)
|
body = await api_validate(SCHEMA_VERSION, request)
|
||||||
version = body.get(ATTR_VERSION)
|
version = body.get(ATTR_VERSION)
|
||||||
|
|
||||||
if version == self.host_controll.version:
|
if version == self.host_controll.version:
|
||||||
|
@ -1,11 +1,22 @@
|
|||||||
"""Init file for HassIO supervisor rest api."""
|
"""Init file for HassIO supervisor rest api."""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from .util import api_process, api_process_hostcontroll, json_loads
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from .util import api_process, api_process_hostcontroll, api_validate
|
||||||
from ..const import ATTR_VERSION, ATTR_CURRENT, ATTR_BETA, HASSIO_VERSION
|
from ..const import ATTR_VERSION, ATTR_CURRENT, ATTR_BETA, HASSIO_VERSION
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
SCHEMA_OPTIONS = vol.Schema({
|
||||||
|
# pylint: disable=no-value-for-parameter
|
||||||
|
vol.Optional(ATTR_BETA): vol.Boolean(),
|
||||||
|
})
|
||||||
|
|
||||||
|
SCHEMA_VERSION = vol.Schema({
|
||||||
|
vol.Optional(ATTR_VERSION): vol.Coerce(str),
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
class APISupervisor(object):
|
class APISupervisor(object):
|
||||||
"""Handle rest api for supervisor functions."""
|
"""Handle rest api for supervisor functions."""
|
||||||
@ -16,6 +27,11 @@ class APISupervisor(object):
|
|||||||
self.loop = loop
|
self.loop = loop
|
||||||
self.host_controll = host_controll
|
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
|
@api_process
|
||||||
async def info(self, request):
|
async def info(self, request):
|
||||||
"""Return host information."""
|
"""Return host information."""
|
||||||
@ -30,7 +46,7 @@ class APISupervisor(object):
|
|||||||
@api_process
|
@api_process
|
||||||
async def options(self, request):
|
async def options(self, request):
|
||||||
"""Set supervisor options."""
|
"""Set supervisor options."""
|
||||||
body = await request.json(loads=json_loads)
|
body = await api_validate(SCHEMA_OPTIONS, request)
|
||||||
|
|
||||||
if ATTR_BETA in body:
|
if ATTR_BETA in body:
|
||||||
self.config.upstream_beta = body[ATTR_BETA]
|
self.config.upstream_beta = body[ATTR_BETA]
|
||||||
@ -40,7 +56,7 @@ class APISupervisor(object):
|
|||||||
@api_process_hostcontroll
|
@api_process_hostcontroll
|
||||||
async def update(self, request):
|
async def update(self, request):
|
||||||
"""Update host OS."""
|
"""Update host OS."""
|
||||||
body = await request.json(loads=json_loads)
|
body = await api_validate(SCHEMA_VERSION, request)
|
||||||
version = body.get(ATTR_VERSION, self.config.current_hassio)
|
version = body.get(ATTR_VERSION, self.config.current_hassio)
|
||||||
|
|
||||||
if version == HASSIO_VERSION:
|
if version == HASSIO_VERSION:
|
||||||
|
@ -4,6 +4,8 @@ import logging
|
|||||||
|
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
from aiohttp.web_exceptions import HTTPServiceUnavailable
|
from aiohttp.web_exceptions import HTTPServiceUnavailable
|
||||||
|
import voluptuous as vol
|
||||||
|
from voluptuous.humanize import humanize_error
|
||||||
|
|
||||||
from ..const import (
|
from ..const import (
|
||||||
JSON_RESULT, JSON_DATA, JSON_MESSAGE, RESULT_OK, RESULT_ERROR)
|
JSON_RESULT, JSON_DATA, JSON_MESSAGE, RESULT_OK, RESULT_ERROR)
|
||||||
@ -52,7 +54,7 @@ def api_process_hostcontroll(method):
|
|||||||
if isinstance(answer, dict):
|
if isinstance(answer, dict):
|
||||||
return api_return_ok(data=answer)
|
return api_return_ok(data=answer)
|
||||||
elif answer is None:
|
elif answer is None:
|
||||||
return api_not_supported()
|
return api_return_error("Function is not supported")
|
||||||
elif answer:
|
elif answer:
|
||||||
return api_return_ok()
|
return api_return_ok()
|
||||||
return api_return_error()
|
return api_return_error()
|
||||||
@ -72,10 +74,16 @@ def api_return_ok(data=None):
|
|||||||
"""Return a API ok answer."""
|
"""Return a API ok answer."""
|
||||||
return web.json_response({
|
return web.json_response({
|
||||||
JSON_RESULT: RESULT_OK,
|
JSON_RESULT: RESULT_OK,
|
||||||
JSON_DATA: data,
|
JSON_DATA: data or {},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
def api_not_supported():
|
async def api_validate(schema, request):
|
||||||
"""Return a api error with not supported."""
|
"""Validate request data with schema."""
|
||||||
return api_return_error("Function is not supported")
|
data = await request.json(loads=json_loads)
|
||||||
|
try:
|
||||||
|
schema(data)
|
||||||
|
except vol.Invalid as ex:
|
||||||
|
raise RuntimeError(humanize_error(data, ex)) from None
|
||||||
|
|
||||||
|
return data
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
"""Const file for HassIO."""
|
"""Const file for HassIO."""
|
||||||
HASSIO_VERSION = '0.4'
|
HASSIO_VERSION = '0.5'
|
||||||
|
|
||||||
URL_HASSIO_VERSION = \
|
URL_HASSIO_VERSION = \
|
||||||
'https://raw.githubusercontent.com/pvizeli/hassio/master/version.json'
|
'https://raw.githubusercontent.com/pvizeli/hassio/master/version.json'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user