Skip to content

Commit

Permalink
Fixes invalid value for characteristic properties on the security sys…
Browse files Browse the repository at this point in the history
…tem service.
  • Loading branch information
mkormendy committed Jul 21, 2021
1 parent 16b3317 commit 008cd1a
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/platformAccessory.ts
Expand Up @@ -10,11 +10,14 @@ export class KonnectedPlatformAccessory {
private service: Service;
private accessoryServiceType: string;
private temperatureSensorService;
private validSecuritySystemStates: number[];

constructor(private readonly platform: KonnectedHomebridgePlatform, private readonly accessory: PlatformAccessory) {
// translate the accessory type to the service type
this.accessoryServiceType = TYPES_TO_ACCESSORIES[this.accessory.context.device.type][0];

this.validSecuritySystemStates = [];

// set accessory information
this.accessory
.getService(this.platform.Service.AccessoryInformation)!
Expand All @@ -41,14 +44,14 @@ export class KonnectedPlatformAccessory {
case 'SecuritySystem':
{
// default/required security system modes for HomeKit app
const validValues = [
this.validSecuritySystemStates = [
this.platform.Characteristic.SecuritySystemTargetState.DISARM,
this.platform.Characteristic.SecuritySystemTargetState.AWAY_ARM,
// this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM,
// this.platform.Characteristic.SecuritySystemTargetState.NIGHT_ARM,
];
let stayHomeModeUsed = false,
nightModeUsed = false;
let stayHomeModeUsed = false;
let nightModeUsed = false;
// find which security system modes are required/set by sensors and switches
this.platform.accessories.forEach((existingAccessory) => {
if (existingAccessory.context.device.triggerableModes?.includes('0')) { // stay/home
Expand All @@ -65,22 +68,21 @@ export class KonnectedPlatformAccessory {
}
});
if (stayHomeModeUsed) {
validValues.push(this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM);
this.validSecuritySystemStates.push(this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM);
}
if (nightModeUsed) {
validValues.push(this.platform.Characteristic.SecuritySystemTargetState.NIGHT_ARM);
this.validSecuritySystemStates.push(this.platform.Characteristic.SecuritySystemTargetState.NIGHT_ARM);
}

const validValues = this.validSecuritySystemStates;

this.service
.getCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState)
.onGet(this.getSecuritySystemCurrentState.bind(this));
this.service
.getCharacteristic(this.platform.Characteristic.SecuritySystemTargetState)
.setProps({ validValues })
/**
* @removed 'Security System Target State': characteristic was supplied illegal value: number 4 exceeded maximum of 3. See https://git.io/JtMGR for more info.
* .onGet(this.getSecuritySystemTargetState.bind(this))
*/
.onGet(this.getSecuritySystemTargetState.bind(this))
.onSet(this.setSecuritySystemTargetState.bind(this));
}
break;
Expand Down Expand Up @@ -206,8 +208,14 @@ export class KonnectedPlatformAccessory {
// get and set the security system states
getSecuritySystemState(characteristic: string) {
let value = 1; // default to Away (in case of catastrophic reset when not home, this preserves the home's security)
// set defaults
if (typeof this.accessory.context.device.state === 'undefined') {
// set default as 1, if there's no previous accessory state in Homebridge cache
// or if the secuity system state exists but isn't a valid value for the states available
// e.g., if the system was set to Home (0), but Home is not a state that is available
// because the user didn't want or choose it for any of the sensors to trigger in
if (
typeof this.accessory.context.device.state === 'undefined' ||
!this.validSecuritySystemStates.includes(this.accessory.context.device.state)
) {
this.accessory.context.device.state = value;
this.platform.log.debug(
`Assigning default state '${value}' to [${this.accessory.context.device.displayName}] (${this.accessory.context.device.serialNumber}) '${this.accessoryServiceType}' characteristic value. Awaiting zone's first state change...`
Expand Down

0 comments on commit 008cd1a

Please sign in to comment.