homey-api 1.5.22 → 1.5.25
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,
|
|
@@ -12,7 +13,7 @@ class Item extends EventEmitter {
|
|
|
12
13
|
}) {
|
|
13
14
|
super();
|
|
14
15
|
|
|
15
|
-
// Set
|
|
16
|
+
// Set Homey
|
|
16
17
|
Object.defineProperty(this, 'homey', {
|
|
17
18
|
value: homey,
|
|
18
19
|
enumerable: false,
|
|
@@ -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,101 @@ 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
|
+
this.__debug('onEvent', event, data);
|
|
102
|
+
|
|
103
|
+
this.emit(event, data);
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
await this.io;
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Delete the connecting Promise
|
|
112
|
+
this.__connectPromise
|
|
113
|
+
.catch(() => { })
|
|
114
|
+
.finally(() => {
|
|
115
|
+
delete this.__connectPromise;
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
await this.__connectPromise;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async disconnect() {
|
|
122
|
+
this.__debug('disconnect');
|
|
123
|
+
|
|
124
|
+
// If connecting, await that first
|
|
125
|
+
try {
|
|
126
|
+
await this.__connectPromise;
|
|
127
|
+
} catch (err) { }
|
|
128
|
+
|
|
129
|
+
this.__disconnectPromise = Promise.resolve().then(async () => {
|
|
130
|
+
this.__connected = false;
|
|
131
|
+
|
|
132
|
+
if (this.io) {
|
|
133
|
+
this.io
|
|
134
|
+
.then(io => io.unsubscribe())
|
|
135
|
+
.catch(err => this.__debug('Error Disconnecting:', err));
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// Delete the disconnecting Promise
|
|
140
|
+
this.__disconnectPromise
|
|
141
|
+
.catch(() => { })
|
|
142
|
+
.finally(() => {
|
|
143
|
+
delete this.__disconnectPromise;
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
await this.__disconnectPromise;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
onConnect() {
|
|
150
|
+
// Overload Me
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
onReconnect() {
|
|
154
|
+
// Overload Me
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
onDisconnect() {
|
|
158
|
+
// Overload Me
|
|
159
|
+
}
|
|
160
|
+
|
|
57
161
|
destroy() {
|
|
58
162
|
// Remove all event listeners
|
|
59
163
|
this.removeAllListeners();
|
|
164
|
+
|
|
165
|
+
// Disconnect from Socket.io
|
|
166
|
+
this.disconnect().catch(() => { });
|
|
60
167
|
}
|
|
61
168
|
|
|
62
169
|
}
|
|
@@ -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,15 @@ 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,
|
|
355
|
+
homey: this.homey,
|
|
334
356
|
manager: this,
|
|
335
357
|
properties: result,
|
|
336
358
|
});
|
|
@@ -343,6 +365,7 @@ class Manager extends EventEmitter {
|
|
|
343
365
|
}
|
|
344
366
|
case 'deleteOne': {
|
|
345
367
|
const key = getItemKey(args);
|
|
368
|
+
|
|
346
369
|
if (this.__cache[itemId][key]) {
|
|
347
370
|
this.__cache[itemId][key].destroy();
|
|
348
371
|
delete this.__cache[itemId][key];
|
|
@@ -416,11 +439,16 @@ class Manager extends EventEmitter {
|
|
|
416
439
|
const key = itemType === 'filter'
|
|
417
440
|
? `${data.uri}:${data.id}`
|
|
418
441
|
: `${data.id}`;
|
|
442
|
+
const uri = itemType === 'filter'
|
|
443
|
+
? null
|
|
444
|
+
: `homey:${itemId}:${data.id}`;
|
|
419
445
|
|
|
420
446
|
switch (operation) {
|
|
421
447
|
case 'create': {
|
|
422
448
|
this.__cache[itemId][key] = new ItemClass({
|
|
423
449
|
key,
|
|
450
|
+
uri,
|
|
451
|
+
homey: this.homey,
|
|
424
452
|
manager: this,
|
|
425
453
|
properties: { ...data },
|
|
426
454
|
});
|
|
@@ -437,6 +465,8 @@ class Manager extends EventEmitter {
|
|
|
437
465
|
} else {
|
|
438
466
|
this.__cache[itemId][key] = new ItemClass({
|
|
439
467
|
key,
|
|
468
|
+
uri,
|
|
469
|
+
homey: this.homey,
|
|
440
470
|
manager: this,
|
|
441
471
|
properties: { ...data },
|
|
442
472
|
});
|
|
@@ -437,7 +437,10 @@ class HomeyAPIV2 extends HomeyAPI {
|
|
|
437
437
|
// and then sending the JWT token to Homey.
|
|
438
438
|
this.__debug('Retrieving token...');
|
|
439
439
|
const jwtToken = await this.__api.createDelegationToken({ audience: 'homey' });
|
|
440
|
-
const token = await this.users.login({
|
|
440
|
+
const token = await this.users.login({
|
|
441
|
+
$socket: false,
|
|
442
|
+
token: jwtToken,
|
|
443
|
+
});
|
|
441
444
|
await this.__setStore({ token });
|
|
442
445
|
this.__debug('Got token');
|
|
443
446
|
|
|
@@ -486,6 +489,7 @@ class HomeyAPIV2 extends HomeyAPI {
|
|
|
486
489
|
|
|
487
490
|
await this.connect();
|
|
488
491
|
await new Promise((resolve, reject) => {
|
|
492
|
+
this.__ioNamespace.once('disconnect', reject);
|
|
489
493
|
this.__ioNamespace.emit('subscribe', uri, err => {
|
|
490
494
|
if (err) return reject(err);
|
|
491
495
|
return resolve();
|