Skip to content

Commit

Permalink
Add a battery service to the lock.
Browse files Browse the repository at this point in the history
Currently sets the low battery alert to 40% because I have noticed they
tend to start having issues below this threshold.
  • Loading branch information
jgstroud committed Nov 30, 2023
1 parent ab03e89 commit 9759e75
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { KwiksetHaloPlatform } from './platform';
*/
export class KwiksetHaloAccessory {
public service: Service;
public batteryservice: Service;
private batterylevel;
lowBatteryLevel = 40;

/**
* These are just used to create a working example
Expand Down Expand Up @@ -58,6 +61,17 @@ export class KwiksetHaloAccessory {
.onGet(this.getShouldLock.bind(this))
.onSet(this.setShouldLock.bind(this));

// get the Battery service if it exists, otherwise create a new Battery service
// you can create multiple services for each accessory
this.batteryservice =
this.accessory.getService(this.platform.Service.Battery) ||
this.accessory.addService(this.platform.Service.Battery);

// create handlers for required characteristics
this.batteryservice
.getCharacteristic(this.platform.Characteristic.StatusLowBattery)
.onGet(this.handleStatusLowBatteryGet.bind(this));

/**
* Creating multiple services of the same type.
*
Expand Down Expand Up @@ -96,6 +110,15 @@ export class KwiksetHaloAccessory {
lockTargetStatus,
);
}

this.batterylevel = lock.batterypercentage;
this.batteryservice.updateCharacteristic(
this.platform.Characteristic.BatteryLevel,
this.batterylevel,
);
this.accessory
.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.SerialNumber, lock.serialnumber);
});
}

Expand Down Expand Up @@ -205,6 +228,11 @@ export class KwiksetHaloAccessory {
lockStatus,
);
}
this.batterylevel = lock.batterypercentage;
this.batteryservice.updateCharacteristic(
this.platform.Characteristic.BatteryLevel,
this.batterylevel,
);
this.lockStates.locked = lockStatus;
});

Expand Down Expand Up @@ -233,4 +261,11 @@ export class KwiksetHaloAccessory {

return isLocking;
}

async handleStatusLowBatteryGet(): Promise<CharacteristicValue> {
if (this.batterylevel <= this.lowBatteryLevel) {
return this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
}
return this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
}
}

0 comments on commit 9759e75

Please sign in to comment.