From cbed6cec1f307340d7c6496a3cefc4ee84001001 Mon Sep 17 00:00:00 2001 From: David Kerr Date: Fri, 1 Dec 2023 12:25:04 -0500 Subject: [PATCH] Get initial device status before setting up homebride accessory handlers --- package.json | 2 +- src/contactDevice.ts | 9 +++++---- src/garageDoor.ts | 12 ++++++++---- src/infraredRemoter.ts | 6 +++--- src/leakDevice.ts | 31 ++++++++++++++++--------------- src/lightbulbDevice.ts | 9 +++++---- src/lockDevice.ts | 9 +++++---- src/motionDevice.ts | 9 +++++---- src/outletDevice.ts | 8 +++++--- src/powerDevice.ts | 9 +++++---- src/statelessSwitch.ts | 8 +++++--- src/switchDevice.ts | 9 ++++++--- src/thermoHydroDevice.ts | 8 +++++--- src/valveDevice.ts | 9 +++++---- 14 files changed, 79 insertions(+), 59 deletions(-) diff --git a/package.json b/package.json index 6035085..b101e3a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "displayName": "Homebridge YoLink", "name": "homebridge-yolink", - "version": "1.4.3-beta.3", + "version": "1.4.3-beta.4", "description": "Connect to YoLink.", "license": "Apache-2.0", "repository": { diff --git a/src/contactDevice.ts b/src/contactDevice.ts index b5dc8ea..49f5129 100644 --- a/src/contactDevice.ts +++ b/src/contactDevice.ts @@ -18,14 +18,15 @@ export async function initContactSensor(this: YoLinkPlatformAccessory): Promise< const accessory: PlatformAccessory = this.accessory; const device: YoLinkDevice = accessory.context.device; + // Call get handler to initialize data fields to current state and set + // timer to regularly update the data. + await this.refreshDataTimer(handleGetBlocking.bind(this)); + + // Once we have initial data, setup all the Homebridge handlers this.contactService = accessory.getService(platform.Service.ContactSensor) || accessory.addService(platform.Service.ContactSensor); this.contactService.setCharacteristic(platform.Characteristic.Name, device.name); this.contactService.getCharacteristic(platform.Characteristic.ContactSensorState) .onGet(handleGet.bind(this)); - - // Call get handler to initialize data fields to current state and set - // timer to regularly update the data. - await this.refreshDataTimer(handleGetBlocking.bind(this)); } /*********************************************************************** diff --git a/src/garageDoor.ts b/src/garageDoor.ts index 91189d9..66ec3a7 100644 --- a/src/garageDoor.ts +++ b/src/garageDoor.ts @@ -25,6 +25,14 @@ export async function initGarageDoor(this: YoLinkPlatformAccessory): Promise { return (false); }); - - await this.refreshDataTimer(handleGetBlocking.bind(this, doorSensor)); - // above must await because we use doorSensor in handleGet for doorController - await this.refreshDataTimer(handleGetBlocking.bind(this, doorController)); } /*********************************************************************** diff --git a/src/infraredRemoter.ts b/src/infraredRemoter.ts index 652f6e7..a26030b 100644 --- a/src/infraredRemoter.ts +++ b/src/infraredRemoter.ts @@ -27,6 +27,9 @@ export async function initInfraredRemoter(this: YoLinkPlatformAccessory): Promis const nLearned = device.data.keys.reduce((n: number, x: boolean) => (x === true) ? n + 1 : n, 0); platform.verboseLog(`Initialize infrared remoter with ${nLearned} button${(nLearned > 1) ? 's' : ''}`); + // timer to regularly update the data... really only to monitor battery level. + await this.refreshDataTimer(handleGet.bind(this, -1)); + // serviceLabel required when multiple services of same type on one accessory this.serviceLabel = accessory.getService(platform.Service.ServiceLabel) || accessory.addService(platform.Service.ServiceLabel); this.serviceLabel.setCharacteristic(platform.Characteristic.Name, device.name); @@ -62,9 +65,6 @@ export async function initInfraredRemoter(this: YoLinkPlatformAccessory): Promis .onSet(handleSet.bind(this, i)); } }); - - // timer to regularly update the data... really only to monitor battery level. - await this.refreshDataTimer(handleGet.bind(this, -1)); } /*********************************************************************** diff --git a/src/leakDevice.ts b/src/leakDevice.ts index 57c1cb7..78d786b 100644 --- a/src/leakDevice.ts +++ b/src/leakDevice.ts @@ -18,6 +18,11 @@ export async function initLeakSensor(this: YoLinkPlatformAccessory): Promise