homebridge-tydom 0.27.2 → 0.28.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
@@ -376,6 +376,7 @@ var TydomController = class extends import_events.EventEmitter {
376
376
  devices = /* @__PURE__ */ new Map();
377
377
  state = /* @__PURE__ */ new Map();
378
378
  refreshInterval;
379
+ hasConnectedOnce = false;
379
380
  constructor(log, config) {
380
381
  super();
381
382
  this.config = config;
@@ -402,10 +403,20 @@ var TydomController = class extends import_events.EventEmitter {
402
403
  this.log.info(
403
404
  `Successfully connected to Tydom hostname=${chalkString(hostname)} with username=${chalkString(username)}`
404
405
  );
406
+ if (this.hasConnectedOnce) {
407
+ this.log.warn(`Reconnected to Tydom hostname=${chalkString(hostname)}, re-syncing state...`);
408
+ this.resync().catch((err) => {
409
+ this.log.error(`Failed to re-sync after reconnection: ${stringifyError(err)}`);
410
+ });
411
+ }
405
412
  this.emit("connect");
406
413
  });
407
414
  this.client.on("disconnect", () => {
408
415
  this.log.warn(`Disconnected from Tydom hostname=${chalkString(hostname)}"`);
416
+ if (this.refreshInterval) {
417
+ clearInterval(this.refreshInterval);
418
+ this.refreshInterval = void 0;
419
+ }
409
420
  this.emit("disconnect");
410
421
  });
411
422
  }
@@ -423,6 +434,7 @@ var TydomController = class extends import_events.EventEmitter {
423
434
  await this.client.connect();
424
435
  await asyncWait(250);
425
436
  await this.client.get("/ping");
437
+ this.hasConnectedOnce = true;
426
438
  } catch (err) {
427
439
  this.log.error(`Failed to connect to Tydom hostname=${hostname} with username="${username}"`);
428
440
  throw err;
@@ -537,6 +549,24 @@ var TydomController = class extends import_events.EventEmitter {
537
549
  debug(`Refreshing Tydom controller ...`);
538
550
  await this.client.post("/refresh/all");
539
551
  }
552
+ async resync() {
553
+ const { hostname, refreshInterval = DEFAULT_REFRESH_INTERVAL_SEC } = this.config;
554
+ debug(`Re-syncing state after reconnection to hostname=${chalkString(hostname)}...`);
555
+ await asyncWait(250);
556
+ await this.client.get("/ping");
557
+ await this.refresh();
558
+ if (this.refreshInterval) {
559
+ clearInterval(this.refreshInterval);
560
+ }
561
+ debug(`Re-configuring refresh interval of ${chalkNumber(Math.round(refreshInterval))}s`);
562
+ this.refreshInterval = setInterval(async () => {
563
+ try {
564
+ await this.refresh();
565
+ } catch (err) {
566
+ debug(`Failed interval refresh with err ${err}`);
567
+ }
568
+ }, refreshInterval * 1e3);
569
+ }
540
570
  handleMessage(message) {
541
571
  const { uri, method, body } = message;
542
572
  const isDeviceUpdate = uri === "/devices/data" && method === "PUT";
@@ -4413,7 +4443,24 @@ var TydomPlatform = class {
4413
4443
  async didFinishLaunching() {
4414
4444
  assert(this.controller);
4415
4445
  this.cleanupAccessoriesIds = new Set(this.accessories.keys());
4416
- await this.controller.connect();
4446
+ const maxRetries = 10;
4447
+ const maxDelay = 5 * 60 * 1e3;
4448
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
4449
+ try {
4450
+ await this.controller.connect();
4451
+ break;
4452
+ } catch (err) {
4453
+ if (attempt === maxRetries) {
4454
+ this.log.error(
4455
+ `Failed to connect after ${maxRetries} retries, giving up: ${stringifyError(err)}`
4456
+ );
4457
+ return;
4458
+ }
4459
+ const delay = Math.min(5e3 * Math.pow(2, attempt), maxDelay);
4460
+ this.log.warn(`Connection attempt ${attempt + 1} failed, retrying in ${Math.round(delay / 1e3)}s...`);
4461
+ await new Promise((resolve) => setTimeout(resolve, delay));
4462
+ }
4463
+ }
4417
4464
  await this.controller.scan();
4418
4465
  this.cleanupAccessoriesIds.forEach((accessoryId) => {
4419
4466
  const accessory = this.accessories.get(accessoryId);