Skip to content

Commit fe4e1a1

Browse files
authored
Merge pull request #2 from pvizeli/Release-0_5
Release 0.5
2 parents ce01e53 + 8c191e8 commit fe4e1a1

File tree

8 files changed

+68
-27
lines changed

8 files changed

+68
-27
lines changed

hassio/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
loop.run_until_complete(hassio.setup())
2424

2525
_LOGGER.info("Start Hassio task")
26-
loop.create_task(hassio.start())
26+
loop.call_soon_threadsafe(asyncio.ensure_future, hassio.start(), loop)
2727

2828
try:
2929
loop.add_signal_handler(
@@ -32,4 +32,5 @@
3232
_LOGGER.warning("Could not bind to SIGTERM")
3333

3434
loop.run_forever()
35+
loop.close()
3536
_LOGGER.info("Close Hassio")

hassio/api/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def register_supervisor(self, host_controll):
4444
"""Register supervisor function."""
4545
api_supervisor = APISupervisor(self.config, self.loop, host_controll)
4646

47+
self.webapp.router.add_get('/supervisor/ping', api_supervisor.ping)
4748
self.webapp.router.add_get('/supervisor/info', api_supervisor.info)
4849
self.webapp.router.add_get('/supervisor/update', api_supervisor.update)
4950
self.webapp.router.add_get(

hassio/api/homeassistant.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22
import asyncio
33
import logging
44

5-
from .util import api_process, json_loads
5+
import voluptuous as vol
6+
7+
from .util import api_process, api_validate
68
from ..const import ATTR_VERSION, ATTR_CURRENT
79

810
_LOGGER = logging.getLogger(__name__)
911

12+
SCHEMA_VERSION = vol.Schema({
13+
vol.Optional(ATTR_VERSION): vol.Coerce(str),
14+
})
15+
1016

1117
class APIHomeAssistant(object):
1218
"""Handle rest api for homeassistant functions."""
@@ -30,7 +36,7 @@ async def info(self, request):
3036
@api_process
3137
async def update(self, request):
3238
"""Update host OS."""
33-
body = await request.json(loads=json_loads)
39+
body = await api_validate(SCHEMA_VERSION, request)
3440
version = body.get(ATTR_VERSION, self.config.current_homeassistant)
3541

3642
if self.dock_hass.in_progress:

hassio/api/host.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
"""Init file for HassIO host rest api."""
22
import logging
33

4-
from .util import api_process_hostcontroll, json_loads
4+
import voluptuous as vol
5+
6+
from .util import api_process_hostcontroll, api_process, api_validate
57
from ..const import ATTR_VERSION
68

79
_LOGGER = logging.getLogger(__name__)
810

11+
UNKNOWN = 'unknown'
12+
13+
SCHEMA_VERSION = vol.Schema({
14+
vol.Optional(ATTR_VERSION): vol.Coerce(str),
15+
})
16+
917

1018
class APIHost(object):
1119
"""Handle rest api for host functions."""
@@ -16,10 +24,20 @@ def __init__(self, config, loop, host_controll):
1624
self.loop = loop
1725
self.host_controll = host_controll
1826

19-
@api_process_hostcontroll
20-
def info(self, request):
27+
@api_process
28+
async def info(self, request):
2129
"""Return host information."""
22-
return self.host_controll.info()
30+
if not self.host_controll.active:
31+
info = {
32+
'os': UNKNOWN,
33+
'version': UNKNOWN,
34+
'current': UNKNOWN,
35+
'level': 0,
36+
'hostname': UNKNOWN,
37+
}
38+
return info
39+
40+
return await self.host_controll.info()
2341

2442
@api_process_hostcontroll
2543
def reboot(self, request):
@@ -31,20 +49,10 @@ def shutdown(self, request):
3149
"""Poweroff host."""
3250
return self.host_controll.shutdown()
3351

34-
@api_process_hostcontroll
35-
def network_info(self, request):
36-
"""Edit network settings."""
37-
pass
38-
39-
@api_process_hostcontroll
40-
def network_update(self, request):
41-
"""Edit network settings."""
42-
pass
43-
4452
@api_process_hostcontroll
4553
async def update(self, request):
4654
"""Update host OS."""
47-
body = await request.json(loads=json_loads)
55+
body = await api_validate(SCHEMA_VERSION, request)
4856
version = body.get(ATTR_VERSION)
4957

5058
if version == self.host_controll.version:

hassio/api/supervisor.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
"""Init file for HassIO supervisor rest api."""
22
import logging
33

4-
from .util import api_process, api_process_hostcontroll, json_loads
4+
import voluptuous as vol
5+
6+
from .util import api_process, api_process_hostcontroll, api_validate
57
from ..const import ATTR_VERSION, ATTR_CURRENT, ATTR_BETA, HASSIO_VERSION
68

79
_LOGGER = logging.getLogger(__name__)
810

11+
SCHEMA_OPTIONS = vol.Schema({
12+
# pylint: disable=no-value-for-parameter
13+
vol.Optional(ATTR_BETA): vol.Boolean(),
14+
})
15+
16+
SCHEMA_VERSION = vol.Schema({
17+
vol.Optional(ATTR_VERSION): vol.Coerce(str),
18+
})
19+
920

1021
class APISupervisor(object):
1122
"""Handle rest api for supervisor functions."""
@@ -16,6 +27,11 @@ def __init__(self, config, loop, host_controll):
1627
self.loop = loop
1728
self.host_controll = host_controll
1829

30+
@api_process
31+
async def ping(self, request):
32+
"""Return ok for signal that the api is ready."""
33+
return True
34+
1935
@api_process
2036
async def info(self, request):
2137
"""Return host information."""
@@ -30,7 +46,7 @@ async def info(self, request):
3046
@api_process
3147
async def options(self, request):
3248
"""Set supervisor options."""
33-
body = await request.json(loads=json_loads)
49+
body = await api_validate(SCHEMA_OPTIONS, request)
3450

3551
if ATTR_BETA in body:
3652
self.config.upstream_beta = body[ATTR_BETA]
@@ -40,7 +56,7 @@ async def options(self, request):
4056
@api_process_hostcontroll
4157
async def update(self, request):
4258
"""Update host OS."""
43-
body = await request.json(loads=json_loads)
59+
body = await api_validate(SCHEMA_VERSION, request)
4460
version = body.get(ATTR_VERSION, self.config.current_hassio)
4561

4662
if version == HASSIO_VERSION:

hassio/api/util.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from aiohttp import web
66
from aiohttp.web_exceptions import HTTPServiceUnavailable
7+
import voluptuous as vol
8+
from voluptuous.humanize import humanize_error
79

810
from ..const import (
911
JSON_RESULT, JSON_DATA, JSON_MESSAGE, RESULT_OK, RESULT_ERROR)
@@ -52,7 +54,7 @@ async def wrap_hostcontroll(api, *args, **kwargs):
5254
if isinstance(answer, dict):
5355
return api_return_ok(data=answer)
5456
elif answer is None:
55-
return api_not_supported()
57+
return api_return_error("Function is not supported")
5658
elif answer:
5759
return api_return_ok()
5860
return api_return_error()
@@ -72,10 +74,16 @@ def api_return_ok(data=None):
7274
"""Return a API ok answer."""
7375
return web.json_response({
7476
JSON_RESULT: RESULT_OK,
75-
JSON_DATA: data,
77+
JSON_DATA: data or {},
7678
})
7779

7880

79-
def api_not_supported():
80-
"""Return a api error with not supported."""
81-
return api_return_error("Function is not supported")
81+
async def api_validate(schema, request):
82+
"""Validate request data with schema."""
83+
data = await request.json(loads=json_loads)
84+
try:
85+
schema(data)
86+
except vol.Invalid as ex:
87+
raise RuntimeError(humanize_error(data, ex)) from None
88+
89+
return data

hassio/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Const file for HassIO."""
2-
HASSIO_VERSION = '0.4'
2+
HASSIO_VERSION = '0.5'
33

44
URL_HASSIO_VERSION = \
55
'https://raw.githubusercontent.com/pvizeli/hassio/master/version.json'

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@
3636
'aiohttp',
3737
'docker',
3838
'colorlog',
39+
'voluptuous',
3940
]
4041
)

0 commit comments

Comments
 (0)