Skip to content

Commit

Permalink
Allow changing to window covering type for positional controls
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleBoyer committed Jul 27, 2024
1 parent a41e356 commit 70e860c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
10 changes: 10 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@
}
}
},
"garageDoorType": {
"title": "Enable publishing a separate \"occupancy\" obstruction sensor for the garage",
"type": "string",
"default": "garage",
"oneOf": [
{ "title": "Garage Door", "enum": ["garage"] },
{ "title": "Window Covering (allows positional control)", "enum": ["cover"] }
],
"required": true
},
"enableSeparateObstructionSensor": {
"title": "Enable publishing a separate \"occupancy\" obstruction sensor for the garage",
"type": "boolean",
Expand Down
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.21",
"version": "0.2.22",
"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
57 changes: 54 additions & 3 deletions src/accessory/garage-door.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const BINARY_SENSOR_PREFIX = 'binary_sensor-';
const COVER_PREFIX = 'cover-';
const LOCK_PREFIX = 'lock-';

type BlaQGarageDoorAccessoryConstructorParams = BaseBlaQAccessoryConstructorParams & {
type: 'garage' | 'cover';
};

/**
* Platform Accessory
* An instance of this class is created for each accessory your platform registers
Expand All @@ -35,9 +39,11 @@ export class BlaQGarageDoorAccessory extends BaseBlaQAccessory {
private coverType?: GarageCoverType = 'garage_door';
private preClosing?: boolean;

constructor(args: BaseBlaQAccessoryConstructorParams) {
constructor(args: BlaQGarageDoorAccessoryConstructorParams) {
super(args);
this.garageDoorService = this.getOrAddService(this.platform.service.GarageDoorOpener);
this.garageDoorService = this.getOrAddService(
args.type === 'garage' ? this.platform.service.GarageDoorOpener : this.platform.service.WindowCovering,
);

// Set the service name. This is what is displayed as the name on the Home
// app. We use what we stored in `accessory.context` in `discoverDevices`.
Expand All @@ -46,6 +52,13 @@ export class BlaQGarageDoorAccessory extends BaseBlaQAccessory {
this.garageDoorService.getCharacteristic(this.platform.characteristic.CurrentDoorState)
.onGet(this.getCurrentDoorState.bind(this));

this.garageDoorService.getCharacteristic(this.platform.characteristic.PositionState)
.onGet(this.getCurrentPositionState.bind(this));

this.garageDoorService.getCharacteristic(this.platform.characteristic.HoldPosition)
.onGet(this.getHoldPositionState.bind(this))
.onSet(this.setHoldPositionState.bind(this));

this.garageDoorService.getCharacteristic(this.platform.characteristic.CurrentPosition)
.onGet(this.getCurrentPosition.bind(this));

Expand Down Expand Up @@ -103,7 +116,12 @@ export class BlaQGarageDoorAccessory extends BaseBlaQAccessory {
);
}

getCurrentDoorState(): CharacteristicValue {
getCurrentDoorState():
typeof this.platform.characteristic.CurrentDoorState.CLOSING |
typeof this.platform.characteristic.CurrentDoorState.OPENING |
typeof this.platform.characteristic.CurrentDoorState.CLOSED |
typeof this.platform.characteristic.CurrentDoorState.OPEN |
typeof this.platform.characteristic.CurrentDoorState.STOPPED {
if(this.preClosing || this.currentOperation === 'CLOSING' || (
this.position !== undefined && this.targetPosition !== undefined && this.targetPosition < this.position
)){
Expand All @@ -125,11 +143,37 @@ export class BlaQGarageDoorAccessory extends BaseBlaQAccessory {
this.updateCurrentDoorState();
}

private getHoldPositionState(): CharacteristicValue {
return [
this.platform.characteristic.CurrentDoorState.STOPPED,
this.platform.characteristic.CurrentDoorState.CLOSED,
this.platform.characteristic.CurrentDoorState.OPEN,
].includes(this.getCurrentDoorState());
}

private getCurrentPositionState(): CharacteristicValue {
return {
[this.platform.characteristic.CurrentDoorState.STOPPED]: this.platform.characteristic.PositionState.STOPPED,
[this.platform.characteristic.CurrentDoorState.CLOSED]: this.platform.characteristic.PositionState.STOPPED,
[this.platform.characteristic.CurrentDoorState.OPEN]: this.platform.characteristic.PositionState.STOPPED,
[this.platform.characteristic.CurrentDoorState.CLOSING]: this.platform.characteristic.PositionState.DECREASING,
[this.platform.characteristic.CurrentDoorState.OPENING]: this.platform.characteristic.PositionState.INCREASING,
}[this.getCurrentDoorState()];
}

private updateCurrentDoorState(){
this.garageDoorService.setCharacteristic(
this.platform.characteristic.CurrentDoorState,
this.getCurrentDoorState(),
);
this.garageDoorService.updateCharacteristic(
this.platform.characteristic.PositionState,
this.getCurrentPositionState(),
);
this.garageDoorService.updateCharacteristic(
this.platform.characteristic.HoldPosition,
this.getHoldPositionState(),
);
this.garageDoorService.updateCharacteristic(
this.platform.characteristic.TargetDoorState,
this.getTargetDoorState(),
Expand Down Expand Up @@ -187,6 +231,13 @@ export class BlaQGarageDoorAccessory extends BaseBlaQAccessory {
// throw new Error(`Invalid target door state: ${this.currentOperation}`);
}

private async setHoldPositionState(target: CharacteristicValue){
const shouldHold = target;
if(shouldHold){
await fetch(`${this.apiBaseURL}/cover/${this.coverType}/stop`, {method: 'POST'});
}
}

private async setTargetDoorState(target: CharacteristicValue){
let apiTarget: string;
if (target === this.platform.characteristic.TargetDoorState.CLOSED) {
Expand Down
2 changes: 1 addition & 1 deletion src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class BlaQHub {

private initGarageDoorAccessory({ platform, accessory, friendlyName, serialNumber}: InitAccessoryParams){
this.accessories.push(new BlaQGarageDoorAccessory({
platform, accessory, friendlyName, serialNumber, apiBaseURL: this.getAPIBaseURL(),
platform, accessory, friendlyName, serialNumber, apiBaseURL: this.getAPIBaseURL(), type: this.pluginConfig.garageDoorType,
}));
}

Expand Down

0 comments on commit 70e860c

Please sign in to comment.