homey-api 1.5.20 → 1.5.21
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/HomeyAPI/HomeyAPIV2.js +27 -37
- package/package.json +1 -1
|
@@ -211,40 +211,39 @@ class HomeyAPIV2 extends HomeyAPI {
|
|
|
211
211
|
|
|
212
212
|
// Ping method
|
|
213
213
|
const ping = async (strategyId, timeout) => {
|
|
214
|
+
let pingTimeout;
|
|
214
215
|
const baseUrl = urls[strategyId];
|
|
215
|
-
|
|
216
|
+
return Promise.race([
|
|
216
217
|
Util.fetch(`${baseUrl}/api/manager/system/ping?id=${this.id}`, {
|
|
217
218
|
headers: {
|
|
218
219
|
'X-Homey-ID': this.id,
|
|
219
220
|
},
|
|
221
|
+
}).then(async res => {
|
|
222
|
+
const text = await res.text();
|
|
223
|
+
if (!res.ok) throw new Error(text || res.statusText);
|
|
224
|
+
if (text === 'false') throw new Error('Invalid Homey ID');
|
|
225
|
+
|
|
226
|
+
const homeyId = res.headers.get('X-Homey-ID');
|
|
227
|
+
if (homeyId) {
|
|
228
|
+
if (homeyId !== this.id) throw new Error('Invalid Homey ID'); // TODO: Add to Homey Connect
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Set the version that Homey told us.
|
|
232
|
+
// It's the absolute truth, because the Cloud API may be behind.
|
|
233
|
+
const homeyVersion = res.headers.get('X-Homey-Version');
|
|
234
|
+
if (homeyVersion !== this.version) {
|
|
235
|
+
this.version = homeyVersion;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
baseUrl,
|
|
240
|
+
strategyId,
|
|
241
|
+
};
|
|
220
242
|
}),
|
|
221
243
|
new Promise((_, reject) => {
|
|
222
|
-
|
|
223
|
-
promise
|
|
224
|
-
.catch(() => { })
|
|
225
|
-
.finally(() => clearTimeout(pingTimeout));
|
|
244
|
+
pingTimeout = setTimeout(() => reject(new Error('PingTimeout')), timeout);
|
|
226
245
|
}),
|
|
227
|
-
]);
|
|
228
|
-
const text = await res.text();
|
|
229
|
-
if (!res.ok) throw new Error(text || res.statusText);
|
|
230
|
-
if (text === 'false') throw new Error('Invalid Homey ID');
|
|
231
|
-
|
|
232
|
-
const homeyId = res.headers.get('X-Homey-ID');
|
|
233
|
-
if (homeyId) {
|
|
234
|
-
if (homeyId !== this.id) throw new Error('Invalid Homey ID'); // TODO: Add to Homey Connect
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// Set the version that Homey told us.
|
|
238
|
-
// It's the absolute truth, because the Cloud API may be behind.
|
|
239
|
-
const homeyVersion = res.headers.get('X-Homey-Version');
|
|
240
|
-
if (homeyVersion !== this.version) {
|
|
241
|
-
this.version = homeyVersion;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
return {
|
|
245
|
-
baseUrl,
|
|
246
|
-
strategyId,
|
|
247
|
-
};
|
|
246
|
+
]).finally(() => clearTimeout(pingTimeout));
|
|
248
247
|
};
|
|
249
248
|
|
|
250
249
|
const pings = {};
|
|
@@ -296,19 +295,10 @@ class HomeyAPIV2 extends HomeyAPI {
|
|
|
296
295
|
throw new HomeyOfflineError();
|
|
297
296
|
}
|
|
298
297
|
|
|
299
|
-
return Promise.
|
|
298
|
+
return Promise.any(promises);
|
|
300
299
|
})
|
|
301
300
|
.then(result => resolve(result))
|
|
302
|
-
.catch(err =>
|
|
303
|
-
// Last resort: try cloud
|
|
304
|
-
if (pings[HomeyAPI.DISCOVERY_STRATEGIES.CLOUD]) {
|
|
305
|
-
return pings[HomeyAPI.DISCOVERY_STRATEGIES.CLOUD].catch(err => {
|
|
306
|
-
throw new HomeyOfflineError(err);
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
reject(new HomeyOfflineError(err));
|
|
311
|
-
});
|
|
301
|
+
.catch(err => reject(new HomeyOfflineError(err)));
|
|
312
302
|
} else if (pings[HomeyAPI.DISCOVERY_STRATEGIES.LOCAL]) {
|
|
313
303
|
pings[HomeyAPI.DISCOVERY_STRATEGIES.LOCAL]
|
|
314
304
|
.then(result => resolve(result))
|