Skip to content

Commit

Permalink
Adds shade position maintenance
Browse files Browse the repository at this point in the history
Fixes typos
  • Loading branch information
mzbik committed Mar 8, 2022
1 parent ac8d978 commit 60455cb
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 17 deletions.
112 changes: 99 additions & 13 deletions src/homeworksAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class HomeworksAccessory {
public dimmerState = {
On: false,
Brightness: 0,
PositionState: 2
}

private _name;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -155,8 +163,6 @@ export class HomeworksAccessory {
callback(null);
}



private getOn(callback: CharacteristicGetCallback) {
const isOn = this.dimmerState.On;

Expand All @@ -181,7 +187,6 @@ export class HomeworksAccessory {
callback(null, brightness); //error,value
}


private setBrightness(targetValue: CharacteristicValue, callback: CharacteristicSetCallback) {

if (targetValue === this.dimmerState.Brightness) {
Expand All @@ -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

Expand All @@ -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);
}

}
8 changes: 4 additions & 4 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 60455cb

Please sign in to comment.