From bb32d23ac755a536323bf42b7e7e440754dd6db3 Mon Sep 17 00:00:00 2001 From: kyleboyer Date: Sat, 3 Aug 2024 13:15:13 -0500 Subject: [PATCH] Queue events while not synced --- package-lock.json | 4 +- package.json | 2 +- src/accessory/base.ts | 43 +++++++++++++++++++++- src/accessory/garage-door.ts | 7 ++++ src/accessory/garage-learn-mode.ts | 7 ++++ src/accessory/garage-light.ts | 7 ++++ src/accessory/garage-lock.ts | 7 ++++ src/accessory/garage-motion-sensor.ts | 7 ++++ src/accessory/garage-obstruction-sensor.ts | 7 ++++ src/accessory/garage-pre-close-warning.ts | 7 ++++ 10 files changed, 93 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5d5b148..8e89c64 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "homebridge-blaq", - "version": "0.2.27", + "version": "0.2.28", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "homebridge-blaq", - "version": "0.2.27", + "version": "0.2.28", "funding": [ { "type": "github", diff --git a/package.json b/package.json index 9196f78..0d5041c 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "private": false, "displayName": "Konnected BlaQ", "name": "homebridge-blaq", - "version": "0.2.27", + "version": "0.2.28", "description": "Control and view your garage door(s) remotely with real-time updates using Konnected's BlaQ hardware", "license": "Apache-2.0", "type": "module", diff --git a/src/accessory/base.ts b/src/accessory/base.ts index 99e07e8..906f1d8 100644 --- a/src/accessory/base.ts +++ b/src/accessory/base.ts @@ -32,6 +32,12 @@ export const correctAPIBaseURL = (inputURL: string) => { export class BaseBlaQAccessory implements BaseBlaQAccessoryInterface { protected apiBaseURL: string; protected firmwareVersion?: string; + protected synced?: boolean; + protected queuedEvents: { + type: 'state' | 'log' | 'ping'; + event: StateUpdateMessageEvent | LogMessageEvent | PingMessageEvent; + }[] = []; + protected readonly accessory: PlatformAccessory; protected readonly accessoryInformationService: Service; protected readonly logger: Logger; @@ -83,10 +89,41 @@ export class BaseBlaQAccessory implements BaseBlaQAccessoryInterface { } } - handleStateEvent(stateEvent: StateUpdateMessageEvent){ + processQueuedEvents() { + while(this.queuedEvents.length){ + const event = this.queuedEvents.shift()!; + const funcToCall = { + 'ping': (this as BaseBlaQAccessoryInterface).handlePingEvent?.bind(this), + 'log': (this as BaseBlaQAccessoryInterface).handleLogEvent?.bind(this), + 'state': (this as BaseBlaQAccessoryInterface).handleStateEvent?.bind(this), + }[event.type]; + if(funcToCall){ + funcToCall(event.event); + } + } + } + + handlePingEvent(logEvent: LogMessageEvent){ + if(!this.synced){ + this.queuedEvents.push({type: 'ping', event: logEvent}); + } + } + + handleLogEvent(logEvent: LogMessageEvent){ + if(!this.synced){ + this.queuedEvents.push({type: 'log', event: logEvent}); + } + } + + handleStateEvent(stateEvent: StateUpdateMessageEvent): void { try { const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord; - if (['text_sensor-esphome_version', 'text_sensor-firmware_version'].includes(stateInfo.id)) { + if (['binary_sensor-synced'].includes(stateInfo.id)) { + this.synced = stateInfo.value as boolean | undefined; + if(this.synced){ + this.processQueuedEvents(); + } + }else if (['text_sensor-esphome_version', 'text_sensor-firmware_version'].includes(stateInfo.id)) { const b = stateInfo as BlaQTextSensorEvent; if (b.value && b.value === b.state) { this.setFirmwareVersion(b.value); @@ -94,6 +131,8 @@ export class BaseBlaQAccessory implements BaseBlaQAccessoryInterface { this.logger.error('Mismatched firmware versions in value/state:', b.value, b.state); this.firmwareVersion = undefined; } + } else if(!this.synced){ + this.queuedEvents.push({type: 'state', event: stateEvent}); } } catch(e) { this.logger.error('Cannot deserialize message:', stateEvent); diff --git a/src/accessory/garage-door.ts b/src/accessory/garage-door.ts index 9f65031..977c53a 100644 --- a/src/accessory/garage-door.ts +++ b/src/accessory/garage-door.ts @@ -342,6 +342,9 @@ export class BlaQGarageDoorAccessory extends BaseBlaQAccessory { handleStateEvent(stateEvent: StateUpdateMessageEvent){ super.handleStateEvent(stateEvent); + if(!this.synced){ + return; + } try { const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord; if (['cover-garage_door', 'cover-door'].includes(stateInfo.id)) { @@ -379,6 +382,10 @@ export class BlaQGarageDoorAccessory extends BaseBlaQAccessory { } handleLogEvent(logEvent: LogMessageEvent){ + super.handleLogEvent(logEvent); + if(!this.synced){ + return; + } try { const logStr = logEvent.data; const lowercaseLogStr = logStr.toLowerCase(); diff --git a/src/accessory/garage-learn-mode.ts b/src/accessory/garage-learn-mode.ts index 3710373..e5c99ca 100644 --- a/src/accessory/garage-learn-mode.ts +++ b/src/accessory/garage-learn-mode.ts @@ -53,6 +53,9 @@ export class BlaQGarageLearnModeAccessory extends BaseBlaQAccessory { handleStateEvent(stateEvent: StateUpdateMessageEvent){ super.handleStateEvent(stateEvent); + if(!this.synced){ + return; + } try { const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord; if (['switch-learn'].includes(stateInfo.id)) { @@ -68,6 +71,10 @@ export class BlaQGarageLearnModeAccessory extends BaseBlaQAccessory { } handleLogEvent(logEvent: LogMessageEvent){ + super.handleLogEvent(logEvent); + if(!this.synced){ + return; + } try { const logStr = logEvent.data; const lowercaseLogStr = logStr.toLowerCase(); diff --git a/src/accessory/garage-light.ts b/src/accessory/garage-light.ts index ebf665c..2787019 100644 --- a/src/accessory/garage-light.ts +++ b/src/accessory/garage-light.ts @@ -58,6 +58,9 @@ export class BlaQGarageLightAccessory extends BaseBlaQAccessory { handleStateEvent(stateEvent: StateUpdateMessageEvent){ super.handleStateEvent(stateEvent); + if(!this.synced){ + return; + } try { const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord; if (['light-garage_light', 'light-light'].includes(stateInfo.id)) { @@ -74,6 +77,10 @@ export class BlaQGarageLightAccessory extends BaseBlaQAccessory { } handleLogEvent(logEvent: LogMessageEvent){ + super.handleLogEvent(logEvent); + if(!this.synced){ + return; + } try { const logStr = logEvent.data; const lowercaseLogStr = logStr.toLowerCase(); diff --git a/src/accessory/garage-lock.ts b/src/accessory/garage-lock.ts index 5397d5c..6954dfa 100644 --- a/src/accessory/garage-lock.ts +++ b/src/accessory/garage-lock.ts @@ -71,6 +71,9 @@ export class BlaQGarageLockAccessory extends BaseBlaQAccessory { handleStateEvent(stateEvent: StateUpdateMessageEvent){ super.handleStateEvent(stateEvent); + if(!this.synced){ + return; + } try { const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord; if (['lock-lock', 'lock-lock_remotes'].includes(stateInfo.id)) { @@ -87,6 +90,10 @@ export class BlaQGarageLockAccessory extends BaseBlaQAccessory { } handleLogEvent(logEvent: LogMessageEvent){ + super.handleLogEvent(logEvent); + if(!this.synced){ + return; + } try { const logStr = logEvent.data; const lowercaseLogStr = logStr.toLowerCase(); diff --git a/src/accessory/garage-motion-sensor.ts b/src/accessory/garage-motion-sensor.ts index fafbfd9..24f132a 100644 --- a/src/accessory/garage-motion-sensor.ts +++ b/src/accessory/garage-motion-sensor.ts @@ -46,6 +46,9 @@ export class BlaQGarageMotionSensorAccessory extends BaseBlaQAccessory { handleStateEvent(stateEvent: StateUpdateMessageEvent){ super.handleStateEvent(stateEvent); + if(!this.synced){ + return; + } try { const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord; if (['binary_sensor-motion'].includes(stateInfo.id)) { @@ -61,6 +64,10 @@ export class BlaQGarageMotionSensorAccessory extends BaseBlaQAccessory { } handleLogEvent(logEvent: LogMessageEvent){ + super.handleLogEvent(logEvent); + if(!this.synced){ + return; + } try { const logStr = logEvent.data; const lowercaseLogStr = logStr.toLowerCase(); diff --git a/src/accessory/garage-obstruction-sensor.ts b/src/accessory/garage-obstruction-sensor.ts index 2bbbb86..d040c9a 100644 --- a/src/accessory/garage-obstruction-sensor.ts +++ b/src/accessory/garage-obstruction-sensor.ts @@ -48,6 +48,9 @@ export class BlaQGarageObstructionSensorAccessory extends BaseBlaQAccessory { handleStateEvent(stateEvent: StateUpdateMessageEvent){ super.handleStateEvent(stateEvent); + if(!this.synced){ + return; + } try { const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord; if (['binary_sensor-obstruction'].includes(stateInfo.id)) { @@ -63,6 +66,10 @@ export class BlaQGarageObstructionSensorAccessory extends BaseBlaQAccessory { } handleLogEvent(logEvent: LogMessageEvent){ + super.handleLogEvent(logEvent); + if(!this.synced){ + return; + } try { const logStr = logEvent.data; const lowercaseLogStr = logStr.toLowerCase(); diff --git a/src/accessory/garage-pre-close-warning.ts b/src/accessory/garage-pre-close-warning.ts index bc8b60d..d505d95 100644 --- a/src/accessory/garage-pre-close-warning.ts +++ b/src/accessory/garage-pre-close-warning.ts @@ -52,6 +52,9 @@ export class BlaQGaragePreCloseWarningAccessory extends BaseBlaQAccessory { handleStateEvent(stateEvent: StateUpdateMessageEvent){ super.handleStateEvent(stateEvent); + if(!this.synced){ + return; + } try { const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord; if (['button-pre-close_warning'].includes(stateInfo.id)) { @@ -67,6 +70,10 @@ export class BlaQGaragePreCloseWarningAccessory extends BaseBlaQAccessory { } handleLogEvent(logEvent: LogMessageEvent){ + super.handleLogEvent(logEvent); + if(!this.synced){ + return; + } try { const logStr = logEvent.data; const lowercaseLogStr = logStr.toLowerCase();