Skip to content

Commit 76cead7

Browse files
authored
Update API for hass api v2 (#14)
* Update API for hass api v2 * fix lint * Refactory the old version of host_control * cleanup * Cleanup name inside addons/data * Cleanup name inside addons/data p2 * Rename api list * Fix path bug * Fix wrong config set
1 parent a0f17ff commit 76cead7

File tree

15 files changed

+186
-170
lines changed

15 files changed

+186
-170
lines changed

API.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## HassIO REST API
44

5-
Interface for HomeAssistant to controll things from supervisor.
5+
Interface for HomeAssistant to control things from supervisor.
66

77
On error:
88
```json
@@ -29,13 +29,13 @@ On success
2929
```json
3030
{
3131
"version": "INSTALL_VERSION",
32-
"current": "CURRENT_VERSION",
33-
"beta": "true|false",
32+
"last_version": "CURRENT_VERSION",
33+
"beta_channel": "true|false",
3434
"addons": [
3535
{
3636
"name": "xy bla",
3737
"slug": "xy",
38-
"version": "CURRENT_VERSION",
38+
"version": "LAST_VERSION",
3939
"installed": "none|INSTALL_VERSION",
4040
"dedicated": "bool",
4141
"description": "description"
@@ -55,7 +55,7 @@ Optional:
5555
- `/supervisor/option`
5656
```json
5757
{
58-
"beta": "true|false"
58+
"beta_channel": "true|false"
5959
}
6060
```
6161

@@ -74,13 +74,13 @@ Output the raw docker log
7474
- `/host/reboot`
7575

7676
- `/host/info`
77-
See HostControll info command.
77+
See HostControl info command.
7878
```json
7979
{
80-
"os": "",
80+
"type": "",
8181
"version": "",
82-
"current": "",
83-
"level": "",
82+
"last_version": "",
83+
"features": ["shutdown", "reboot", "update", "network_info", "network_control"],
8484
"hostname": "",
8585
}
8686
```
@@ -116,7 +116,7 @@ Optional:
116116
```json
117117
{
118118
"version": "INSTALL_VERSION",
119-
"current": "CURRENT_VERSION"
119+
"last_version": "LAST_VERSION"
120120
}
121121
```
122122

@@ -138,7 +138,7 @@ Output the raw docker log
138138
```json
139139
{
140140
"version": "VERSION",
141-
"current": "CURRENT_VERSION",
141+
"last_version": "LAST_VERSION",
142142
"state": "started|stopped",
143143
"boot": "auto|manual",
144144
"options": {},
@@ -149,7 +149,7 @@ Output the raw docker log
149149
```json
150150
{
151151
"boot": "auto|manual",
152-
"options": {},
152+
"options": {},
153153
}
154154
```
155155

@@ -179,14 +179,14 @@ Optional:
179179

180180
Output the raw docker log
181181

182-
## Host Controll
182+
## Host Control
183183

184184
Communicate over unix socket with a host daemon.
185185

186186
- commands
187187
```
188188
# info
189-
-> {'os', 'version', 'current', 'level', 'hostname'}
189+
-> {'type', 'version', 'last_version', 'features', 'hostname'}
190190
# reboot
191191
# shutdown
192192
# host-update [v]
@@ -200,10 +200,12 @@ Communicate over unix socket with a host daemon.
200200
# network int route xy
201201
```
202202

203-
level:
204-
- 1: power functions
205-
- 2: host update
206-
- 4: network functions
203+
features:
204+
- shutdown
205+
- reboot
206+
- update
207+
- network_info
208+
- network_control
207209

208210
Answer:
209211
```

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# HassIO
22
First private cloud solution for home automation.
33

4-
It is a docker image (supervisor) they manage HomeAssistant docker and give a interface to controll itself over UI. It have a own eco system with addons to extend the functionality in a easy way.
4+
It is a docker image (supervisor) they manage HomeAssistant docker and give a interface to control itself over UI. It have a own eco system with addons to extend the functionality in a easy way.
55

66
[HassIO-Addons](https://github.com/pvizeli/hassio-addons) | [HassIO-Build](https://github.com/pvizeli/hassio-build)
77

@@ -33,8 +33,8 @@ After extracting the archive, flash it to a drive using [Etcher](https://etcher.
3333
## History
3434
- **0.1**: First techpreview with dumy supervisor (ResinOS 2.0.0-RC5)
3535
- **0.2**: Fix some bugs and update it to HassIO 0.2
36-
- **0.3**: Update HostControll and feature for HassIO 0.3 (ResinOS 2.0.0 / need reflash)
37-
- **0.4**: Update HostControll and bring resinos OTA (resinhub) back (ResinOS 2.0.0-rev3)
36+
- **0.3**: Update HostControl and feature for HassIO 0.3 (ResinOS 2.0.0 / need reflash)
37+
- **0.4**: Update HostControl and bring resinos OTA (resinhub) back (ResinOS 2.0.0-rev3)
3838
3939
## Configuring the image
4040
You can configure the WiFi network that the image should connect to after flashing using [`resin-device-toolbox`](https://resinos.io/docs/raspberrypi3/gettingstarted/#install-resin-device-toolbox).

hassio/addons/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ async def install(self, addon, version=None):
7878
addon_docker = DockerAddon(
7979
self.config, self.loop, self.dock, self, addon)
8080

81-
version = version or self.get_version(addon)
81+
version = version or self.get_last_version(addon)
8282
if not await addon_docker.install(version):
8383
return False
8484

@@ -144,7 +144,7 @@ async def update(self, addon, version=None):
144144
_LOGGER.error("No docker found for addon %s", addon)
145145
return False
146146

147-
version = version or self.get_version(addon)
147+
version = version or self.get_last_version(addon)
148148
is_running = self.dockers[addon].is_running()
149149

150150
# update

hassio/addons/data.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(self, config):
2828
"""Initialize data holder."""
2929
super().__init__(FILE_HASSIO_ADDONS)
3030
self.config = config
31-
self._addons_data = self._data.get(SYSTEM, {})
31+
self._system_data = self._data.get(SYSTEM, {})
3232
self._user_data = self._data.get(USER, {})
3333
self._current_data = {}
3434
self.arch = None
@@ -37,7 +37,7 @@ def save(self):
3737
"""Store data to config file."""
3838
self._data = {
3939
USER: self._user_data,
40-
SYSTEM: self._addons_data,
40+
SYSTEM: self._system_data,
4141
}
4242
super().save()
4343

@@ -69,13 +69,13 @@ def _read_addons_folder(self, folder):
6969
@property
7070
def list_installed(self):
7171
"""Return a list of installed addons."""
72-
return set(self._addons_data.keys())
72+
return set(self._system_data.keys())
7373

7474
@property
75-
def list(self):
76-
"""Return a list of available addons."""
75+
def list_api(self):
76+
"""Return a list of available addons for api."""
7777
data = []
78-
all_addons = {**self._addons_data, **self._current_data}
78+
all_addons = {**self._system_data, **self._current_data}
7979
dedicated = self.list_removed
8080

8181
for addon, values in all_addons.items():
@@ -95,12 +95,12 @@ def list(self):
9595
def list_startup(self, start_type):
9696
"""Get list of installed addon with need start by type."""
9797
addon_list = set()
98-
for addon in self._addons_data.keys():
98+
for addon in self._system_data.keys():
9999
if self.get_boot(addon) != BOOT_AUTO:
100100
continue
101101

102102
try:
103-
if self._addons_data[addon][ATTR_STARTUP] == start_type:
103+
if self._system_data[addon][ATTR_STARTUP] == start_type:
104104
addon_list.add(addon)
105105
except KeyError:
106106
_LOGGER.warning("Orphaned addon detect %s", addon)
@@ -112,30 +112,27 @@ def list_startup(self, start_type):
112112
def list_removed(self):
113113
"""Return local addons they not support from repo."""
114114
addon_list = set()
115-
for addon in self._addons_data.keys():
115+
for addon in self._system_data.keys():
116116
if addon not in self._current_data:
117117
addon_list.add(addon)
118118

119119
return addon_list
120120

121121
def exists_addon(self, addon):
122122
"""Return True if a addon exists."""
123-
return addon in self._current_data or addon in self._addons_data
123+
return addon in self._current_data or addon in self._system_data
124124

125125
def is_installed(self, addon):
126126
"""Return True if a addon is installed."""
127-
return addon in self._addons_data
127+
return addon in self._system_data
128128

129129
def version_installed(self, addon):
130130
"""Return installed version."""
131-
if ATTR_VERSION not in self._user_data[addon]:
132-
return self._addons_data[addon][ATTR_VERSION]
133-
134131
return self._user_data[addon][ATTR_VERSION]
135132

136133
def set_addon_install(self, addon, version):
137134
"""Set addon as installed."""
138-
self._addons_data[addon] = self._current_data[addon]
135+
self._system_data[addon] = self._current_data[addon]
139136
self._user_data[addon] = {
140137
ATTR_OPTIONS: {},
141138
ATTR_VERSION: version,
@@ -144,13 +141,13 @@ def set_addon_install(self, addon, version):
144141

145142
def set_addon_uninstall(self, addon):
146143
"""Set addon as uninstalled."""
147-
self._addons_data.pop(addon, None)
144+
self._system_data.pop(addon, None)
148145
self._user_data.pop(addon, None)
149146
self.save()
150147

151148
def set_addon_update(self, addon, version):
152149
"""Update version of addon."""
153-
self._addons_data[addon] = self._current_data[addon]
150+
self._system_data[addon] = self._current_data[addon]
154151
self._user_data[addon][ATTR_VERSION] = version
155152
self.save()
156153

@@ -167,7 +164,7 @@ def set_boot(self, addon, boot):
167164
def get_options(self, addon):
168165
"""Return options with local changes."""
169166
return {
170-
**self._addons_data[addon][ATTR_OPTIONS],
167+
**self._system_data[addon][ATTR_OPTIONS],
171168
**self._user_data[addon][ATTR_OPTIONS],
172169
}
173170

@@ -176,29 +173,29 @@ def get_boot(self, addon):
176173
if ATTR_BOOT in self._user_data[addon]:
177174
return self._user_data[addon][ATTR_BOOT]
178175

179-
return self._addons_data[addon][ATTR_BOOT]
176+
return self._system_data[addon][ATTR_BOOT]
180177

181178
def get_name(self, addon):
182179
"""Return name of addon."""
183-
return self._addons_data[addon][ATTR_NAME]
180+
return self._system_data[addon][ATTR_NAME]
184181

185182
def get_description(self, addon):
186183
"""Return description of addon."""
187-
return self._addons_data[addon][ATTR_DESCRIPTON]
184+
return self._system_data[addon][ATTR_DESCRIPTON]
188185

189-
def get_version(self, addon):
186+
def get_last_version(self, addon):
190187
"""Return version of addon."""
191188
if addon not in self._current_data:
192189
return self.version_installed(addon)
193190
return self._current_data[addon][ATTR_VERSION]
194191

195192
def get_ports(self, addon):
196193
"""Return ports of addon."""
197-
return self._addons_data[addon].get(ATTR_PORTS)
194+
return self._system_data[addon].get(ATTR_PORTS)
198195

199196
def get_image(self, addon):
200197
"""Return image name of addon."""
201-
addon_data = self._addons_data.get(addon, self._current_data[addon])
198+
addon_data = self._system_data.get(addon, self._current_data[addon])
202199

203200
if ATTR_IMAGE not in addon_data:
204201
return "{}/{}-addon-{}".format(DOCKER_REPO, self.arch, addon)
@@ -207,11 +204,11 @@ def get_image(self, addon):
207204

208205
def need_config(self, addon):
209206
"""Return True if config map is needed."""
210-
return self._addons_data[addon][ATTR_MAP_CONFIG]
207+
return self._system_data[addon][ATTR_MAP_CONFIG]
211208

212209
def need_ssl(self, addon):
213210
"""Return True if ssl map is needed."""
214-
return self._addons_data[addon][ATTR_MAP_SSL]
211+
return self._system_data[addon][ATTR_MAP_SSL]
215212

216213
def path_data(self, addon):
217214
"""Return addon data path inside supervisor."""
@@ -241,7 +238,7 @@ def write_addon_options(self, addon):
241238

242239
def get_schema(self, addon):
243240
"""Create a schema for addon options."""
244-
raw_schema = self._addons_data[addon][ATTR_SCHEMA]
241+
raw_schema = self._system_data[addon][ATTR_SCHEMA]
245242

246243
schema = vol.Schema(vol.All(dict, validate_options(raw_schema)))
247244
return schema

hassio/api/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,26 @@ def __init__(self, config, loop):
2525
self._handler = None
2626
self.server = None
2727

28-
def register_host(self, host_controll):
29-
"""Register hostcontroll function."""
30-
api_host = APIHost(self.config, self.loop, host_controll)
28+
def register_host(self, host_control):
29+
"""Register hostcontrol function."""
30+
api_host = APIHost(self.config, self.loop, host_control)
3131

3232
self.webapp.router.add_get('/host/info', api_host.info)
3333
self.webapp.router.add_get('/host/reboot', api_host.reboot)
3434
self.webapp.router.add_get('/host/shutdown', api_host.shutdown)
3535
self.webapp.router.add_get('/host/update', api_host.update)
3636

37-
def register_network(self, host_controll):
37+
def register_network(self, host_control):
3838
"""Register network function."""
39-
api_net = APINetwork(self.config, self.loop, host_controll)
39+
api_net = APINetwork(self.config, self.loop, host_control)
4040

4141
self.webapp.router.add_get('/network/info', api_net.info)
4242
self.webapp.router.add_get('/network/options', api_net.options)
4343

44-
def register_supervisor(self, supervisor, addons):
44+
def register_supervisor(self, supervisor, addons, host_control):
4545
"""Register supervisor function."""
4646
api_supervisor = APISupervisor(
47-
self.config, self.loop, supervisor, addons)
47+
self.config, self.loop, supervisor, addons, host_control)
4848

4949
self.webapp.router.add_get('/supervisor/ping', api_supervisor.ping)
5050
self.webapp.router.add_get('/supervisor/info', api_supervisor.info)

0 commit comments

Comments
 (0)