Skip to content

Commit

Permalink
Handle shades with switchLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
iklein99 committed Sep 23, 2023
1 parent c96f6df commit 226c992
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 77 deletions.
4 changes: 4 additions & 0 deletions src/multiServiceAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ export class MultiServiceAccessory {
capabilities: ['windowShade', 'windowShadeLevel'],
service: WindowCoveringService,
},
{
capabilities: ['windowShade', 'switchLevel'],
service: WindowCoveringService,
},
];

protected accessory: PlatformAccessory;
Expand Down
99 changes: 22 additions & 77 deletions src/services/windowCoveringService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export class WindowCoveringService extends BaseService {

private currentPositionState = this.states.stopped;

private useWindowShadeLevel = false;

constructor(platform: IKHomeBridgeHomebridgePlatform, accessory: PlatformAccessory, componentId: string, capabilities: string[],
multiServiceAccessory: MultiServiceAccessory,
name: string, deviceStatus) {
Expand Down Expand Up @@ -43,6 +45,10 @@ export class WindowCoveringService extends BaseService {
platform.Characteristic.PositionState);
}

if (this.capabilities.includes('windowShadeLevel')) {
this.useWindowShadeLevel = true;
}

}

/**
Expand All @@ -60,80 +66,25 @@ export class WindowCoveringService extends BaseService {
throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}

this.multiServiceAccessory.sendCommand('windowShadeLevel', 'setShadeLevel', [value])
let capability = 'switchLevel';
let command = 'setLevel';

if (this.useWindowShadeLevel) {
capability = 'windowShadeLevel';
command = 'setShadeLevel';
}

this.multiServiceAccessory.sendCommand(capability, command, [value])
.then(() => {
this.log.debug('onSet(' + value + ') SUCCESSFUL for ' + this.name);
this.multiServiceAccessory.forceNextStatusRefresh();
// this.pollTry = 0;
// this.log.debug('Polling shade status...');

// Poll every 2 seconds
// this.timesAtSameLevel = 0;
// this.shadeState = this.platform.Characteristic.PositionState.STOPPED;
// this.timer = setInterval(this.pollShadeLevel.bind(this), 2000);
})
.catch(reason => {
this.log.error('onSet(' + value + ') FAILED for ' + this.name + ': reason ' + reason);
throw (new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE));
});
}

// private pollTry = 0;
// private lastPolledShadeLevel = 0;
// private shadeState = this.states.stopped;
// private timesAtSameLevel = 0;
// /**
// *
// * @param t - the 'this' variable
// * Polls the device as it is moving into position after a command to change it.
// */
// private async pollShadeLevel() {
// this.log.debug(`Shade level poll event #${this.pollTry + 1} for ${this.name}`);

// this.getCurrentPosition().then(value => {

// // Update homebridge with the current position
// const currentPostion = +value;
// this.log.debug(`${this.name} shade level is ${currentPostion}`);

// // If we are within 5 pct of target, consider us finished.
// if (Math.abs(currentPostion - this.targetPosition) <= 5) {
// this.log.debug(`${this.name} close to target postion of ${this.targetPosition}. Close enough!`);
// this.service.updateCharacteristic(this.platform.Characteristic.CurrentPosition, this.targetPosition);
// clearInterval(this.timer);
// this.shadeState = this.states.stopped;
// return;
// }

// // Let Homebridge know where we are, then see if we are stuck her, lowering or raising.
// this.service.updateCharacteristic(this.platform.Characteristic.CurrentPosition, currentPostion);
// if (currentPostion === this.lastPolledShadeLevel) {
// if (++this.timesAtSameLevel > 5) {
// // After 10 seconds at same level, we're done.
// clearInterval(this.timer);
// this.shadeState = this.states.stopped;
// this.log.debug(`${this.name} appears to have stopped. Will stop polling`);
// }
// } else {
// this.timesAtSameLevel = 0;
// if (currentPostion < this.lastPolledShadeLevel) {
// this.log.debug(`${this.name} appears to be lowering (decreasing)`);
// this.shadeState = this.states.decreasing;
// } else {
// this.log.debug(`${this.name} appears to be raising (increasing)`);
// this.shadeState = this.states.increasing;
// }
// this.lastPolledShadeLevel = currentPostion;

// // Stop polling after 2 minutes
// if (++this.pollTry > 60) {
// this.log.debug(`Polled ${this.name} for 2 minutes. Ending`);
// clearInterval(this.timer);
// }
// }
// });
// }

async getTargetPosition(): Promise<CharacteristicValue> {
return new Promise(resolve => {
resolve(this.targetPosition);
Expand All @@ -160,17 +111,6 @@ export class WindowCoveringService extends BaseService {
this.log.error('getCurrentPositionState() FAILED for ' + this.name);
reject(new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE));
}
// this.getCurrentPosition().then(currentPosition => {
// if (Math.abs(this.targetPosition - (currentPosition as number)) <= 5) {
// resolve(this.platform.Characteristic.PositionState.STOPPED);
// } else if (this.targetPosition > Number(currentPosition)) {
// resolve(this.platform.Characteristic.PositionState.INCREASING);
// } else {
// resolve(this.platform.Characteristic.PositionState.DECREASING);
// }
// }).catch(() => {
// reject(new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE));
// });
});
});
}
Expand Down Expand Up @@ -203,7 +143,12 @@ export class WindowCoveringService extends BaseService {
this.getStatus().then(success => {

if (success) {
const position = this.deviceStatus.status.windowShadeLevel.shadeLevel.value;
let position = 0;
if (this.useWindowShadeLevel) {
position = this.deviceStatus.status.windowShadeLevel.shadeLevel.value;
} else {
position = this.deviceStatus.status.switchLevel.level.value;
}
this.log.debug('onGet() SUCCESSFUL for ' + this.name + '. value = ' + position);
resolve(position);
} else {
Expand All @@ -215,7 +160,7 @@ export class WindowCoveringService extends BaseService {
}

public processEvent(event: ShortEvent): void {
if (event.capability === 'windowShadeLevel') {
if (event.capability === 'windowShadeLevel' || event.capability === 'switchLevel') {
this.log.debug(`Event updating windowShadeLevel capability for ${this.name} to ${event.value}`);
this.service.updateCharacteristic(this.platform.Characteristic.CurrentPosition, event.value);
} else if (event.capability === 'windowShade') {
Expand Down

0 comments on commit 226c992

Please sign in to comment.