homey-api 3.17.5 → 3.17.7
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/assets/types/homey-api.private.d.ts +8 -0
- package/lib/HomeyAPI/HomeyAPIV3/DiscoveryManager.js +63 -43
- package/lib/HomeyAPI/HomeyAPIV3/DiscoveryNodeTransport.js +589 -0
- package/lib/HomeyAPI/HomeyAPIV3/SocketSession.js +106 -12
- package/lib/HomeyAPI/HomeyAPIV3/SubscriptionRegistry.js +20 -16
- package/lib/HomeyAPI/HomeyAPIV3.js +15 -1
- package/lib/Util.js +20 -256
- package/package.json +1 -1
|
@@ -5748,6 +5748,8 @@ export class Util {
|
|
|
5748
5748
|
timeoutDuration?: number,
|
|
5749
5749
|
|
|
5750
5750
|
timeoutMessage?: string,
|
|
5751
|
+
|
|
5752
|
+
patchOptions?: Function,
|
|
5751
5753
|
): Promise<any>;
|
|
5752
5754
|
|
|
5753
5755
|
static wait(ms: number): Promise<void>;
|
|
@@ -5790,6 +5792,8 @@ export class Util {
|
|
|
5790
5792
|
timeoutDuration?: number,
|
|
5791
5793
|
|
|
5792
5794
|
timeoutMessage?: string,
|
|
5795
|
+
|
|
5796
|
+
patchOptions?: Function,
|
|
5793
5797
|
): Promise<any>;
|
|
5794
5798
|
|
|
5795
5799
|
static wait(ms: number): Promise<void>;
|
|
@@ -9166,6 +9170,8 @@ export class Util {
|
|
|
9166
9170
|
timeoutDuration?: number,
|
|
9167
9171
|
|
|
9168
9172
|
timeoutMessage?: string,
|
|
9173
|
+
|
|
9174
|
+
patchOptions?: Function,
|
|
9169
9175
|
): Promise<any>;
|
|
9170
9176
|
|
|
9171
9177
|
static wait(ms: number): Promise<void>;
|
|
@@ -9208,6 +9214,8 @@ export class Util {
|
|
|
9208
9214
|
timeoutDuration?: number,
|
|
9209
9215
|
|
|
9210
9216
|
timeoutMessage?: string,
|
|
9217
|
+
|
|
9218
|
+
patchOptions?: Function,
|
|
9211
9219
|
): Promise<any>;
|
|
9212
9220
|
|
|
9213
9221
|
static wait(ms: number): Promise<void>;
|
|
@@ -5,9 +5,16 @@ const APIErrorHomeySubscriptionInactive = require('../../APIErrorHomeySubscripti
|
|
|
5
5
|
const APIErrorHomeyInvalidSerialNumber = require('../../APIErrorHomeyInvalidSerialNumber');
|
|
6
6
|
const Util = require('../../Util');
|
|
7
7
|
|
|
8
|
+
function getRuntimeRequire() {
|
|
9
|
+
// Avoid webpack statically bundling Node-only discovery transport into the browser build.
|
|
10
|
+
// eslint-disable-next-line no-eval
|
|
11
|
+
return eval('require');
|
|
12
|
+
}
|
|
13
|
+
|
|
8
14
|
class DiscoveryManager {
|
|
9
15
|
constructor(homey) {
|
|
10
16
|
this.homey = homey;
|
|
17
|
+
this.discoveryNodeTransport = null;
|
|
11
18
|
}
|
|
12
19
|
|
|
13
20
|
async discoverBaseUrl() {
|
|
@@ -17,7 +24,10 @@ class DiscoveryManager {
|
|
|
17
24
|
if (this.homey.__strategies.includes(DISCOVERY_STRATEGIES.MDNS)) {
|
|
18
25
|
if (Util.isHTTPUnsecureSupported()) {
|
|
19
26
|
if (this.homey.__properties.mdnsFqdn) {
|
|
20
|
-
const port =
|
|
27
|
+
const port =
|
|
28
|
+
this.homey.__properties.portHttp && this.homey.__properties.portHttp !== 80
|
|
29
|
+
? `:${this.homey.__properties.portHttp}`
|
|
30
|
+
: '';
|
|
21
31
|
urls[DISCOVERY_STRATEGIES.MDNS] = `http://${this.homey.__properties.mdnsFqdn}${port}`;
|
|
22
32
|
} else {
|
|
23
33
|
urls[DISCOVERY_STRATEGIES.MDNS] = `http://homey-${this.homey.id}.local`;
|
|
@@ -112,18 +122,24 @@ class DiscoveryManager {
|
|
|
112
122
|
|
|
113
123
|
const ping = async (strategyId, timeout) => {
|
|
114
124
|
const baseUrl = urls[strategyId];
|
|
125
|
+
const pingUrl = `${baseUrl}/api/manager/system/ping?id=${this.homey.id}`;
|
|
115
126
|
const abortController = new AbortController();
|
|
116
127
|
pingAbortControllers[strategyId] = abortController;
|
|
117
|
-
|
|
118
|
-
const
|
|
119
|
-
|
|
128
|
+
const discoveryTransport = this.getDiscoveryNodeTransport();
|
|
129
|
+
const fetch =
|
|
130
|
+
discoveryTransport && discoveryTransport.shouldUseForUrl(pingUrl)
|
|
131
|
+
? discoveryTransport.fetch.bind(discoveryTransport)
|
|
132
|
+
: Util.fetch.bind(Util);
|
|
133
|
+
|
|
134
|
+
const response = await fetch(
|
|
135
|
+
pingUrl,
|
|
120
136
|
{
|
|
121
137
|
headers: {
|
|
122
138
|
'X-Homey-ID': this.homey.id,
|
|
123
139
|
},
|
|
124
140
|
signal: abortController.signal,
|
|
125
141
|
},
|
|
126
|
-
timeout
|
|
142
|
+
timeout,
|
|
127
143
|
);
|
|
128
144
|
|
|
129
145
|
const text = await response.text();
|
|
@@ -180,15 +196,9 @@ class DiscoveryManager {
|
|
|
180
196
|
const pings = {};
|
|
181
197
|
|
|
182
198
|
if (urls[DISCOVERY_STRATEGIES.LOCAL_SECURE]) {
|
|
183
|
-
pings[DISCOVERY_STRATEGIES.LOCAL_SECURE] = ping(
|
|
184
|
-
DISCOVERY_STRATEGIES.LOCAL_SECURE,
|
|
185
|
-
1200
|
|
186
|
-
);
|
|
199
|
+
pings[DISCOVERY_STRATEGIES.LOCAL_SECURE] = ping(DISCOVERY_STRATEGIES.LOCAL_SECURE, 1200);
|
|
187
200
|
pings[DISCOVERY_STRATEGIES.LOCAL_SECURE].catch((err) => {
|
|
188
|
-
this.homey.__debug(
|
|
189
|
-
`Ping ${DISCOVERY_STRATEGIES.LOCAL_SECURE} Error:`,
|
|
190
|
-
err && err.message
|
|
191
|
-
);
|
|
201
|
+
this.homey.__debug(`Ping ${DISCOVERY_STRATEGIES.LOCAL_SECURE} Error:`, err && err.message);
|
|
192
202
|
this.homey.__debug(urls[DISCOVERY_STRATEGIES.LOCAL_SECURE]);
|
|
193
203
|
});
|
|
194
204
|
}
|
|
@@ -196,34 +206,34 @@ class DiscoveryManager {
|
|
|
196
206
|
if (urls[DISCOVERY_STRATEGIES.LOCAL]) {
|
|
197
207
|
pings[DISCOVERY_STRATEGIES.LOCAL] = ping(DISCOVERY_STRATEGIES.LOCAL, 1000);
|
|
198
208
|
pings[DISCOVERY_STRATEGIES.LOCAL].catch((err) =>
|
|
199
|
-
this.homey.__debug(`Ping ${DISCOVERY_STRATEGIES.LOCAL} Error:`, err && err.message)
|
|
209
|
+
this.homey.__debug(`Ping ${DISCOVERY_STRATEGIES.LOCAL} Error:`, err && err.message),
|
|
200
210
|
);
|
|
201
211
|
}
|
|
202
212
|
|
|
203
213
|
if (urls[DISCOVERY_STRATEGIES.MDNS]) {
|
|
204
214
|
pings[DISCOVERY_STRATEGIES.MDNS] = ping(DISCOVERY_STRATEGIES.MDNS, 3000);
|
|
205
215
|
pings[DISCOVERY_STRATEGIES.MDNS].catch((err) =>
|
|
206
|
-
this.homey.__debug(`Ping ${DISCOVERY_STRATEGIES.MDNS} Error:`, err && err.message)
|
|
216
|
+
this.homey.__debug(`Ping ${DISCOVERY_STRATEGIES.MDNS} Error:`, err && err.message),
|
|
207
217
|
);
|
|
208
218
|
}
|
|
209
219
|
|
|
210
220
|
if (urls[DISCOVERY_STRATEGIES.CLOUD]) {
|
|
211
221
|
pings[DISCOVERY_STRATEGIES.CLOUD] = ping(DISCOVERY_STRATEGIES.CLOUD, 5000);
|
|
212
222
|
pings[DISCOVERY_STRATEGIES.CLOUD].catch((err) =>
|
|
213
|
-
this.homey.__debug(`Ping ${DISCOVERY_STRATEGIES.CLOUD} Error:`, err && err.message)
|
|
223
|
+
this.homey.__debug(`Ping ${DISCOVERY_STRATEGIES.CLOUD} Error:`, err && err.message),
|
|
214
224
|
);
|
|
215
225
|
}
|
|
216
226
|
|
|
217
227
|
if (urls[DISCOVERY_STRATEGIES.REMOTE_FORWARDED]) {
|
|
218
228
|
pings[DISCOVERY_STRATEGIES.REMOTE_FORWARDED] = ping(
|
|
219
229
|
DISCOVERY_STRATEGIES.REMOTE_FORWARDED,
|
|
220
|
-
2000
|
|
230
|
+
2000,
|
|
221
231
|
);
|
|
222
232
|
pings[DISCOVERY_STRATEGIES.REMOTE_FORWARDED].catch((err) =>
|
|
223
233
|
this.homey.__debug(
|
|
224
234
|
`Ping ${DISCOVERY_STRATEGIES.REMOTE_FORWARDED} Error:`,
|
|
225
|
-
err && err.message
|
|
226
|
-
)
|
|
235
|
+
err && err.message,
|
|
236
|
+
),
|
|
227
237
|
);
|
|
228
238
|
}
|
|
229
239
|
|
|
@@ -240,36 +250,35 @@ class DiscoveryManager {
|
|
|
240
250
|
let selectedRoutePromise;
|
|
241
251
|
|
|
242
252
|
if (pings[DISCOVERY_STRATEGIES.LOCAL_SECURE]) {
|
|
243
|
-
selectedRoutePromise = pings[DISCOVERY_STRATEGIES.LOCAL_SECURE]
|
|
244
|
-
|
|
245
|
-
const fallbackPromises = [];
|
|
253
|
+
selectedRoutePromise = pings[DISCOVERY_STRATEGIES.LOCAL_SECURE].catch((error) => {
|
|
254
|
+
const fallbackPromises = [];
|
|
246
255
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
256
|
+
if (pings[DISCOVERY_STRATEGIES.LOCAL]) {
|
|
257
|
+
fallbackPromises.push(pings[DISCOVERY_STRATEGIES.LOCAL]);
|
|
258
|
+
}
|
|
250
259
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
260
|
+
if (pings[DISCOVERY_STRATEGIES.REMOTE_FORWARDED]) {
|
|
261
|
+
fallbackPromises.push(pings[DISCOVERY_STRATEGIES.REMOTE_FORWARDED]);
|
|
262
|
+
}
|
|
254
263
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
264
|
+
if (pings[DISCOVERY_STRATEGIES.MDNS]) {
|
|
265
|
+
fallbackPromises.push(pings[DISCOVERY_STRATEGIES.MDNS]);
|
|
266
|
+
}
|
|
258
267
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
268
|
+
if (pings[DISCOVERY_STRATEGIES.CLOUD]) {
|
|
269
|
+
fallbackPromises.push(pings[DISCOVERY_STRATEGIES.CLOUD]);
|
|
270
|
+
}
|
|
262
271
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
272
|
+
if (isSubscriptionError(error)) {
|
|
273
|
+
throw error;
|
|
274
|
+
}
|
|
266
275
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
276
|
+
if (!fallbackPromises.length) {
|
|
277
|
+
throw new APIErrorHomeyOffline();
|
|
278
|
+
}
|
|
270
279
|
|
|
271
|
-
|
|
272
|
-
|
|
280
|
+
return Promise.any(fallbackPromises);
|
|
281
|
+
});
|
|
273
282
|
} else if (pings[DISCOVERY_STRATEGIES.LOCAL]) {
|
|
274
283
|
selectedRoutePromise = withCloudFallback(pings[DISCOVERY_STRATEGIES.LOCAL]);
|
|
275
284
|
} else if (pings[DISCOVERY_STRATEGIES.MDNS]) {
|
|
@@ -290,6 +299,17 @@ class DiscoveryManager {
|
|
|
290
299
|
|
|
291
300
|
return promise;
|
|
292
301
|
}
|
|
302
|
+
|
|
303
|
+
getDiscoveryNodeTransport() {
|
|
304
|
+
if (!Util.isNodeJS()) {
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
this.discoveryNodeTransport =
|
|
309
|
+
this.discoveryNodeTransport || getRuntimeRequire()('./DiscoveryNodeTransport');
|
|
310
|
+
|
|
311
|
+
return this.discoveryNodeTransport;
|
|
312
|
+
}
|
|
293
313
|
}
|
|
294
314
|
|
|
295
|
-
module.exports = DiscoveryManager;
|
|
315
|
+
module.exports = DiscoveryManager;
|