homey-api 1.5.9 → 1.5.12
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/lib/APIErrorTimeout.js +21 -0
- package/lib/EventEmitter.js +19 -1
- package/lib/HomeyAPI/HomeyAPIV2/Device.js +18 -1
- package/lib/HomeyAPI/HomeyAPIV2/Manager.js +17 -5
- package/lib/Util.js +15 -2
- package/package.json +1 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/* eslint-disable camelcase */
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
const APIError = require('./APIError');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @class
|
|
9
|
+
* @hideconstructor
|
|
10
|
+
* @extends APIError
|
|
11
|
+
* @memberof HomeyAPI
|
|
12
|
+
*/
|
|
13
|
+
class APIErrorTimeout extends APIError {
|
|
14
|
+
|
|
15
|
+
constructor(message, statusCode = 408) {
|
|
16
|
+
super(message, statusCode);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = APIErrorTimeout;
|
package/lib/EventEmitter.js
CHANGED
|
@@ -26,6 +26,16 @@ class EventEmitter {
|
|
|
26
26
|
listener(...data);
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
|
+
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {string} event
|
|
35
|
+
* @param {function} callback
|
|
36
|
+
*/
|
|
37
|
+
addListener(event, callback) {
|
|
38
|
+
return this.on(event, callback);
|
|
29
39
|
}
|
|
30
40
|
|
|
31
41
|
/**
|
|
@@ -37,6 +47,8 @@ class EventEmitter {
|
|
|
37
47
|
|
|
38
48
|
this.__listeners[event] = this.__listeners[event] || [];
|
|
39
49
|
this.__listeners[event].push(callback);
|
|
50
|
+
|
|
51
|
+
return this;
|
|
40
52
|
}
|
|
41
53
|
|
|
42
54
|
/**
|
|
@@ -53,6 +65,8 @@ class EventEmitter {
|
|
|
53
65
|
|
|
54
66
|
this.__listeners[event] = this.__listeners[event] || [];
|
|
55
67
|
this.__listeners[event].push(callback_);
|
|
68
|
+
|
|
69
|
+
return this;
|
|
56
70
|
}
|
|
57
71
|
|
|
58
72
|
/**
|
|
@@ -63,6 +77,8 @@ class EventEmitter {
|
|
|
63
77
|
this.__debug(`.off('${event}')`);
|
|
64
78
|
|
|
65
79
|
this.__listeners[event] = this.__listeners[event].filter(cb => cb !== callback);
|
|
80
|
+
|
|
81
|
+
return this;
|
|
66
82
|
}
|
|
67
83
|
|
|
68
84
|
/**
|
|
@@ -71,7 +87,7 @@ class EventEmitter {
|
|
|
71
87
|
* @param {function} callback
|
|
72
88
|
*/
|
|
73
89
|
removeListener(event, callback) {
|
|
74
|
-
this.off(event, callback);
|
|
90
|
+
return this.off(event, callback);
|
|
75
91
|
}
|
|
76
92
|
|
|
77
93
|
/**
|
|
@@ -79,6 +95,8 @@ class EventEmitter {
|
|
|
79
95
|
*/
|
|
80
96
|
removeAllListeners(event) {
|
|
81
97
|
this.__listeners[event] = [];
|
|
98
|
+
|
|
99
|
+
return this;
|
|
82
100
|
}
|
|
83
101
|
|
|
84
102
|
}
|
|
@@ -76,7 +76,16 @@ class Device extends Item {
|
|
|
76
76
|
* @returns {Promise<void>}
|
|
77
77
|
* @function HomeyAPIV2.ManagerDevices.Device#setCapabilityValue
|
|
78
78
|
*/
|
|
79
|
-
async setCapabilityValue({
|
|
79
|
+
async setCapabilityValue(options, ...args) {
|
|
80
|
+
// Legacy compatibility from node-athom-api
|
|
81
|
+
if (typeof options === 'string') {
|
|
82
|
+
return this.setCapabilityValueLegacy(options, ...args);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return this.__setCapabilityValue(options);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async __setCapabilityValue({
|
|
80
89
|
capabilityId,
|
|
81
90
|
value,
|
|
82
91
|
opts,
|
|
@@ -93,6 +102,14 @@ class Device extends Item {
|
|
|
93
102
|
});
|
|
94
103
|
}
|
|
95
104
|
|
|
105
|
+
async setCapabilityValueLegacy(capabilityId, value, opts) {
|
|
106
|
+
return this.__setCapabilityValue({
|
|
107
|
+
capabilityId,
|
|
108
|
+
value,
|
|
109
|
+
opts,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
96
113
|
async connect() {
|
|
97
114
|
this.__debug('connect');
|
|
98
115
|
|
|
@@ -215,15 +215,15 @@ class Manager extends EventEmitter {
|
|
|
215
215
|
case 'getAll': {
|
|
216
216
|
// If array (uri + id)
|
|
217
217
|
if (this.__cache[itemId]
|
|
218
|
-
|
|
219
|
-
|
|
218
|
+
&& this.__cacheAllComplete[itemId]
|
|
219
|
+
&& itemType === 'filter') {
|
|
220
220
|
this.__debug(`Serving ${itemId}:all from cache`);
|
|
221
221
|
return Object.values(this.__cache[itemId]);
|
|
222
222
|
}
|
|
223
223
|
|
|
224
224
|
// If object (id)
|
|
225
225
|
if (this.__cache[itemId]
|
|
226
|
-
|
|
226
|
+
&& this.__cacheAllComplete[itemId]) {
|
|
227
227
|
this.__debug(`Serving ${itemId}:all from cache`);
|
|
228
228
|
return this.__cache[itemId];
|
|
229
229
|
}
|
|
@@ -382,8 +382,8 @@ class Manager extends EventEmitter {
|
|
|
382
382
|
|
|
383
383
|
// Transform & add to cache if this is a CRUD event
|
|
384
384
|
if (event.endsWith('.create')
|
|
385
|
-
|
|
386
|
-
|
|
385
|
+
|| event.endsWith('.update')
|
|
386
|
+
|| event.endsWith('.delete')) {
|
|
387
387
|
const [itemId, operation] = event.split('.');
|
|
388
388
|
const ItemClass = this.constructor.ITEMS[itemId] || Item;
|
|
389
389
|
const itemType = this.__itemsById[itemId].type;
|
|
@@ -449,6 +449,18 @@ class Manager extends EventEmitter {
|
|
|
449
449
|
}
|
|
450
450
|
}
|
|
451
451
|
|
|
452
|
+
destroy() {
|
|
453
|
+
for (const id of Object.keys(this.__cache)) {
|
|
454
|
+
this.__cache[id] = {};
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
for (const id of Object.keys(this.__cacheAllComplete)) {
|
|
458
|
+
this.__cacheAllComplete[id] = false;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
this.disconnect().catch(() => { });
|
|
462
|
+
}
|
|
463
|
+
|
|
452
464
|
}
|
|
453
465
|
|
|
454
466
|
module.exports = Manager;
|
package/lib/Util.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
'use strict';
|
|
4
4
|
|
|
5
|
+
const APIErrorTimeout = require('./APIErrorTimeout');
|
|
6
|
+
|
|
5
7
|
/**
|
|
6
8
|
* Helper Utility Class
|
|
7
9
|
* @class
|
|
@@ -43,7 +45,7 @@ class Util {
|
|
|
43
45
|
* @returns {Promise}
|
|
44
46
|
*/
|
|
45
47
|
static async timeout(promise, timeoutMillis = 5000, message = `Timeout after ${timeoutMillis}ms`) {
|
|
46
|
-
const timeoutError = new
|
|
48
|
+
const timeoutError = new APIErrorTimeout(message);
|
|
47
49
|
let timeoutRef;
|
|
48
50
|
return Promise.race([
|
|
49
51
|
promise,
|
|
@@ -86,6 +88,13 @@ class Util {
|
|
|
86
88
|
return (typeof window !== 'undefined');
|
|
87
89
|
}
|
|
88
90
|
|
|
91
|
+
/**
|
|
92
|
+
* @returns {boolean}
|
|
93
|
+
*/
|
|
94
|
+
static isNodeJS() {
|
|
95
|
+
return (typeof process !== 'undefined');
|
|
96
|
+
}
|
|
97
|
+
|
|
89
98
|
/**
|
|
90
99
|
* @param {string} name - Query parameter name
|
|
91
100
|
* @returns {string|null}
|
|
@@ -148,7 +157,11 @@ class Util {
|
|
|
148
157
|
return window.localStorage.getItem(key) || null;
|
|
149
158
|
}
|
|
150
159
|
|
|
151
|
-
|
|
160
|
+
if (this.isNodeJS()) {
|
|
161
|
+
return process.env[key] || null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return null;
|
|
152
165
|
}
|
|
153
166
|
|
|
154
167
|
static envKey(key) {
|