diff --git a/hassio_api/README.md b/hassio_api/README.md index 4ee771d3c..43e434d37 100644 --- a/hassio_api/README.md +++ b/hassio_api/README.md @@ -70,9 +70,12 @@ On success ``` - `/supervisor/update` -Payload: {'version': '0.XX'} +Payload: {"version": "0.XX"} If version is None it read last version from server. +- `/supervisor/option` +Payload: {"beta": true|false} + ### Host - `/host/shutdown` @@ -103,7 +106,7 @@ Payload: {'hostname': '', 'mode': 'dhcp|fixed', 'ssid': '', 'ip': '', 'netmask': ``` - `/homeassistant/update` -Payload: {'version': '0.XX.Y'} +Payload: {"version": "0.XX.Y"} If version is None it read last version from server. ### REST API addons @@ -118,10 +121,10 @@ Payload: {'options': {}} - `/addons/{addon}/stop` - `/addons/{addon}/install` -Payload: {'version': 'x.x'} +Payload: {"version": "x.x"} - `/addons/{addon}/uninstall` - `/addons/{addon}/update` -Payload: {'version': 'x.x'} +Payload: {"version": "x.x"} If version is None it read last version from server. diff --git a/hassio_api/hassio/api/__init__.py b/hassio_api/hassio/api/__init__.py index 0191f300b..d4f842e33 100644 --- a/hassio_api/hassio/api/__init__.py +++ b/hassio_api/hassio/api/__init__.py @@ -42,6 +42,8 @@ class RestAPI(object): 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/options', api_supervisor.options) def register_homeassistant(self, dock_homeassistant): """Register homeassistant function.""" diff --git a/hassio_api/hassio/api/supervisor.py b/hassio_api/hassio/api/supervisor.py index a5f98ec4a..698f6a294 100644 --- a/hassio_api/hassio/api/supervisor.py +++ b/hassio_api/hassio/api/supervisor.py @@ -2,7 +2,7 @@ import logging from .util import api_process, api_process_hostcontroll, json_loads -from ..const import ATTR_VERSION, ATTR_CURRENT, HASSIO_VERSION +from ..const import ATTR_VERSION, ATTR_CURRENT, ATTR_BETA, HASSIO_VERSION _LOGGER = logging.getLogger(__name__) @@ -22,10 +22,22 @@ class APISupervisor(object): info = { ATTR_VERSION: HASSIO_VERSION, ATTR_CURRENT: self.config.current_hassio, + ATTR_BETA: self.config.upstream_beta, } return info + @api_process + async def options(self, request): + """Set supervisor options.""" + update = False + body = await request.json(loads=json_loads) + + if ATTR_BETA in body: + self.config.upstream_beta = body[ATTR_BETA] + + return self.config.save() + @api_process_hostcontroll async def update(self, request): """Update host OS.""" diff --git a/hassio_api/hassio/config.py b/hassio_api/hassio/config.py index b9a1bb3e6..d7295580c 100644 --- a/hassio_api/hassio/config.py +++ b/hassio_api/hassio/config.py @@ -14,6 +14,7 @@ HOMEASSISTANT_IMAGE = 'homeassistant_image' HOMEASSISTANT_CURRENT = 'homeassistant_current' HASSIO_CURRENT = 'hassio_current' +UPSTREAM_BETA = 'upstream_beta' class CoreConfig(object): @@ -37,7 +38,9 @@ class CoreConfig(object): if not self._data: self._data.update({ HOMEASSISTANT_IMAGE: os.environ['HOMEASSISTANT_REPOSITORY'], + UPSTREAM_BETA: False, }) + self.save() def save(self): """Store data to config file.""" @@ -46,10 +49,14 @@ class CoreConfig(object): conf_file.write(json.dumps(self._data)) except OSError: _LOGGER.exception("Can't store config in %s", self._filename) + return False + + return True async def fetch_update_infos(self): """Read current versions from web.""" - current = await fetch_current_versions(self.websession) + current = await fetch_current_versions( + self.websession, beta=self.upstream_beta) if current: self._data.update({ @@ -61,6 +68,16 @@ class CoreConfig(object): return False + @property + def upstream_beta(self): + """Return True if we run in beta upstream.""" + return self._data.get(UPSTREAM_BETA, False) + + @upstream_beta.setter + def upstream_beta(self, value): + """Set beta upstream mode.""" + self._data[UPSTREAM_BETA] = bool(value) + @property def homeassistant_image(self): """Return docker homeassistant repository.""" diff --git a/hassio_api/hassio/const.py b/hassio_api/hassio/const.py index d997347a5..3e14616cc 100644 --- a/hassio_api/hassio/const.py +++ b/hassio_api/hassio/const.py @@ -3,6 +3,8 @@ HASSIO_VERSION = '0.3' URL_HASSIO_VERSION = \ 'https://raw.githubusercontent.com/pvizeli/hassio/master/version.json' +URL_HASSIO_VERSION_BETA = \ + 'https://raw.githubusercontent.com/pvizeli/hassio/master/version_beta.json' URL_ADDONS_REPO = 'https://github.com/pvizeli/hassio-addons' @@ -25,3 +27,4 @@ RESULT_OK = 'ok' ATTR_VERSION = 'version' ATTR_CURRENT = 'current' +ATTR_BETA = 'beta' diff --git a/hassio_api/hassio/tools.py b/hassio_api/hassio/tools.py index e12f4ca96..02aab37b0 100644 --- a/hassio_api/hassio/tools.py +++ b/hassio_api/hassio/tools.py @@ -7,25 +7,26 @@ import socket import aiohttp import async_timeout -from .const import URL_HASSIO_VERSION +from .const import URL_HASSIO_VERSION, URL_HASSIO_VERSION_BETA _LOGGER = logging.getLogger(__name__) _RE_VERSION = re.compile(r"VERSION=(.*)") -async def fetch_current_versions(websession): +async def fetch_current_versions(websession, beta=False): """Fetch current versions from github. Is a coroutine. """ + url = URL_HASSIO_VERSION_BETA if beta or URL_HASSIO_VERSION try: with async_timeout.timeout(10, loop=websession.loop): - async with websession.get(URL_HASSIO_VERSION) as request: + async with websession.get(url) as request: return await request.json(content_type=None) except (ValueError, aiohttp.ClientError, asyncio.TimeoutError) as err: - _LOGGER.warning("Can't fetch versions from github! %s", err) + _LOGGER.warning("Can't fetch versions from %s! %s", url, err) def get_version_from_env(env_list):