homey-api 1.5.22 → 1.5.23
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.
|
@@ -9,20 +9,6 @@ class Device extends Item {
|
|
|
9
9
|
constructor(...props) {
|
|
10
10
|
super(...props);
|
|
11
11
|
|
|
12
|
-
// Set URI
|
|
13
|
-
Object.defineProperty(this, 'uri', {
|
|
14
|
-
value: `homey:device:${this.id}`,
|
|
15
|
-
enumerable: false,
|
|
16
|
-
writable: true,
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
// Set Connected
|
|
20
|
-
Object.defineProperty(this, '__connected', {
|
|
21
|
-
value: false,
|
|
22
|
-
enumerable: false,
|
|
23
|
-
writable: true,
|
|
24
|
-
});
|
|
25
|
-
|
|
26
12
|
// Set Capability Instances
|
|
27
13
|
Object.defineProperty(this, '__capabilityInstances', {
|
|
28
14
|
value: {},
|
|
@@ -110,108 +96,31 @@ class Device extends Item {
|
|
|
110
96
|
});
|
|
111
97
|
}
|
|
112
98
|
|
|
113
|
-
|
|
114
|
-
this.
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
this.__debug('onDisconnect');
|
|
134
|
-
|
|
135
|
-
const capabilityInstances = this.__capabilityInstances;
|
|
136
|
-
if (Object.keys(capabilityInstances).length > 0) {
|
|
137
|
-
// Get the device's latest values
|
|
138
|
-
// TODO: Optimize this with `getDevices()` when >1 device has >0 capability instances.
|
|
139
|
-
this.manager.getDevice({
|
|
140
|
-
id: this.id,
|
|
141
|
-
}).then(async device => {
|
|
142
|
-
Object.entries(capabilityInstances).forEach(([capabilityId, capabilityInstance]) => {
|
|
143
|
-
const value = device.capabilitiesObj
|
|
144
|
-
? typeof device.capabilitiesObj[capabilityId] !== 'undefined'
|
|
145
|
-
? device.capabilitiesObj[capabilityId].value
|
|
146
|
-
: null
|
|
147
|
-
: null;
|
|
148
|
-
|
|
149
|
-
capabilityInstance.__onCapabilityValue({
|
|
150
|
-
capabilityId,
|
|
151
|
-
value,
|
|
152
|
-
transactionId: Util.uuid(),
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
})
|
|
156
|
-
// eslint-disable-next-line no-console
|
|
157
|
-
.catch(err => console.error(`Device[${this.id}].onReconnectError:`, err));
|
|
158
|
-
}
|
|
159
|
-
},
|
|
160
|
-
onEvent: (event, data) => {
|
|
161
|
-
// // Fire event listeners
|
|
162
|
-
if (Array.isArray(this.__listeners[event])) {
|
|
163
|
-
this.__listeners[event].forEach(listener => listener(data));
|
|
164
|
-
}
|
|
165
|
-
},
|
|
99
|
+
onReconnect() {
|
|
100
|
+
const capabilityInstances = this.__capabilityInstances;
|
|
101
|
+
if (Object.keys(capabilityInstances).length > 0) {
|
|
102
|
+
// Get the device's latest values
|
|
103
|
+
// TODO: Optimize this with `getDevices()` when >1 device has >0 capability instances.
|
|
104
|
+
this.manager.getDevice({
|
|
105
|
+
id: this.id,
|
|
106
|
+
}).then(async device => {
|
|
107
|
+
Object.entries(capabilityInstances).forEach(([capabilityId, capabilityInstance]) => {
|
|
108
|
+
const value = device.capabilitiesObj
|
|
109
|
+
? typeof device.capabilitiesObj[capabilityId] !== 'undefined'
|
|
110
|
+
? device.capabilitiesObj[capabilityId].value
|
|
111
|
+
: null
|
|
112
|
+
: null;
|
|
113
|
+
|
|
114
|
+
capabilityInstance.__onCapabilityValue({
|
|
115
|
+
capabilityId,
|
|
116
|
+
value,
|
|
117
|
+
transactionId: Util.uuid(),
|
|
118
|
+
});
|
|
166
119
|
});
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// Delete the connecting Promise
|
|
173
|
-
this.__connectPromise
|
|
174
|
-
.catch(() => { })
|
|
175
|
-
.finally(() => {
|
|
176
|
-
delete this.__connectPromise;
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
await this.__connectPromise;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
async disconnect() {
|
|
183
|
-
this.__debug('disconnect');
|
|
184
|
-
|
|
185
|
-
// If connecting, await that first
|
|
186
|
-
try {
|
|
187
|
-
await this.__connectPromise;
|
|
188
|
-
} catch (err) { }
|
|
189
|
-
|
|
190
|
-
this.__disconnectPromise = Promise.resolve().then(async () => {
|
|
191
|
-
this.__connected = false;
|
|
192
|
-
|
|
193
|
-
if (this.io) {
|
|
194
|
-
this.io
|
|
195
|
-
.then(io => io.unsubscribe())
|
|
196
|
-
.catch(err => this.__debug('Error Disconnecting:', err));
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
// Delete the disconnecting Promise
|
|
201
|
-
this.__disconnectPromise
|
|
202
|
-
.catch(() => { })
|
|
203
|
-
.finally(() => {
|
|
204
|
-
delete this.__disconnectPromise;
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
await this.__disconnectPromise;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
destroy() {
|
|
211
|
-
super.destroy();
|
|
212
|
-
|
|
213
|
-
// Disconnect from Socket.io
|
|
214
|
-
this.disconnect().catch(() => { });
|
|
120
|
+
})
|
|
121
|
+
// eslint-disable-next-line no-console
|
|
122
|
+
.catch(err => console.error(`Device[${this.id}].onReconnectError:`, err));
|
|
123
|
+
}
|
|
215
124
|
}
|
|
216
125
|
|
|
217
126
|
}
|
|
@@ -5,6 +5,7 @@ const EventEmitter = require('../../EventEmitter');
|
|
|
5
5
|
class Item extends EventEmitter {
|
|
6
6
|
|
|
7
7
|
constructor({
|
|
8
|
+
uri,
|
|
8
9
|
key,
|
|
9
10
|
homey,
|
|
10
11
|
manager,
|
|
@@ -33,6 +34,20 @@ class Item extends EventEmitter {
|
|
|
33
34
|
writable: false,
|
|
34
35
|
});
|
|
35
36
|
|
|
37
|
+
// Set URI
|
|
38
|
+
Object.defineProperty(this, '__uri', {
|
|
39
|
+
value: uri,
|
|
40
|
+
enumerable: false,
|
|
41
|
+
writable: true,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Set Connected
|
|
45
|
+
Object.defineProperty(this, '__connected', {
|
|
46
|
+
value: false,
|
|
47
|
+
enumerable: false,
|
|
48
|
+
writable: true,
|
|
49
|
+
});
|
|
50
|
+
|
|
36
51
|
// Set Properties
|
|
37
52
|
for (const [key, value] of Object.entries(properties)) {
|
|
38
53
|
Object.defineProperty(this, key, {
|
|
@@ -54,9 +69,102 @@ class Item extends EventEmitter {
|
|
|
54
69
|
return this;
|
|
55
70
|
}
|
|
56
71
|
|
|
72
|
+
async connect() {
|
|
73
|
+
this.__debug('connect');
|
|
74
|
+
|
|
75
|
+
// If disconnecting, await that first
|
|
76
|
+
try {
|
|
77
|
+
await this.__disconnectPromise;
|
|
78
|
+
} catch (err) { }
|
|
79
|
+
|
|
80
|
+
this.__connectPromise = Promise.resolve().then(async () => {
|
|
81
|
+
if (!this.io) {
|
|
82
|
+
this.io = this.homey.subscribe(this.__uri, {
|
|
83
|
+
onConnect: () => {
|
|
84
|
+
this.__debug('onConnect');
|
|
85
|
+
this.__connected = true;
|
|
86
|
+
|
|
87
|
+
this.onConnect();
|
|
88
|
+
},
|
|
89
|
+
onDisconnect: () => {
|
|
90
|
+
this.__debug('onDisconnect');
|
|
91
|
+
this.__connected = false;
|
|
92
|
+
|
|
93
|
+
this.onDisconnect();
|
|
94
|
+
},
|
|
95
|
+
onReconnect: () => {
|
|
96
|
+
this.__debug('onDisconnect');
|
|
97
|
+
|
|
98
|
+
this.onReconnect();
|
|
99
|
+
},
|
|
100
|
+
onEvent: (event, data) => {
|
|
101
|
+
// // Fire event listeners
|
|
102
|
+
if (Array.isArray(this.__listeners[event])) {
|
|
103
|
+
this.__listeners[event].forEach(listener => listener(data));
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
await this.io;
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// Delete the connecting Promise
|
|
113
|
+
this.__connectPromise
|
|
114
|
+
.catch(() => { })
|
|
115
|
+
.finally(() => {
|
|
116
|
+
delete this.__connectPromise;
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
await this.__connectPromise;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async disconnect() {
|
|
123
|
+
this.__debug('disconnect');
|
|
124
|
+
|
|
125
|
+
// If connecting, await that first
|
|
126
|
+
try {
|
|
127
|
+
await this.__connectPromise;
|
|
128
|
+
} catch (err) { }
|
|
129
|
+
|
|
130
|
+
this.__disconnectPromise = Promise.resolve().then(async () => {
|
|
131
|
+
this.__connected = false;
|
|
132
|
+
|
|
133
|
+
if (this.io) {
|
|
134
|
+
this.io
|
|
135
|
+
.then(io => io.unsubscribe())
|
|
136
|
+
.catch(err => this.__debug('Error Disconnecting:', err));
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// Delete the disconnecting Promise
|
|
141
|
+
this.__disconnectPromise
|
|
142
|
+
.catch(() => { })
|
|
143
|
+
.finally(() => {
|
|
144
|
+
delete this.__disconnectPromise;
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
await this.__disconnectPromise;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
onConnect() {
|
|
151
|
+
// Overload Me
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
onReconnect() {
|
|
155
|
+
// Overload Me
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
onDisconnect() {
|
|
159
|
+
// Overload Me
|
|
160
|
+
}
|
|
161
|
+
|
|
57
162
|
destroy() {
|
|
58
163
|
// Remove all event listeners
|
|
59
164
|
this.removeAllListeners();
|
|
165
|
+
|
|
166
|
+
// Disconnect from Socket.io
|
|
167
|
+
this.disconnect().catch(() => { });
|
|
60
168
|
}
|
|
61
169
|
|
|
62
170
|
}
|
|
@@ -109,6 +109,7 @@ class Manager extends EventEmitter {
|
|
|
109
109
|
$validate = true,
|
|
110
110
|
$cache = true,
|
|
111
111
|
$timeout = 5000,
|
|
112
|
+
$socket = true,
|
|
112
113
|
$body = {},
|
|
113
114
|
$query = {},
|
|
114
115
|
$headers = {},
|
|
@@ -189,6 +190,14 @@ class Manager extends EventEmitter {
|
|
|
189
190
|
}
|
|
190
191
|
}
|
|
191
192
|
|
|
193
|
+
// Append query to path
|
|
194
|
+
if (Object.keys(query).length > 0) {
|
|
195
|
+
const queryString = Object.entries(query).map(([key, value]) => {
|
|
196
|
+
return `${key}=${encodeURIComponent(value)}`;
|
|
197
|
+
}).join('&');
|
|
198
|
+
path = `${path}?${queryString}`;
|
|
199
|
+
}
|
|
200
|
+
|
|
192
201
|
let result;
|
|
193
202
|
const benchmark = Util.benchmark();
|
|
194
203
|
|
|
@@ -236,7 +245,7 @@ class Manager extends EventEmitter {
|
|
|
236
245
|
// If Homey is connected to Socket.io,
|
|
237
246
|
// send the API request to socket.io.
|
|
238
247
|
// This is about ~2x faster than HTTP
|
|
239
|
-
if (this.homey.isConnected()) {
|
|
248
|
+
if (this.homey.isConnected() && $socket === true) {
|
|
240
249
|
result = await Util.timeout(new Promise((resolve, reject) => {
|
|
241
250
|
this.homey.__ioNamespace.emit('api', {
|
|
242
251
|
args,
|
|
@@ -268,13 +277,20 @@ class Manager extends EventEmitter {
|
|
|
268
277
|
if (itemType === 'id') return props.id;
|
|
269
278
|
throw new Error('Invalid Item Type');
|
|
270
279
|
};
|
|
280
|
+
const getItemUri = props => {
|
|
281
|
+
if (itemType === 'filter') return null;
|
|
282
|
+
if (itemType === 'id') return `homey:${itemId}:${props.id}`;
|
|
283
|
+
throw new Error('Invalid Item Type');
|
|
284
|
+
};
|
|
271
285
|
|
|
272
286
|
switch (operation.crud.type) {
|
|
273
287
|
case 'getOne': {
|
|
274
288
|
const key = getItemKey(result);
|
|
289
|
+
const uri = getItemUri(result);
|
|
275
290
|
|
|
276
291
|
result = new ItemClass({
|
|
277
292
|
key,
|
|
293
|
+
uri,
|
|
278
294
|
homey: this.homey,
|
|
279
295
|
manager: this,
|
|
280
296
|
properties: { ...result },
|
|
@@ -290,12 +306,14 @@ class Manager extends EventEmitter {
|
|
|
290
306
|
// Add all to cache
|
|
291
307
|
for (const [resultKey, item] of Object.entries(result)) {
|
|
292
308
|
const key = getItemKey(item);
|
|
309
|
+
const uri = getItemUri(item);
|
|
293
310
|
|
|
294
311
|
if (this.__cache[itemId][key]) {
|
|
295
312
|
result[resultKey] = this.__cache[itemId][key].__update(item);
|
|
296
313
|
} else {
|
|
297
314
|
result[resultKey] = new ItemClass({
|
|
298
315
|
key,
|
|
316
|
+
uri,
|
|
299
317
|
homey: this.homey,
|
|
300
318
|
manager: this,
|
|
301
319
|
properties: { ...item },
|
|
@@ -326,11 +344,14 @@ class Manager extends EventEmitter {
|
|
|
326
344
|
case 'createOne':
|
|
327
345
|
case 'updateOne': {
|
|
328
346
|
const key = getItemKey(result);
|
|
347
|
+
const uri = getItemUri(result);
|
|
348
|
+
|
|
329
349
|
if (this.__cache[itemId][key]) {
|
|
330
350
|
result = this.__cache[itemId][key].__update(result);
|
|
331
351
|
} else {
|
|
332
352
|
result = new ItemClass({
|
|
333
353
|
key,
|
|
354
|
+
uri,
|
|
334
355
|
manager: this,
|
|
335
356
|
properties: result,
|
|
336
357
|
});
|
|
@@ -343,6 +364,7 @@ class Manager extends EventEmitter {
|
|
|
343
364
|
}
|
|
344
365
|
case 'deleteOne': {
|
|
345
366
|
const key = getItemKey(args);
|
|
367
|
+
|
|
346
368
|
if (this.__cache[itemId][key]) {
|
|
347
369
|
this.__cache[itemId][key].destroy();
|
|
348
370
|
delete this.__cache[itemId][key];
|
|
@@ -416,11 +438,15 @@ class Manager extends EventEmitter {
|
|
|
416
438
|
const key = itemType === 'filter'
|
|
417
439
|
? `${data.uri}:${data.id}`
|
|
418
440
|
: `${data.id}`;
|
|
441
|
+
const uri = itemType === 'filter'
|
|
442
|
+
? null
|
|
443
|
+
: `homey:${itemId}:${data.id}`;
|
|
419
444
|
|
|
420
445
|
switch (operation) {
|
|
421
446
|
case 'create': {
|
|
422
447
|
this.__cache[itemId][key] = new ItemClass({
|
|
423
448
|
key,
|
|
449
|
+
uri,
|
|
424
450
|
manager: this,
|
|
425
451
|
properties: { ...data },
|
|
426
452
|
});
|
|
@@ -437,6 +463,7 @@ class Manager extends EventEmitter {
|
|
|
437
463
|
} else {
|
|
438
464
|
this.__cache[itemId][key] = new ItemClass({
|
|
439
465
|
key,
|
|
466
|
+
uri,
|
|
440
467
|
manager: this,
|
|
441
468
|
properties: { ...data },
|
|
442
469
|
});
|