homey-api 1.4.4 → 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 +4 -0
- package/index.js +1 -0
- package/lib/HomeyAPI/HomeyAPIApp.js +122 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -40,6 +40,10 @@ Include Homey API from our CDN:
|
|
|
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).
|
package/index.js
CHANGED
|
@@ -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;
|