From ad4b95f6951b76c4f2df8abb96d0598f2642e266 Mon Sep 17 00:00:00 2001 From: kyleboyer Date: Mon, 15 Jul 2024 05:41:02 -0500 Subject: [PATCH] Position percentage tweaks --- package-lock.json | 4 +-- package.json | 2 +- src/accessory/garage-door.ts | 63 +++++++++++++++++++++++++----------- src/hub.ts | 12 +++---- 4 files changed, 54 insertions(+), 27 deletions(-) diff --git a/package-lock.json b/package-lock.json index f8f11e6..3981ce2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "homebridge-blaq", - "version": "0.2.12", + "version": "0.2.13", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "homebridge-blaq", - "version": "0.2.12", + "version": "0.2.13", "license": "Apache-2.0", "dependencies": { "bonjour-service": "^1.2.1", diff --git a/package.json b/package.json index 7e1605c..d967f75 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "private": false, "displayName": "Konnected BlaQ", "name": "homebridge-blaq", - "version": "0.2.12", + "version": "0.2.13", "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/garage-door.ts b/src/accessory/garage-door.ts index f630c03..919384e 100644 --- a/src/accessory/garage-door.ts +++ b/src/accessory/garage-door.ts @@ -49,7 +49,8 @@ export class BlaQGarageDoorAccessory implements BaseBlaQAccessory { private accessoryInformationService: Service; private garageDoorService: Service; private state?: OpenClosedStateType; - private position?: number; // fraction open/closed; might not always be accurate + private position?: number; // percentage open(100)/closed(0); might not always be accurate + private targetPosition?: number; // percentage open(100)/closed(0); might not always be accurate private currentOperation?: CurrentOperationType; private obstructed?: boolean; private firmwareVersion?: string; @@ -171,13 +172,17 @@ export class BlaQGarageDoorAccessory implements BaseBlaQAccessory { } getCurrentDoorState(): CharacteristicValue { - if(this.preClosing || this.currentOperation === 'CLOSING'){ + if(this.preClosing || this.currentOperation === 'CLOSING' || ( + this.position !== undefined && this.targetPosition !== undefined && this.targetPosition < this.position + )){ return this.platform.characteristic.CurrentDoorState.CLOSING; - } else if(this.currentOperation === 'OPENING'){ + } else if(this.currentOperation === 'OPENING' || ( + this.position !== undefined && this.targetPosition !== undefined && this.targetPosition > this.position + )){ return this.platform.characteristic.CurrentDoorState.OPENING; - } else if (this.state === 'OPEN') { + } else if (this.state === 'OPEN' || (this.position !== undefined && this.position > 0)) { return this.platform.characteristic.CurrentDoorState.OPEN; - } else if (this.state === 'CLOSED') { + } else if (this.state === 'CLOSED' || (this.position !== undefined && this.position <= 0)) { return this.platform.characteristic.CurrentDoorState.CLOSED; } throw new Error('Invalid door state!'); @@ -193,6 +198,18 @@ export class BlaQGarageDoorAccessory implements BaseBlaQAccessory { this.platform.characteristic.CurrentDoorState, this.getCurrentDoorState(), ); + this.garageDoorService.setCharacteristic( + this.platform.characteristic.TargetDoorState, + this.getTargetDoorState(), + ); + this.garageDoorService.setCharacteristic( + this.platform.characteristic.CurrentPosition, + this.getCurrentPosition(), + ); + this.garageDoorService.setCharacteristic( + this.platform.characteristic.TargetPosition, + this.getTargetDoorPosition(), + ); } private setPreClosing(preClosing: boolean){ @@ -209,24 +226,21 @@ export class BlaQGarageDoorAccessory implements BaseBlaQAccessory { if(this.position){ return this.position; } else if (this.state === 'OPEN') { - return 1; + return 100; } else if (this.state === 'CLOSED') { return 0; } else { - return 0.5; // unknown state + return 50; // unknown state } } private setCurrentPosition(position: number) { this.position = position; - this.garageDoorService.setCharacteristic( - this.platform.characteristic.CurrentPosition, - this.getCurrentPosition(), - ); + this.updateCurrentDoorState(); } getTargetDoorState(): CharacteristicValue { - if(this.currentOperation === 'OPENING'){ + if(this.currentOperation === 'OPENING' || (this.targetPosition && this.targetPosition > 0)){ return this.platform.characteristic.TargetDoorState.OPEN; } else if(this.currentOperation === 'CLOSING' || this.preClosing){ return this.platform.characteristic.TargetDoorState.CLOSED; @@ -244,24 +258,29 @@ export class BlaQGarageDoorAccessory implements BaseBlaQAccessory { let apiTarget: string; if (target === this.platform.characteristic.TargetDoorState.CLOSED) { apiTarget = 'close'; + this.targetPosition = 0; } else if (target === this.platform.characteristic.TargetDoorState.OPEN) { + this.targetPosition = 100; apiTarget = 'open'; } else { throw new Error('Invalid target door state!'); } + this.updateCurrentDoorState(); await fetch(`${this.apiBaseURL}/cover/${this.coverType}/${apiTarget}`, {method: 'POST'}); } getTargetDoorPosition(): CharacteristicValue { - if(this.currentOperation === 'OPENING'){ - return 1; + if(this.targetPosition){ + return this.targetPosition; + } else if(this.currentOperation === 'OPENING'){ + return 100; } else if(this.currentOperation === 'CLOSING' || this.preClosing){ return 0; } else if(this.currentOperation === 'IDLE'){ if(this.state === 'CLOSED'){ return 0; }else if(this.state === 'OPEN'){ - return 1; + return 100; } } throw new Error('Invalid target door position!'); @@ -271,7 +290,15 @@ export class BlaQGarageDoorAccessory implements BaseBlaQAccessory { if(isNaN(+target)){ throw new Error('Invalid target door position!'); } - await fetch(`${this.apiBaseURL}/cover/${this.coverType}/set?position=${Math.round(+target * 100)}`, {method: 'POST'}); + const roundedTarget = Math.round(+target); + this.targetPosition = roundedTarget; + if(this.position){ + this.setCurrentOperation(roundedTarget < this.position ? 'CLOSING' : 'OPENING'); + } + this.updateCurrentDoorState(); + if(this.position !== roundedTarget){ + await fetch(`${this.apiBaseURL}/cover/${this.coverType}/set?position=${roundedTarget}`, {method: 'POST'}); + } } getObstructed(): CharacteristicValue { @@ -301,7 +328,7 @@ export class BlaQGarageDoorAccessory implements BaseBlaQAccessory { this.coverType = stateInfo.id.split(COVER_PREFIX).pop() as GarageCoverType; this.setCurrentDoorState(doorEvent.state); this.setCurrentOperation(doorEvent.current_operation); - this.setCurrentPosition(doorEvent.position); + this.setCurrentPosition(Math.round(doorEvent.position * 100)); } else if (stateInfo.id.startsWith(BINARY_SENSOR_PREFIX)) { const binarySensorEvent = stateInfo as BlaQBinarySensorEvent; const short_id = binarySensorEvent.id.split(BINARY_SENSOR_PREFIX).pop(); @@ -350,7 +377,7 @@ export class BlaQGarageDoorAccessory implements BaseBlaQAccessory { const positionMatch = lowercaseLogStr.match(/position[:=] ?([\d.]+%?)/g)?.shift(); if(positionMatch){ const positionNum = +positionMatch.replaceAll(/[^\d.]+/g, ''); - const multiplier = (positionMatch.includes('%') || positionNum > 1) ? 1/100 : 1; + const multiplier = positionMatch.includes('%') ? 1 : 100; this.setCurrentPosition(positionNum * multiplier); } if (lowercaseLogStr.includes('warning for ')){ diff --git a/src/hub.ts b/src/hub.ts index bde0de6..ae05fb8 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -111,7 +111,7 @@ export class BlaQHub { } private initGarageLightAccessory({ platform, accessory, model, serialNumber}: InitAccessoryParams){ - if(this.pluginConfig.enableLight) { + if(this.pluginConfig.enableLight ?? true) { this.accessories.push(new BlaQGarageLightAccessory({ platform, accessory, model, serialNumber, apiBaseURL: this.getAPIBaseURL(), })); @@ -119,7 +119,7 @@ export class BlaQHub { } private initGarageLockAccessory({ platform, accessory, model, serialNumber}: InitAccessoryParams){ - if(this.pluginConfig.enableLockRemotes){ + if(this.pluginConfig.enableLockRemotes ?? true){ this.accessories.push(new BlaQGarageLockAccessory({ platform, accessory, model, serialNumber, apiBaseURL: this.getAPIBaseURL(), })); @@ -127,7 +127,7 @@ export class BlaQHub { } private initGarageMotionSensorAccessory({ platform, accessory, model, serialNumber}: InitAccessoryParams){ - if(this.pluginConfig.enableMotionSensor){ + if(this.pluginConfig.enableMotionSensor ?? true){ this.accessories.push(new BlaQGarageMotionSensorAccessory({ platform, accessory, model, serialNumber, apiBaseURL: this.getAPIBaseURL(), })); @@ -135,7 +135,7 @@ export class BlaQHub { } private initGaragePreCloseWarningAccessory({ platform, accessory, model, serialNumber}: InitAccessoryParams){ - if(this.pluginConfig.enablePreCloseWarning){ + if(this.pluginConfig.enablePreCloseWarning ?? true){ this.accessories.push(new BlaQGaragePreCloseWarningAccessory({ platform, accessory, model, serialNumber, apiBaseURL: this.getAPIBaseURL(), })); @@ -143,7 +143,7 @@ export class BlaQHub { } private initGarageLearnModeAccessory({ platform, accessory, model, serialNumber}: InitAccessoryParams){ - if(this.pluginConfig.enableLearnMode){ + if(this.pluginConfig.enableLearnMode ?? true){ this.accessories.push(new BlaQGarageLearnModeAccessory({ platform, accessory, model, serialNumber, apiBaseURL: this.getAPIBaseURL(), })); @@ -151,7 +151,7 @@ export class BlaQHub { } private initGarageObstructionSensorAccessory({ platform, accessory, model, serialNumber}: InitAccessoryParams){ - if(this.pluginConfig.enableSeparateObstructionSensor){ + if(this.pluginConfig.enableSeparateObstructionSensor ?? true){ this.accessories.push(new BlaQGarageObstructionSensorAccessory({ platform, accessory, model, serialNumber, apiBaseURL: this.getAPIBaseURL(), }));