Skip to content

Commit

Permalink
Merge pull request #97 from home-assistant/dev
Browse files Browse the repository at this point in the history
Release 0.43
  • Loading branch information
pvizeli authored Jul 12, 2017
2 parents 7e5b267 + 253962d commit bfa7443
Show file tree
Hide file tree
Showing 20 changed files with 428 additions and 211 deletions.
92 changes: 55 additions & 37 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,48 +51,13 @@ The addons from `addons` are only installed one.
],
"addons_repositories": [
"REPO_URL"
],
"snapshots": [
{
"slug": "SLUG",
"data": "ISO",
"name": "Custom name"
}
]
}
```

- GET `/supervisor/addons`

Get all available addons

```json
{
"addons": [
{
"name": "xy bla",
"slug": "xy",
"description": "description",
"arch": ["armhf", "aarch64", "i386", "amd64"],
"repository": "core|local|REP_ID",
"version": "LAST_VERSION",
"installed": "none|INSTALL_VERSION",
"detached": "bool",
"build": "bool",
"url": "null|url"
}
],
"repositories": [
{
"slug": "12345678",
"name": "Repitory Name|unknown",
"source": "URL_OF_REPOSITORY",
"url": "WEBSITE|REPOSITORY",
"maintainer": "BLA BLU <[email protected]>|unknown"
}
]
}
```
Get all available addons. Will be delete soon. Look to `/addons`

- POST `/supervisor/update`
Optional:
Expand Down Expand Up @@ -157,6 +122,21 @@ Return QR-Code

### Backup/Snapshot

- GET `/snapshots`
```json
{
"snapshots": [
{
"slug": "SLUG",
"date": "ISO",
"name": "Custom name"
}
]
}
```

- POST `/snapshots/reload`

- POST `/snapshots/new/full`
```json
{
Expand Down Expand Up @@ -269,7 +249,9 @@ Optional:
{
"version": "INSTALL_VERSION",
"last_version": "LAST_VERSION",
"devices": []
"devices": [""],
"image": "str",
"custom": "bool -> if custom image"
}
```

Expand All @@ -291,11 +273,47 @@ Output the raw docker log
```json
{
"devices": [],
"image": "Optional|null",
"last_version": "Optional for custom image|null"
}
```

Image with `null` and last_version with `null` reset this options.

### REST API addons

- GET `/addons`

Get all available addons

```json
{
"addons": [
{
"name": "xy bla",
"slug": "xy",
"description": "description",
"arch": ["armhf", "aarch64", "i386", "amd64"],
"repository": "core|local|REP_ID",
"version": "LAST_VERSION",
"installed": "none|INSTALL_VERSION",
"detached": "bool",
"build": "bool",
"url": "null|url"
}
],
"repositories": [
{
"slug": "12345678",
"name": "Repitory Name|unknown",
"source": "URL_OF_REPOSITORY",
"url": "WEBSITE|REPOSITORY",
"maintainer": "BLA BLU <[email protected]>|unknown"
}
]
}
```

- POST `/addons/reload`

- GET `/addons/{addon}/info`
Expand Down
39 changes: 4 additions & 35 deletions hassio/addons/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,22 @@
from ..const import (
FILE_HASSIO_ADDONS, ATTR_VERSION, ATTR_SLUG, ATTR_REPOSITORY, ATTR_LOCATON,
REPOSITORY_CORE, REPOSITORY_LOCAL, ATTR_USER, ATTR_SYSTEM)
from ..tools import read_json_file, write_json_file
from ..tools import JsonConfig, read_json_file

_LOGGER = logging.getLogger(__name__)

RE_VOLUME = re.compile(MAP_VOLUME)


class Data(object):
class Data(JsonConfig):
"""Hold data for addons inside HassIO."""

def __init__(self, config):
"""Initialize data holder."""
self._file = FILE_HASSIO_ADDONS
self._data = {}
super().__init__(FILE_HASSIO_ADDONS, SCHEMA_ADDON_FILE)
self.config = config
self._cache = {}
self._repositories = {}

# init or load data
if self._file.is_file():
try:
self._data = read_json_file(self._file)
except (OSError, json.JSONDecodeError):
_LOGGER.warning("Can't read %s", self._file)
self._data = {}

# validate
try:
self._data = SCHEMA_ADDON_FILE(self._data)
except vol.Invalid as ex:
_LOGGER.error("Can't parse addons.json -> %s",
humanize_error(self._data, ex))

def save(self):
"""Store data to config file."""
# validate
try:
self._data = SCHEMA_ADDON_FILE(self._data)
except vol.Invalid as ex:
_LOGGER.error("Can't parse addons data -> %s",
humanize_error(self._data, ex))
return False

if not write_json_file(self._file, self._data):
_LOGGER.error("Can't store config in %s", self._file)
return False
return True
self._cache = {}

@property
def user(self):
Expand Down
6 changes: 6 additions & 0 deletions hassio/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def register_addons(self, addons):
"""Register homeassistant function."""
api_addons = APIAddons(self.config, self.loop, addons)

self.webapp.router.add_get('/addons', api_addons.list)
self.webapp.router.add_post('/addons/reload', api_addons.reload)

self.webapp.router.add_get('/addons/{addon}/info', api_addons.info)
self.webapp.router.add_post(
'/addons/{addon}/install', api_addons.install)
Expand Down Expand Up @@ -105,6 +108,9 @@ def register_snapshots(self, snapshots):
"""Register snapshots function."""
api_snapshots = APISnapshots(self.config, self.loop, snapshots)

self.webapp.router.add_get('/snapshots', api_snapshots.list)
self.webapp.router.add_post('/snapshots/reload', api_snapshots.reload)

self.webapp.router.add_post(
'/snapshots/new/full', api_snapshots.snapshot_full)
self.webapp.router.add_post(
Expand Down
43 changes: 41 additions & 2 deletions hassio/api/addons.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from ..const import (
ATTR_VERSION, ATTR_LAST_VERSION, ATTR_STATE, ATTR_BOOT, ATTR_OPTIONS,
ATTR_URL, ATTR_DESCRIPTON, ATTR_DETACHED, ATTR_NAME, ATTR_REPOSITORY,
ATTR_BUILD, ATTR_AUTO_UPDATE, ATTR_NETWORK, ATTR_HOST_NETWORK,
BOOT_AUTO, BOOT_MANUAL)
ATTR_BUILD, ATTR_AUTO_UPDATE, ATTR_NETWORK, ATTR_HOST_NETWORK, ATTR_SLUG,
ATTR_SOURCE, ATTR_REPOSITORIES, ATTR_ADDONS, ATTR_ARCH, ATTR_MAINTAINER,
ATTR_INSTALLED, BOOT_AUTO, BOOT_MANUAL)
from ..validate import DOCKER_PORTS

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -47,6 +48,44 @@ def _extract_addon(self, request, check_installed=True):

return addon

@api_process
async def list(self, request):
"""Return all addons / repositories ."""
data_addons = []
for addon in self.addons.list_addons:
data_addons.append({
ATTR_NAME: addon.name,
ATTR_SLUG: addon.slug,
ATTR_DESCRIPTON: addon.description,
ATTR_VERSION: addon.last_version,
ATTR_INSTALLED: addon.version_installed,
ATTR_ARCH: addon.supported_arch,
ATTR_DETACHED: addon.is_detached,
ATTR_REPOSITORY: addon.repository,
ATTR_BUILD: addon.need_build,
ATTR_URL: addon.url,
})

data_repositories = []
for repository in self.addons.list_repositories:
data_repositories.append({
ATTR_SLUG: repository.slug,
ATTR_NAME: repository.name,
ATTR_SOURCE: repository.source,
ATTR_URL: repository.url,
ATTR_MAINTAINER: repository.maintainer,
})

return {
ATTR_ADDONS: data_addons,
ATTR_REPOSITORIES: data_repositories,
}

@api_process
def reload(self, request):
"""Reload all addons data."""
return self.addons.reload()

@api_process
async def info(self, request):
"""Return addon information."""
Expand Down
21 changes: 14 additions & 7 deletions hassio/api/homeassistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import voluptuous as vol

from .util import api_process, api_process_raw, api_validate
from ..const import ATTR_VERSION, ATTR_LAST_VERSION, ATTR_DEVICES
from ..const import (
ATTR_VERSION, ATTR_LAST_VERSION, ATTR_DEVICES, ATTR_IMAGE, ATTR_CUSTOM)
from ..validate import HASS_DEVICES

_LOGGER = logging.getLogger(__name__)


SCHEMA_OPTIONS = vol.Schema({
vol.Optional(ATTR_DEVICES): HASS_DEVICES,
vol.Inclusive(ATTR_IMAGE, 'custom_hass'): vol.Any(None, vol.Coerce(str)),
vol.Inclusive(ATTR_LAST_VERSION, 'custom_hass'):
vol.Any(None, vol.Coerce(str)),
})

SCHEMA_VERSION = vol.Schema({
Expand All @@ -34,8 +38,10 @@ async def info(self, request):
"""Return host information."""
return {
ATTR_VERSION: self.homeassistant.version,
ATTR_LAST_VERSION: self.config.last_homeassistant,
ATTR_DEVICES: self.config.homeassistant_devices,
ATTR_LAST_VERSION: self.homeassistant.last_version,
ATTR_IMAGE: self.homeassistant.image,
ATTR_DEVICES: self.homeassistant.devices,
ATTR_CUSTOM: self.homeassistant.is_custom_image,
}

@api_process
Expand All @@ -44,7 +50,11 @@ async def options(self, request):
body = await api_validate(SCHEMA_OPTIONS, request)

if ATTR_DEVICES in body:
self.config.homeassistant_devices = body[ATTR_DEVICES]
self.homeassistant.devices = body[ATTR_DEVICES]

if ATTR_IMAGE in body:
self.homeassistant.set_custom(
body[ATTR_IMAGE], body[ATTR_LAST_VERSION])

return True

Expand All @@ -57,9 +67,6 @@ async def update(self, request):
if self.homeassistant.in_progress:
raise RuntimeError("Other task is in progress")

if version == self.homeassistant.version:
raise RuntimeError("Version is already in use")

return await asyncio.shield(
self.homeassistant.update(version), loop=self.loop)

Expand Down
40 changes: 28 additions & 12 deletions hassio/api/snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ..const import (
ATTR_NAME, ATTR_SLUG, ATTR_DATE, ATTR_ADDONS, ATTR_REPOSITORIES,
ATTR_HOMEASSISTANT, ATTR_VERSION, ATTR_SIZE, ATTR_FOLDERS, ATTR_TYPE,
ATTR_DEVICES)
ATTR_DEVICES, ATTR_SNAPSHOTS)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -47,23 +47,39 @@ def _extract_snapshot(self, request):
raise RuntimeError("Snapshot not exists")
return snapshot

@staticmethod
def _addons_list(snapshot):
"""Generate a list with addons data."""
data = []
for addon_data in snapshot.addons:
data.append({
ATTR_SLUG: addon_data[ATTR_SLUG],
ATTR_NAME: addon_data[ATTR_NAME],
ATTR_VERSION: addon_data[ATTR_VERSION],
@api_process
async def list(self, request):
"""Return snapshot list."""
data_snapshots = []
for snapshot in self.snapshots.list_snapshots:
data_snapshots.append({
ATTR_SLUG: snapshot.slug,
ATTR_NAME: snapshot.name,
ATTR_DATE: snapshot.date,
})
return data

return {
ATTR_SNAPSHOTS: data_snapshots,
}

@api_process
def reload(self, request):
"""Reload snapshot list."""
return asyncio.shield(self.snapshots.reload(), loop=self.loop)

@api_process
async def info(self, request):
"""Return snapshot info."""
snapshot = self._extract_snapshot(request)

data_addons = []
for addon_data in snapshot.addons:
data_addons.append({
ATTR_SLUG: addon_data[ATTR_SLUG],
ATTR_NAME: addon_data[ATTR_NAME],
ATTR_VERSION: addon_data[ATTR_VERSION],
})

return {
ATTR_SLUG: snapshot.slug,
ATTR_TYPE: snapshot.sys_type,
Expand All @@ -74,7 +90,7 @@ async def info(self, request):
ATTR_VERSION: snapshot.homeassistant_version,
ATTR_DEVICES: snapshot.homeassistant_devices,
},
ATTR_ADDONS: self._addons_list(snapshot),
ATTR_ADDONS: data_addons,
ATTR_REPOSITORIES: snapshot.repositories,
ATTR_FOLDERS: snapshot.folders,
}
Expand Down
Loading

0 comments on commit bfa7443

Please sign in to comment.