From 60455cb5256ebb94bc1176958cc2f23ec6c3e48c Mon Sep 17 00:00:00 2001 From: Mark Zbikowski Date: Tue, 8 Mar 2022 10:20:10 -0700 Subject: [PATCH] Adds shade position maintenance Fixes typos --- src/homeworksAccessory.ts | 112 +++++++++++++++++++++++++++++++++----- src/platform.ts | 8 +-- 2 files changed, 103 insertions(+), 17 deletions(-) diff --git a/src/homeworksAccessory.ts b/src/homeworksAccessory.ts index e5120a7..2e04f25 100644 --- a/src/homeworksAccessory.ts +++ b/src/homeworksAccessory.ts @@ -15,6 +15,7 @@ export class HomeworksAccessory { public dimmerState = { On: false, Brightness: 0, + PositionState: 2 } private _name; @@ -50,16 +51,23 @@ export class HomeworksAccessory { this.service = this.accessory.getService(this.platform.Service.WindowCovering) || this.accessory.addService(this.platform.Service.WindowCovering); this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.context.device.name); - this.service.getCharacteristic(this.platform.Characteristic.On) - .on('set', this.setOn.bind(this)) // SET - bind to the `setOn` method below - .on('get', this.getOn.bind(this)); // GET - bind to the `getOn` method below + // Current position of the shade + this.service.getCharacteristic(this.platform.Characteristic.CurrentPosition) + .on('set', this.setCurrentPosition.bind(this)) + .on('get', this.getCurrentPosition.bind(this)); - // register handlers for the Brightness Characteristic - if (dimmable === true) { - this.service.getCharacteristic(this.platform.Characteristic.Brightness) - .on('set', this.setBrightness.bind(this)) // SET - bind to the 'setBrightness` method below - .on('get', this.getBrightness.bind(this)); // GET - bind to the 'getBrightness` method below - } + // Target position of the shade + this.service.getCharacteristic(this.platform.Characteristic.TargetPosition) + .on('set', this.setTargetPosition.bind(this)) + .on('get', this.getTargetPosition.bind(this)); + + // Current status of shade motion + // TODO: Is this ever invoked? + this.service.getCharacteristic(this.platform.Characteristic.PositionState) + .on('set', this.setPositionState.bind(this)) + .on('get', this.getPositionState.bind(this)); + + this.dimmerState.PositionState = this.platform.Characteristic.PositionState.STOPPED; } else { //Assign HK Service @@ -155,8 +163,6 @@ export class HomeworksAccessory { callback(null); } - - private getOn(callback: CharacteristicGetCallback) { const isOn = this.dimmerState.On; @@ -181,7 +187,6 @@ export class HomeworksAccessory { callback(null, brightness); //error,value } - private setBrightness(targetValue: CharacteristicValue, callback: CharacteristicSetCallback) { if (targetValue === this.dimmerState.Brightness) { @@ -202,6 +207,88 @@ export class HomeworksAccessory { callback(null); // null or error } + /** + * Handle the "SET/GET" CurrentPosition requests from HomeKit + */ + + private getCurrentPosition(callback: CharacteristicGetCallback) { + const brightness = this.dimmerState.Brightness; + + this.platform.log.info('[Accessory] Get CurrentPosition -> %i %s', brightness, this._name); + + callback(null, brightness); //error,value + } + + private setCurrentPosition(targetValue: CharacteristicValue, callback: CharacteristicSetCallback) { + + this.platform.log.debug('[Accessory] Set CurrentPosition -> %i %s', targetValue, this.getName()); + + if (targetValue === this.dimmerState.Brightness) { + callback(null); + return; + } + + const targetBrightnessVal = targetValue as number; + this.dimmerState.Brightness = targetBrightnessVal; + + if (this.lutronBrightnessChangeCallback) { + this.lutronBrightnessChangeCallback(targetBrightnessVal, this.getIsDimmable(), this); + } + + callback(null); // null or error + } + + /** + * Handle the "SET/GET" CurrentPosition requests from HomeKit + */ + + private getTargetPosition(callback: CharacteristicGetCallback) { + const brightness = this.dimmerState.Brightness; + + this.platform.log.info('[Accessory] Get TargetPosition -> %i %s', brightness, this._name); + + callback(null, brightness); //error,value + } + + private setTargetPosition(targetValue: CharacteristicValue, callback: CharacteristicSetCallback) { + + this.platform.log.debug('[Accessory] Set TargetPosition -> %i %s', targetValue, this.getName()); + + if (targetValue === this.dimmerState.Brightness) { + callback(null); + return; + } + + const targetBrightnessVal = targetValue as number; + this.dimmerState.Brightness = targetBrightnessVal; + + if (this.lutronBrightnessChangeCallback) { + this.lutronBrightnessChangeCallback(targetBrightnessVal, this.getIsDimmable(), this); + } + + callback(null); // null or error + } + + /** + * Handle the "SET/GET" CurrentPosition requests from HomeKit + */ + + private getPositionState(callback: CharacteristicGetCallback) { + const brightness = this.dimmerState.Brightness; + + this.platform.log.info('[Accessory] Get PositionState -> %i %s', brightness, this._name); + + callback(null, this.dimmerState.PositionState); //error,value + } + + private setPositionState(targetValue: CharacteristicValue, callback: CharacteristicSetCallback) { + + this.platform.log.debug('[Accessory] Set PositionState -> %i %s', targetValue, this.getName()); + + // Don't know what to do here. + callback(null); // null or error + } + //************************************* //* Accessory Callbacks @@ -226,5 +313,4 @@ export class HomeworksAccessory { this.service.updateCharacteristic(this.platform.Characteristic.On, this.dimmerState.On); this.service.updateCharacteristic(this.platform.Characteristic.Brightness, this.dimmerState.Brightness); } - } diff --git a/src/platform.ts b/src/platform.ts index f2ae6b7..107eddd 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -11,7 +11,7 @@ export class HomeworksPlatform implements DynamicPlatformPlugin { private configuration: Configuration = {devices:[], apiPort:23, host:'127.0.0.1', username:'', password:''}; private readonly engine: NetworkEngine; private readonly cachedPlatformAccessories: PlatformAccessory[] = []; - private readonly homeworksAccesories: HomeworksAccessory[] = []; + private readonly homeworksAccessories: HomeworksAccessory[] = []; constructor( public readonly log: Logger, @@ -79,7 +79,7 @@ export class HomeworksPlatform implements DynamicPlatformPlugin { const deviceId = splittedMessage[1]; //Assign values from splitted message const brigthness = Number(splittedMessage[splittedMessage.length-1]); const uuid = this.api.hap.uuid.generate(deviceId); - const targetDevice = this.homeworksAccesories.find(accessory => accessory.getUUID() === uuid); + const targetDevice = this.homeworksAccessories.find(accessory => accessory.getUUID() === uuid); if (targetDevice) { //If we find a device, it means we are observing it and need the value. this.log.debug('[Platform][EngineCallback] Set: %s to: %i', targetDevice.getName(), brigthness); @@ -93,7 +93,7 @@ export class HomeworksPlatform implements DynamicPlatformPlugin { // * Will be called eveytime we connect to the processor (socket) const connectedCallback = (engine: NetworkEngine) : void => { //When we connect. We want to get the latest state for the lights. So we issue a query - for (const accessory of this.homeworksAccesories) { + for (const accessory of this.homeworksAccessories) { this.log.debug('[Platform] Requesting updates for:', accessory.getName()); const command = `?OUTPUT,${accessory.getIntegrationId()},1`; engine.send(command); @@ -166,7 +166,7 @@ export class HomeworksPlatform implements DynamicPlatformPlugin { this.log.info('[Platform] Registering: %s as %s Dimmable: %s', loadedAccessory.displayName, confDevice.name, isDimmable); const hwa = new HomeworksAccessory(this, loadedAccessory, loadedAccessory.UUID, confDevice.integrationID, confDevice.deviceType, isDimmable); - this.homeworksAccesories.push(hwa); + this.homeworksAccessories.push(hwa); hwa.lutronBrightnessChangeCallback = brightnessChangeCallback; allAddedAccesories.push(loadedAccessory); } else {