Skip to content

Commit

Permalink
Outlets Read/Reset Only
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelahern committed Aug 19, 2023
1 parent a9072c0 commit a4b2cf0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ Example platform config in the Homebridge config.json:
"host": "10.0.0.10",
"username": "wattbox",
"password": "wattbox",
"serviceTag": "ST1234567890ABCD"
"serviceTag": "ST1234567890ABCD",
"outletsReadOnly": false,
"outletsResetOnly": false
},
{
"name": "My WattBox 2",
Expand Down
10 changes: 10 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
"type": "string",
"required": true,
"placeholder": "ST1234567890ABCD"
},
"outletsReadOnly": {
"title": "Outlets Read Only",
"type": "boolean",
"required": false
},
"outletsResetOnly": {
"title": "Outlets Reset Only",
"type": "boolean",
"required": false
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ export interface WattBoxDeviceConfig {
host: string,
username: string,
password: string,
serviceTag: string
serviceTag: string,
outletsReadOnly?: boolean,
outletsResetOnly?: boolean
}
1 change: 1 addition & 0 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class WattBoxPlatform implements DynamicPlatformPlugin {
}

accessory.context = <WattBoxPlatformAccessoryContext>{
deviceConfig: deviceConfig,
deviceInfo: deviceInfo
};
}
Expand Down
13 changes: 10 additions & 3 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CharacteristicValue, Logger, PlatformAccessory } from "homebridge";

import { WattBoxPlatform } from "./platform";
import { WattBoxDeviceApi, WattBoxDeviceInfo, WattBoxOutletAction, WattBoxOutletStatus } from "./wattbox";
import { WattBoxDeviceConfig } from "./config";

export class WattBoxPlatformAccessory {
private readonly context: WattBoxPlatformAccessoryContext;
Expand Down Expand Up @@ -56,20 +57,26 @@ export class WattBoxPlatformAccessory {
}

private async setOn(value: CharacteristicValue) {
const action = value ? WattBoxOutletAction.ON : WattBoxOutletAction.OFF;
this.log.debug(`${this.logPrefix} setOn(${WattBoxOutletStatus[action]})`)
if (this.context.deviceConfig.outletsReadOnly) {
this.log.debug(`${this.logPrefix} setOn(NOOP)`)
throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.READ_ONLY_CHARACTERISTIC);
}

const action = this.context.deviceConfig.outletsResetOnly ? WattBoxOutletAction.RESET : (value ? WattBoxOutletAction.ON : WattBoxOutletAction.OFF);
this.log.debug(`${this.logPrefix} setOn(${WattBoxOutletAction[action]})`)

try {
await this.deviceApi.setOutletAction(this.outletId, action);
this.outletStatus = value ? WattBoxOutletStatus.ON : WattBoxOutletStatus.OFF;
}
catch (error: unknown) {
this.log.error(`${this.logPrefix} setOn(${WattBoxOutletStatus[action]}) -> ${(<Error>error).message}`)
this.log.error(`${this.logPrefix} setOn(${WattBoxOutletAction[action]}) -> ${(<Error>error).message}`)
throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
}
}

export interface WattBoxPlatformAccessoryContext {
deviceConfig: WattBoxDeviceConfig
deviceInfo: WattBoxDeviceInfo;
}

0 comments on commit a4b2cf0

Please sign in to comment.