Skip to content

Commit

Permalink
Added garage set target lock state
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleBoyer committed Jul 15, 2024
1 parent 27738d1 commit 57a8409
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 16 additions & 0 deletions src/accessory/garage-door.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
BlaQTextSensorEvent,
CurrentOperationType,
GarageCoverType,
GarageLockType,
LockStateType,
OpenClosedStateType,
} from '../types.js';
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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<string, LockStateType> = {
'LOCKED': 'SECURED',
Expand Down

0 comments on commit 57a8409

Please sign in to comment.