diff --git a/src/multiServiceAccessory.ts b/src/multiServiceAccessory.ts index 091ec27..36b5d76 100644 --- a/src/multiServiceAccessory.ts +++ b/src/multiServiceAccessory.ts @@ -126,6 +126,10 @@ export class MultiServiceAccessory { capabilities: ['windowShade', 'windowShadeLevel'], service: WindowCoveringService, }, + { + capabilities: ['windowShade', 'switchLevel'], + service: WindowCoveringService, + }, ]; protected accessory: PlatformAccessory; diff --git a/src/services/windowCoveringService.ts b/src/services/windowCoveringService.ts index 07ac427..9a9ae64 100644 --- a/src/services/windowCoveringService.ts +++ b/src/services/windowCoveringService.ts @@ -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) { @@ -43,6 +45,10 @@ export class WindowCoveringService extends BaseService { platform.Characteristic.PositionState); } + if (this.capabilities.includes('windowShadeLevel')) { + this.useWindowShadeLevel = true; + } + } /** @@ -60,17 +66,18 @@ 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); @@ -78,62 +85,6 @@ export class WindowCoveringService extends BaseService { }); } - // 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 { return new Promise(resolve => { resolve(this.targetPosition); @@ -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)); - // }); }); }); } @@ -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 { @@ -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') {