homey-api 1.4.1 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/assets/specifications/AthomAppsAPI.json +11 -0
- package/index.js +1 -0
- package/lib/API.js +1 -1
- package/lib/HomeyAPI/HomeyAPIApp.js +122 -0
- package/lib/HomeyAPI/HomeyAPIV2.js +29 -23
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -35,11 +35,15 @@ const AthomCloudAPI = require('homey-api/lib/AthomCloudAPI');
|
|
|
35
35
|
Include Homey API from our CDN:
|
|
36
36
|
|
|
37
37
|
```html
|
|
38
|
-
<script type="text/javascript" src="https://cdn.athom.com/homey-api
|
|
38
|
+
<script type="text/javascript" src="https://cdn.athom.com/homey-api/$VERSION.js"></script>
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
You can then access the APIs using `window.AthomCloudAPI` etc.
|
|
42
42
|
|
|
43
|
+
### In-app
|
|
44
|
+
|
|
45
|
+
To use Homey API inside of an Homey Pro app with the `homey:manager:api` permission, see {@link HomeyAPIApp}.
|
|
46
|
+
|
|
43
47
|
## Issues
|
|
44
48
|
|
|
45
49
|
Please report any issues you find in the [Web API Issue Tracker](https://github.com/athombv/homey-web-api-issues/issues).
|
|
@@ -140,6 +140,17 @@
|
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
},
|
|
143
|
+
"getAppChangelog": {
|
|
144
|
+
"path": "/app/{appId}/changelog",
|
|
145
|
+
"method": "get",
|
|
146
|
+
"parameters": {
|
|
147
|
+
"appId": {
|
|
148
|
+
"in": "path",
|
|
149
|
+
"type": "string",
|
|
150
|
+
"required": true
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
},
|
|
143
154
|
"updateAppAuthor": {
|
|
144
155
|
"path": "/app/{appId}/author",
|
|
145
156
|
"method": "put",
|
package/index.js
CHANGED
package/lib/API.js
CHANGED
|
@@ -195,7 +195,7 @@ class API {
|
|
|
195
195
|
}
|
|
196
196
|
|
|
197
197
|
// Body
|
|
198
|
-
if (['PUT', 'POST'].includes(request.method)) {
|
|
198
|
+
if (['PUT', 'POST', 'DELETE'].includes(request.method)) {
|
|
199
199
|
if (request.body && request.bodyJSON !== false) {
|
|
200
200
|
request.headers['Content-Type'] = 'application/json';
|
|
201
201
|
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const HomeyAPI = require('./HomeyAPI');
|
|
4
|
+
const HomeyAPIV2 = require('./HomeyAPIV2');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Use this class to utilize Homey Pro's Web API from inside an app.
|
|
8
|
+
*
|
|
9
|
+
* > This class only works on apps using Apps SDK v3 running on Homey Pro.
|
|
10
|
+
*
|
|
11
|
+
* > Make sure your app has the `homey:manager:api` permission.
|
|
12
|
+
*
|
|
13
|
+
* @class
|
|
14
|
+
* @extends HomeyAPIV2
|
|
15
|
+
* @example
|
|
16
|
+
* // app.json
|
|
17
|
+
* {
|
|
18
|
+
* ...
|
|
19
|
+
* "platforms": [ "local" ],
|
|
20
|
+
* "permissions": [ "homey:manager:api" ],
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* // app.js
|
|
24
|
+
* const { Homey } = require('homey');
|
|
25
|
+
* const { HomeyAPIApp } = require('homey-api');
|
|
26
|
+
*
|
|
27
|
+
* class MyApp extends Homey.App {
|
|
28
|
+
*
|
|
29
|
+
* async onInit() {
|
|
30
|
+
* const api = new HomeyAPIApp({
|
|
31
|
+
* homey: this.homey,
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* const devices = await api.devices.getDevices();
|
|
35
|
+
* this.log('Devices:', devices);
|
|
36
|
+
* }
|
|
37
|
+
*
|
|
38
|
+
* }
|
|
39
|
+
*/
|
|
40
|
+
class HomeyAPIApp extends HomeyAPIV2 {
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Possible Discovery Strategies
|
|
44
|
+
* @static
|
|
45
|
+
* @property {object} DISCOVERY_STRATEGIES
|
|
46
|
+
* @property {string} DISCOVERY_STRATEGIES.LOCAL - Local HTTP, e.g. `http://192.168.1.100`.
|
|
47
|
+
*/
|
|
48
|
+
static DISCOVERY_STRATEGIES = {};
|
|
49
|
+
|
|
50
|
+
constructor({
|
|
51
|
+
homey,
|
|
52
|
+
debug = false,
|
|
53
|
+
...props
|
|
54
|
+
}) {
|
|
55
|
+
super({
|
|
56
|
+
...props,
|
|
57
|
+
api: null,
|
|
58
|
+
properties: {
|
|
59
|
+
id: 'local',
|
|
60
|
+
softwareVersion: null,
|
|
61
|
+
},
|
|
62
|
+
strategy: [
|
|
63
|
+
HomeyAPI.DISCOVERY_STRATEGIES.LOCAL,
|
|
64
|
+
],
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
Object.defineProperty(this, '__homeySDK', {
|
|
68
|
+
value: homey,
|
|
69
|
+
enumerable: false,
|
|
70
|
+
writable: true,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
Object.defineProperty(this, '__debugEnabled', {
|
|
74
|
+
value: !!debug,
|
|
75
|
+
enumerable: false,
|
|
76
|
+
writable: true,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
__debug(...props) {
|
|
81
|
+
if (!this.__debugEnabled) return;
|
|
82
|
+
this.__homeySDK.log('[homey-api]', `[${this.constructor.name}]`, ...props);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async login() {
|
|
86
|
+
if (!this.__loginPromise) {
|
|
87
|
+
this.__loginPromise = Promise.resolve().then(async () => {
|
|
88
|
+
const token = await this.__homeySDK.api.getOwnerApiToken();
|
|
89
|
+
this.__debug(`Local Token: ${token}`);
|
|
90
|
+
return token;
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
this.__loginPromise
|
|
94
|
+
.then(token => {
|
|
95
|
+
this.__token = token;
|
|
96
|
+
})
|
|
97
|
+
.catch(err => {
|
|
98
|
+
this.__debug('Error Logging In:', err);
|
|
99
|
+
})
|
|
100
|
+
.finally(() => {
|
|
101
|
+
this.__loginPromise = null;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return this.__loginPromise;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async discoverBaseUrl() {
|
|
109
|
+
this.__baseUrl = await this.__homeySDK.api.getLocalUrl();
|
|
110
|
+
this.__strategyId = 'local';
|
|
111
|
+
|
|
112
|
+
this.__debug(`Local URL: ${this.__baseUrl}`);
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
baseUrl: this.__baseUrl,
|
|
116
|
+
strategyId: this.__strategyId,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
module.exports = HomeyAPIApp;
|
|
@@ -275,24 +275,30 @@ class HomeyAPIV2 extends HomeyAPI {
|
|
|
275
275
|
pings[HomeyAPI.DISCOVERY_STRATEGIES.LOCAL_SECURE]
|
|
276
276
|
.then(result => resolve(result))
|
|
277
277
|
.catch(() => {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
278
|
+
const promises = [];
|
|
279
|
+
|
|
280
|
+
if (pings[HomeyAPI.DISCOVERY_STRATEGIES.LOCAL]) {
|
|
281
|
+
promises.push(pings[HomeyAPI.DISCOVERY_STRATEGIES.LOCAL]);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (pings[HomeyAPI.DISCOVERY_STRATEGIES.MDNS]) {
|
|
285
|
+
promises.push(pings[HomeyAPI.DISCOVERY_STRATEGIES.MDNS]);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// TODO: Move this to the catch handler to always fallback on cloud
|
|
289
|
+
// Now mdns or local will error first and cloud won't have a chance!!
|
|
290
|
+
if (pings[HomeyAPI.DISCOVERY_STRATEGIES.CLOUD]) {
|
|
291
|
+
promises.push(pings[HomeyAPI.DISCOVERY_STRATEGIES.CLOUD]);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (!promises.length) {
|
|
295
|
+
throw new HomeyOfflineError();
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return Promise.race(promises);
|
|
299
|
+
})
|
|
300
|
+
.then(result => resolve(result))
|
|
301
|
+
.catch(() => reject(new HomeyOfflineError()));
|
|
296
302
|
} else if (pings[HomeyAPI.DISCOVERY_STRATEGIES.LOCAL]) {
|
|
297
303
|
pings[HomeyAPI.DISCOVERY_STRATEGIES.LOCAL]
|
|
298
304
|
.then(result => resolve(result))
|
|
@@ -466,11 +472,11 @@ class HomeyAPIV2 extends HomeyAPI {
|
|
|
466
472
|
}
|
|
467
473
|
|
|
468
474
|
async subscribe(uri, {
|
|
469
|
-
onConnect = () => {},
|
|
470
|
-
onReconnect = () => {},
|
|
471
|
-
onReconnectError = () => {},
|
|
472
|
-
onDisconnect = () => {},
|
|
473
|
-
onEvent = () => {},
|
|
475
|
+
onConnect = () => { },
|
|
476
|
+
onReconnect = () => { },
|
|
477
|
+
onReconnectError = () => { },
|
|
478
|
+
onDisconnect = () => { },
|
|
479
|
+
onEvent = () => { },
|
|
474
480
|
}) {
|
|
475
481
|
this.__debug('subscribe', uri);
|
|
476
482
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "homey-api",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Homey API",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"types": "assets/types/homey-api.d.ts",
|
|
@@ -52,7 +52,6 @@
|
|
|
52
52
|
"ejs": "^3.1.6",
|
|
53
53
|
"eslint": "^7.32.0",
|
|
54
54
|
"eslint-config-athom": "^2.1.1",
|
|
55
|
-
"filemanager-webpack-plugin": "^5.0.0",
|
|
56
55
|
"fs-extra": "^10.0.0",
|
|
57
56
|
"homey-jsdoc-template": "github:athombv/homey-jsdoc-template#1.4",
|
|
58
57
|
"http-server": "^0.12.3",
|