homebridge-tydom 0.27.2 → 0.29.0

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/README.md CHANGED
@@ -127,7 +127,7 @@ You can either add a `pin` field:
127
127
  }
128
128
  ```
129
129
 
130
- Or you can also use an environment variable `HOMEBRIDGE_TYDOM_PIN` with the base64 encoded value of your pin (might be safer than having it inside your `config.json`).
130
+ Or you can also use an environment variable `HOMEBRIDGE_TYDOM_PIN` with the base64 encoded value of your pin (useful for containerized deployments where secrets are injected via environment variables).
131
131
 
132
132
  You can optionnaly rename zones (default is `Zone 1`, `Zone 2`, etc.),
133
133
 
package/dist/index.js CHANGED
@@ -236,6 +236,9 @@ var getTydomDeviceData = async (client, { deviceId, endpointId }) => {
236
236
  }
237
237
  return res.data ? res.data : res;
238
238
  });
239
+ promise.catch(() => {
240
+ cacheMap.delete(uri);
241
+ });
239
242
  cacheMap.set(uri, { time: now, promise });
240
243
  return promise;
241
244
  };
@@ -249,6 +252,9 @@ var runTydomDeviceCommand = async (client, name, { deviceId, endpointId, searchP
249
252
  }
250
253
  }
251
254
  const promise = client.command(uri);
255
+ promise.catch(() => {
256
+ cacheMap.delete(uri);
257
+ });
252
258
  cacheMap.set(uri, { time: now, promise });
253
259
  return promise;
254
260
  };
@@ -376,6 +382,7 @@ var TydomController = class extends import_events.EventEmitter {
376
382
  devices = /* @__PURE__ */ new Map();
377
383
  state = /* @__PURE__ */ new Map();
378
384
  refreshInterval;
385
+ hasConnectedOnce = false;
379
386
  constructor(log, config) {
380
387
  super();
381
388
  this.config = config;
@@ -402,10 +409,20 @@ var TydomController = class extends import_events.EventEmitter {
402
409
  this.log.info(
403
410
  `Successfully connected to Tydom hostname=${chalkString(hostname)} with username=${chalkString(username)}`
404
411
  );
412
+ if (this.hasConnectedOnce) {
413
+ this.log.warn(`Reconnected to Tydom hostname=${chalkString(hostname)}, re-syncing state...`);
414
+ this.resync().catch((err) => {
415
+ this.log.error(`Failed to re-sync after reconnection: ${stringifyError(err)}`);
416
+ });
417
+ }
405
418
  this.emit("connect");
406
419
  });
407
420
  this.client.on("disconnect", () => {
408
421
  this.log.warn(`Disconnected from Tydom hostname=${chalkString(hostname)}"`);
422
+ if (this.refreshInterval) {
423
+ clearInterval(this.refreshInterval);
424
+ this.refreshInterval = void 0;
425
+ }
409
426
  this.emit("disconnect");
410
427
  });
411
428
  }
@@ -423,6 +440,7 @@ var TydomController = class extends import_events.EventEmitter {
423
440
  await this.client.connect();
424
441
  await asyncWait(250);
425
442
  await this.client.get("/ping");
443
+ this.hasConnectedOnce = true;
426
444
  } catch (err) {
427
445
  this.log.error(`Failed to connect to Tydom hostname=${hostname} with username="${username}"`);
428
446
  throw err;
@@ -537,6 +555,24 @@ var TydomController = class extends import_events.EventEmitter {
537
555
  debug(`Refreshing Tydom controller ...`);
538
556
  await this.client.post("/refresh/all");
539
557
  }
558
+ async resync() {
559
+ const { hostname, refreshInterval = DEFAULT_REFRESH_INTERVAL_SEC } = this.config;
560
+ debug(`Re-syncing state after reconnection to hostname=${chalkString(hostname)}...`);
561
+ await asyncWait(250);
562
+ await this.client.get("/ping");
563
+ await this.refresh();
564
+ if (this.refreshInterval) {
565
+ clearInterval(this.refreshInterval);
566
+ }
567
+ debug(`Re-configuring refresh interval of ${chalkNumber(Math.round(refreshInterval))}s`);
568
+ this.refreshInterval = setInterval(async () => {
569
+ try {
570
+ await this.refresh();
571
+ } catch (err) {
572
+ debug(`Failed interval refresh with err ${err}`);
573
+ }
574
+ }, refreshInterval * 1e3);
575
+ }
540
576
  handleMessage(message) {
541
577
  const { uri, method, body } = message;
542
578
  const isDeviceUpdate = uri === "/devices/data" && method === "PUT";
@@ -4413,7 +4449,24 @@ var TydomPlatform = class {
4413
4449
  async didFinishLaunching() {
4414
4450
  assert(this.controller);
4415
4451
  this.cleanupAccessoriesIds = new Set(this.accessories.keys());
4416
- await this.controller.connect();
4452
+ const maxRetries = 10;
4453
+ const maxDelay = 5 * 60 * 1e3;
4454
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
4455
+ try {
4456
+ await this.controller.connect();
4457
+ break;
4458
+ } catch (err) {
4459
+ if (attempt === maxRetries) {
4460
+ this.log.error(
4461
+ `Failed to connect after ${maxRetries} retries, giving up: ${stringifyError(err)}`
4462
+ );
4463
+ return;
4464
+ }
4465
+ const delay = Math.min(5e3 * Math.pow(2, attempt), maxDelay);
4466
+ this.log.warn(`Connection attempt ${attempt + 1} failed, retrying in ${Math.round(delay / 1e3)}s...`);
4467
+ await new Promise((resolve) => setTimeout(resolve, delay));
4468
+ }
4469
+ }
4417
4470
  await this.controller.scan();
4418
4471
  this.cleanupAccessoriesIds.forEach((accessoryId) => {
4419
4472
  const accessory = this.accessories.get(accessoryId);