From 57a8409b827b89f8b747ce75a062bfba26cc8f0f Mon Sep 17 00:00:00 2001 From: kyleboyer Date: Mon, 15 Jul 2024 00:17:09 -0500 Subject: [PATCH] Added garage set target lock state --- package-lock.json | 4 ++-- package.json | 2 +- src/accessory/garage-door.ts | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index bb98dbc..d959eb5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "homebridge-blaq", - "version": "0.2.10", + "version": "0.2.11", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "homebridge-blaq", - "version": "0.2.10", + "version": "0.2.11", "license": "Apache-2.0", "dependencies": { "bonjour-service": "^1.2.1", diff --git a/package.json b/package.json index f8c1871..79a3b26 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "private": false, "displayName": "Konnected BlaQ", "name": "homebridge-blaq", - "version": "0.2.10", + "version": "0.2.11", "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 b50074b..64cb60c 100644 --- a/src/accessory/garage-door.ts +++ b/src/accessory/garage-door.ts @@ -9,6 +9,7 @@ import { BlaQTextSensorEvent, CurrentOperationType, GarageCoverType, + GarageLockType, LockStateType, OpenClosedStateType, } from '../types.js'; @@ -17,6 +18,7 @@ import { BaseBlaQAccessory } from './base.js'; const BINARY_SENSOR_PREFIX = 'binary_sensor-'; const COVER_PREFIX = 'cover-'; +const LOCK_PREFIX = 'lock-'; const correctAPIBaseURL = (inputURL: string) => { let correctedAPIBaseURL = inputURL; @@ -52,6 +54,7 @@ export class BlaQGarageDoorAccessory implements BaseBlaQAccessory { private obstructed?: boolean; private firmwareVersion?: string; private lockState: LockStateType = 'UNKNOWN'; + private lockType?: GarageLockType = 'lock'; private coverType?: GarageCoverType = 'garage_door'; private preClosing?: boolean; private apiBaseURL: string; @@ -110,6 +113,9 @@ export class BlaQGarageDoorAccessory implements BaseBlaQAccessory { this.garageDoorService.getCharacteristic(this.platform.characteristic.LockCurrentState) .onGet(this.getLockState.bind(this)); + this.garageDoorService.getCharacteristic(this.platform.characteristic.LockTargetState) + .onSet(this.updateLockState.bind(this)); + // Publish firmware version; this may not be initialized yet, so we set a getter. // Note that this is against the AccessoryInformation service, not the GDO service. this.accessoryInformationService @@ -130,6 +136,15 @@ export class BlaQGarageDoorAccessory implements BaseBlaQAccessory { ); } + private async updateLockState(lockState: CharacteristicValue){ + const lockDesired = lockState === this.platform.characteristic.LockTargetState.SECURED; + const apiTarget: string = lockDesired ? 'lock' : 'unlock'; + const currentlyLocked = this.getLockState() === this.platform.characteristic.LockCurrentState.SECURED; + if(lockDesired !== currentlyLocked){ + await fetch(`${this.apiBaseURL}/lock/${this.lockType}/${apiTarget}`, {method: 'POST'}); + } + } + getLockState(): CharacteristicValue { const lockStateMap = { 'UNSECURED': this.platform.characteristic.LockCurrentState.UNSECURED, @@ -295,6 +310,7 @@ export class BlaQGarageDoorAccessory implements BaseBlaQAccessory { this.firmwareVersion = undefined; } } else if (['lock-lock', 'lock-lock_remotes'].includes(stateInfo.id)) { + this.lockType = stateInfo.id.split(LOCK_PREFIX).pop() as GarageLockType; const b = stateInfo as BlaQLockEvent; const lockStateMap: Record = { 'LOCKED': 'SECURED',