Skip to content

Commit f8fbee6

Browse files
authored
Merge pull request #96 from pvizeli/cleanup_api_snapshot_p2
Update API / fix isoformat
2 parents bcfd76d + 3c5d403 commit f8fbee6

File tree

6 files changed

+125
-65
lines changed

6 files changed

+125
-65
lines changed

API.md

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,48 +51,13 @@ The addons from `addons` are only installed one.
5151
],
5252
"addons_repositories": [
5353
"REPO_URL"
54-
],
55-
"snapshots": [
56-
{
57-
"slug": "SLUG",
58-
"data": "ISO",
59-
"name": "Custom name"
60-
}
6154
]
6255
}
6356
```
6457

6558
- GET `/supervisor/addons`
6659

67-
Get all available addons
68-
69-
```json
70-
{
71-
"addons": [
72-
{
73-
"name": "xy bla",
74-
"slug": "xy",
75-
"description": "description",
76-
"arch": ["armhf", "aarch64", "i386", "amd64"],
77-
"repository": "core|local|REP_ID",
78-
"version": "LAST_VERSION",
79-
"installed": "none|INSTALL_VERSION",
80-
"detached": "bool",
81-
"build": "bool",
82-
"url": "null|url"
83-
}
84-
],
85-
"repositories": [
86-
{
87-
"slug": "12345678",
88-
"name": "Repitory Name|unknown",
89-
"source": "URL_OF_REPOSITORY",
90-
"url": "WEBSITE|REPOSITORY",
91-
"maintainer": "BLA BLU <[email protected]>|unknown"
92-
}
93-
]
94-
}
95-
```
60+
Get all available addons. Will be delete soon. Look to `/addons`
9661

9762
- POST `/supervisor/update`
9863
Optional:
@@ -157,6 +122,21 @@ Return QR-Code
157122

158123
### Backup/Snapshot
159124

125+
- GET `/snapshots`
126+
```json
127+
{
128+
"snapshots": [
129+
{
130+
"slug": "SLUG",
131+
"date": "ISO",
132+
"name": "Custom name"
133+
}
134+
]
135+
}
136+
```
137+
138+
- POST `/snapshots/reload`
139+
160140
- POST `/snapshots/new/full`
161141
```json
162142
{
@@ -302,6 +282,38 @@ Image with `null` and last_version with `null` reset this options.
302282

303283
### REST API addons
304284

285+
- GET `/addons`
286+
287+
Get all available addons
288+
289+
```json
290+
{
291+
"addons": [
292+
{
293+
"name": "xy bla",
294+
"slug": "xy",
295+
"description": "description",
296+
"arch": ["armhf", "aarch64", "i386", "amd64"],
297+
"repository": "core|local|REP_ID",
298+
"version": "LAST_VERSION",
299+
"installed": "none|INSTALL_VERSION",
300+
"detached": "bool",
301+
"build": "bool",
302+
"url": "null|url"
303+
}
304+
],
305+
"repositories": [
306+
{
307+
"slug": "12345678",
308+
"name": "Repitory Name|unknown",
309+
"source": "URL_OF_REPOSITORY",
310+
"url": "WEBSITE|REPOSITORY",
311+
"maintainer": "BLA BLU <[email protected]>|unknown"
312+
}
313+
]
314+
}
315+
```
316+
305317
- POST `/addons/reload`
306318

307319
- GET `/addons/{addon}/info`

hassio/api/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def register_addons(self, addons):
7777
"""Register homeassistant function."""
7878
api_addons = APIAddons(self.config, self.loop, addons)
7979

80+
self.webapp.router.add_get('/addons', api_addons.list)
81+
self.webapp.router.add_post('/addons/reload', api_addons.reload)
82+
8083
self.webapp.router.add_get('/addons/{addon}/info', api_addons.info)
8184
self.webapp.router.add_post(
8285
'/addons/{addon}/install', api_addons.install)
@@ -105,6 +108,9 @@ def register_snapshots(self, snapshots):
105108
"""Register snapshots function."""
106109
api_snapshots = APISnapshots(self.config, self.loop, snapshots)
107110

111+
self.webapp.router.add_get('/snapshots', api_snapshots.list)
112+
self.webapp.router.add_post('/snapshots/reload', api_snapshots.reload)
113+
108114
self.webapp.router.add_post(
109115
'/snapshots/new/full', api_snapshots.snapshot_full)
110116
self.webapp.router.add_post(

hassio/api/addons.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
from ..const import (
1010
ATTR_VERSION, ATTR_LAST_VERSION, ATTR_STATE, ATTR_BOOT, ATTR_OPTIONS,
1111
ATTR_URL, ATTR_DESCRIPTON, ATTR_DETACHED, ATTR_NAME, ATTR_REPOSITORY,
12-
ATTR_BUILD, ATTR_AUTO_UPDATE, ATTR_NETWORK, ATTR_HOST_NETWORK,
13-
BOOT_AUTO, BOOT_MANUAL)
12+
ATTR_BUILD, ATTR_AUTO_UPDATE, ATTR_NETWORK, ATTR_HOST_NETWORK, ATTR_SLUG,
13+
ATTR_SOURCE, ATTR_REPOSITORIES, ATTR_ADDONS, ATTR_ARCH, ATTR_MAINTAINER,
14+
ATTR_INSTALLED, BOOT_AUTO, BOOT_MANUAL)
1415
from ..validate import DOCKER_PORTS
1516

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

4849
return addon
4950

51+
@api_process
52+
async def list(self, request):
53+
"""Return all addons / repositories ."""
54+
data_addons = []
55+
for addon in self.addons.list_addons:
56+
data_addons.append({
57+
ATTR_NAME: addon.name,
58+
ATTR_SLUG: addon.slug,
59+
ATTR_DESCRIPTON: addon.description,
60+
ATTR_VERSION: addon.last_version,
61+
ATTR_INSTALLED: addon.version_installed,
62+
ATTR_ARCH: addon.supported_arch,
63+
ATTR_DETACHED: addon.is_detached,
64+
ATTR_REPOSITORY: addon.repository,
65+
ATTR_BUILD: addon.need_build,
66+
ATTR_URL: addon.url,
67+
})
68+
69+
data_repositories = []
70+
for repository in self.addons.list_repositories:
71+
data_repositories.append({
72+
ATTR_SLUG: repository.slug,
73+
ATTR_NAME: repository.name,
74+
ATTR_SOURCE: repository.source,
75+
ATTR_URL: repository.url,
76+
ATTR_MAINTAINER: repository.maintainer,
77+
})
78+
79+
return {
80+
ATTR_ADDONS: data_addons,
81+
ATTR_REPOSITORIES: data_repositories,
82+
}
83+
84+
@api_process
85+
def reload(self, request):
86+
"""Reload all addons data."""
87+
return self.addons.reload()
88+
5089
@api_process
5190
async def info(self, request):
5291
"""Return addon information."""

hassio/api/snapshots.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ..const import (
1010
ATTR_NAME, ATTR_SLUG, ATTR_DATE, ATTR_ADDONS, ATTR_REPOSITORIES,
1111
ATTR_HOMEASSISTANT, ATTR_VERSION, ATTR_SIZE, ATTR_FOLDERS, ATTR_TYPE,
12-
ATTR_DEVICES)
12+
ATTR_DEVICES, ATTR_SNAPSHOTS)
1313

1414
_LOGGER = logging.getLogger(__name__)
1515

@@ -47,23 +47,39 @@ def _extract_snapshot(self, request):
4747
raise RuntimeError("Snapshot not exists")
4848
return snapshot
4949

50-
@staticmethod
51-
def _addons_list(snapshot):
52-
"""Generate a list with addons data."""
53-
data = []
54-
for addon_data in snapshot.addons:
55-
data.append({
56-
ATTR_SLUG: addon_data[ATTR_SLUG],
57-
ATTR_NAME: addon_data[ATTR_NAME],
58-
ATTR_VERSION: addon_data[ATTR_VERSION],
50+
@api_process
51+
async def list(self, request):
52+
"""Return snapshot list."""
53+
data_snapshots = []
54+
for snapshot in self.snapshots.list_snapshots:
55+
data_snapshots.append({
56+
ATTR_SLUG: snapshot.slug,
57+
ATTR_NAME: snapshot.name,
58+
ATTR_DATE: snapshot.date,
5959
})
60-
return data
60+
61+
return {
62+
ATTR_SNAPSHOTS: data_snapshots,
63+
}
64+
65+
@api_process
66+
def reload(self, request):
67+
"""Reload snapshot list."""
68+
return asyncio.shield(self.snapshots.reload(), loop=self.loop)
6169

6270
@api_process
6371
async def info(self, request):
6472
"""Return snapshot info."""
6573
snapshot = self._extract_snapshot(request)
6674

75+
data_addons = []
76+
for addon_data in snapshot.addons:
77+
data_addons.append({
78+
ATTR_SLUG: addon_data[ATTR_SLUG],
79+
ATTR_NAME: addon_data[ATTR_NAME],
80+
ATTR_VERSION: addon_data[ATTR_VERSION],
81+
})
82+
6783
return {
6884
ATTR_SLUG: snapshot.slug,
6985
ATTR_TYPE: snapshot.sys_type,
@@ -74,7 +90,7 @@ async def info(self, request):
7490
ATTR_VERSION: snapshot.homeassistant_version,
7591
ATTR_DEVICES: snapshot.homeassistant_devices,
7692
},
77-
ATTR_ADDONS: self._addons_list(snapshot),
93+
ATTR_ADDONS: data_addons,
7894
ATTR_REPOSITORIES: snapshot.repositories,
7995
ATTR_FOLDERS: snapshot.folders,
8096
}

hassio/api/supervisor.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
HASSIO_VERSION, ATTR_ADDONS_REPOSITORIES, ATTR_REPOSITORIES,
1111
ATTR_REPOSITORY, ATTR_DESCRIPTON, ATTR_NAME, ATTR_SLUG, ATTR_INSTALLED,
1212
ATTR_DETACHED, ATTR_SOURCE, ATTR_MAINTAINER, ATTR_URL, ATTR_ARCH,
13-
ATTR_BUILD, ATTR_TIMEZONE, ATTR_DATE, ATTR_SNAPSHOTS)
13+
ATTR_BUILD, ATTR_TIMEZONE)
1414
from ..tools import validate_timezone
1515

1616
_LOGGER = logging.getLogger(__name__)
@@ -77,18 +77,6 @@ def _repositories_list(self):
7777

7878
return data
7979

80-
def _snapshots_list(self):
81-
"""Return a list of available snapshots."""
82-
data = []
83-
for snapshot in self.snapshots.list_snapshots:
84-
data.append({
85-
ATTR_SLUG: snapshot.slug,
86-
ATTR_NAME: snapshot.name,
87-
ATTR_DATE: snapshot.date,
88-
})
89-
90-
return data
91-
9280
@api_process
9381
async def ping(self, request):
9482
"""Return ok for signal that the api is ready."""
@@ -105,7 +93,6 @@ async def info(self, request):
10593
ATTR_TIMEZONE: self.config.timezone,
10694
ATTR_ADDONS: self._addons_list(only_installed=True),
10795
ATTR_ADDONS_REPOSITORIES: self.config.addons_repositories,
108-
ATTR_SNAPSHOTS: self._snapshots_list(),
10996
}
11097

11198
@api_process

hassio/snapshots/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def get(self, slug):
3737

3838
def _create_snapshot(self, name, sys_type):
3939
"""Initialize a new snapshot object from name."""
40-
date_str = str(datetime.utcnow())
40+
date_str = datetime.utcnow().isoformat()
4141
slug = create_slug(name, date_str)
4242
tar_file = Path(self.config.path_backup, "{}.tar".format(slug))
4343

0 commit comments

Comments
 (0)