Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full support for lutron shades #10

Merged
merged 10 commits into from
Mar 9, 2022
Prev Previous commit
Next Next commit
Simplify construction
  • Loading branch information
mzbik committed Mar 8, 2022
commit 6ba84ef0ecda24a07d7f7c8c602966d00cc7bc93
138 changes: 66 additions & 72 deletions src/homeworksAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,80 +8,74 @@ interface SetLutronBrightnessCallback { (value: number, isDimmable:boolean, Acce
* HomeworksAccessory
* An instance of this class is created for each accessory your platform registers
*/

export class HomeworksAccessory {
private service: Service;
private _service: Service;
public lutronBrightnessChangeCallback? : SetLutronBrightnessCallback;

public dimmerState = {
public _dimmerState = {
On: false,
Brightness: 0,
PositionState: 2
}

private _name;
private _integrationId;
private _UUID;
private _deviceType: string;
private _dimmable = true;


constructor(
private readonly platform: HomeworksPlatform,
private readonly accessory: PlatformAccessory,
private readonly uuid: string,
private readonly integrationId: string,
private readonly deviceType: string,
private readonly dimmable: boolean,
private readonly _platform: HomeworksPlatform,
private readonly _accessory: PlatformAccessory,
private readonly _uuid: string,
private readonly _integrationId: string,
private readonly _deviceType: string,
private readonly _dimmable: boolean,
) {

//Assign local variables
this._name = accessory.context.device.name;
this._UUID = uuid;
this._integrationId = integrationId;
this._deviceType = deviceType || 'light';
this._dimmable = dimmable;
this._name = _accessory.context.device.name;
this._deviceType = this._deviceType || 'light';

//Set Info
this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Homebridge')
.setCharacteristic(this.platform.Characteristic.Model, 'Homeworks Plugin')
.setCharacteristic(this.platform.Characteristic.SerialNumber, 'n/a')
this._accessory.getService(this._platform.Service.AccessoryInformation)!
.setCharacteristic(this._platform.Characteristic.Manufacturer, 'Homebridge')
.setCharacteristic(this._platform.Characteristic.Model, 'Homeworks Plugin')
.setCharacteristic(this._platform.Characteristic.SerialNumber, 'n/a')
// .setCharacteristic(this.platform.Characteristic.FirmwareRevision, '0.2');

if (this._deviceType === 'shade') {
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 = 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);

// Current position of the shade
this.service.getCharacteristic(this.platform.Characteristic.CurrentPosition)
this._service.getCharacteristic(this._platform.Characteristic.CurrentPosition)
.on('set', this.setCurrentPosition.bind(this))
.on('get', this.getCurrentPosition.bind(this));

// Target position of the shade
this.service.getCharacteristic(this.platform.Characteristic.TargetPosition)
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)
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;
this._dimmerState.PositionState = this._platform.Characteristic.PositionState.STOPPED;

} else {
//Assign HK Service
this.service = this.accessory.getService(this.platform.Service.Lightbulb) || this.accessory.addService(this.platform.Service.Lightbulb);
this._service = this._accessory.getService(this._platform.Service.Lightbulb) || this._accessory.addService(this._platform.Service.Lightbulb);
//Set Characteristic Name
this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.context.device.name);
this._service.setCharacteristic(this._platform.Characteristic.Name, _accessory.context.device.name);

// register handlers for the On/Off Characteristic (minimum for lightbulb)
this.service.getCharacteristic(this.platform.Characteristic.On)
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
// register handlers for the Brightness Characteristic
if (dimmable === true) {
this.service.getCharacteristic(this.platform.Characteristic.Brightness)
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
}
Expand Down Expand Up @@ -114,14 +108,14 @@ export class HomeworksAccessory {
* getUUID()
*/
public getUUID() {
return this._UUID;
return this._uuid;
}


/**
* Handle the "GET" UUID
* Handle the "GET" is dimmable
* @example
* getUUID()
* getIsDimable()
*/
public getIsDimmable() {
return this._dimmable;
Expand All @@ -137,39 +131,39 @@ export class HomeworksAccessory {
private setOn(targetValue: CharacteristicValue, callback: CharacteristicSetCallback) {
const isDimmable = this.getIsDimmable();

if (targetValue === this.dimmerState.On) {
if (targetValue === this._dimmerState.On) {
callback(null);
return;
}

this.dimmerState.On = targetValue as boolean;
this._dimmerState.On = targetValue as boolean;

if (targetValue === true) {
this.dimmerState.Brightness = 100;
this._dimmerState.Brightness = 100;
} else {
this.dimmerState.Brightness = 0;
this._dimmerState.Brightness = 0;
}

if (this.getIsDimmable() === false) { //If we are not dimmable. Assume 100% brightness on on state.
this.service.updateCharacteristic(this.platform.Characteristic.Brightness, this.dimmerState.Brightness);
this._service.updateCharacteristic(this._platform.Characteristic.Brightness, this._dimmerState.Brightness);
}

if (this.lutronBrightnessChangeCallback) {
this.lutronBrightnessChangeCallback(this.dimmerState.Brightness, isDimmable, this);
this.lutronBrightnessChangeCallback(this._dimmerState.Brightness, isDimmable, this);
}

this.platform.log.debug('[Accessory][setOn] %s [name: %s|dim: %s]', this.dimmerState.On, this._name, this._dimmable);
this._platform.log.debug('[Accessory][setOn] %s [name: %s|dim: %s]', this._dimmerState.On, this._name, this._dimmable);

callback(null);
}

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

if (isOn === true) {
this.platform.log.debug('[Accessory][getOn] %s is ON', this.getName());
this._platform.log.debug('[Accessory][getOn] %s is ON', this.getName());
} else {
this.platform.log.debug('[Accessory][getOn] %s is OFF', this.getName());
this._platform.log.debug('[Accessory][getOn] %s is OFF', this.getName());
}

callback(null, isOn); //error,value
Expand All @@ -180,27 +174,27 @@ export class HomeworksAccessory {
*/

private getBrightness(callback: CharacteristicGetCallback) {
const brightness = this.dimmerState.Brightness;
const brightness = this._dimmerState.Brightness;

this.platform.log.debug('[Accessory] Get Characteristic Brightness -> %i %s', brightness, this._name);
this._platform.log.debug('[Accessory] Get Characteristic Brightness -> %i %s', brightness, this._name);

callback(null, brightness); //error,value
}

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

if (targetValue === this.dimmerState.Brightness) {
if (targetValue === this._dimmerState.Brightness) {
callback(null);
return;
}

this.platform.log.debug('[Accessory] Set Characteristic Brightness -> %i %s', targetValue, this.getName());
this._platform.log.debug('[Accessory] Set Characteristic Brightness -> %i %s', targetValue, this.getName());

const targetBrightnessVal = targetValue as number;
this.dimmerState.Brightness = targetBrightnessVal;
this._dimmerState.Brightness = targetBrightnessVal;

if (this.lutronBrightnessChangeCallback) {
this.lutronBrightnessChangeCallback(targetBrightnessVal, this.getIsDimmable(), this);
this.lutronBrightnessChangeCallback(targetBrightnessVal, this.getIsDimmable(), this);
}


Expand All @@ -212,24 +206,24 @@ export class HomeworksAccessory {
*/

private getCurrentPosition(callback: CharacteristicGetCallback) {
const brightness = this.dimmerState.Brightness;
const brightness = this._dimmerState.Brightness;

this.platform.log.info('[Accessory] Get CurrentPosition -> %i %s', brightness, this._name);
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.info('[Accessory] Set CurrentPosition -> %i %s', targetValue, this.getName());
this._platform.log.info('[Accessory] Set CurrentPosition -> %i %s', targetValue, this.getName());

if (targetValue === this.dimmerState.Brightness) {
if (targetValue === this._dimmerState.Brightness) {
callback(null);
return;
}

const targetBrightnessVal = targetValue as number;
this.dimmerState.Brightness = targetBrightnessVal;
this._dimmerState.Brightness = targetBrightnessVal;

if (this.lutronBrightnessChangeCallback) {
this.lutronBrightnessChangeCallback(targetBrightnessVal, this.getIsDimmable(), this);
Expand All @@ -243,24 +237,24 @@ export class HomeworksAccessory {
*/

private getTargetPosition(callback: CharacteristicGetCallback) {
const brightness = this.dimmerState.Brightness;
const brightness = this._dimmerState.Brightness;

this.platform.log.info('[Accessory] Get TargetPosition -> %i %s', brightness, this._name);
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.info('[Accessory] Set TargetPosition -> %i %s', targetValue, this.getName());
this._platform.log.info('[Accessory] Set TargetPosition -> %i %s', targetValue, this.getName());

if (targetValue === this.dimmerState.Brightness) {
if (targetValue === this._dimmerState.Brightness) {
callback(null);
return;
}

const targetBrightnessVal = targetValue as number;
this.dimmerState.Brightness = targetBrightnessVal;
this._dimmerState.Brightness = targetBrightnessVal;

if (this.lutronBrightnessChangeCallback) {
this.lutronBrightnessChangeCallback(targetBrightnessVal, this.getIsDimmable(), this);
Expand All @@ -274,14 +268,14 @@ export class HomeworksAccessory {
*/

private getPositionState(callback: CharacteristicGetCallback) {
this.platform.log.info('[Accessory] Get PositionState -> %i %s', this.dimmerState.PositionState, this._name);
this._platform.log.info('[Accessory] Get PositionState -> %i %s', this._dimmerState.PositionState, this._name);

callback(null, this.dimmerState.PositionState); //error,value
callback(null, this._dimmerState.PositionState); //error,value
}

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

this.platform.log.info('[Accessory] Set PositionState -> %i %s', targetValue, this.getName());
this._platform.log.info('[Accessory] Set PositionState -> %i %s', targetValue, this.getName());

// Don't know what to do here.
callback(null); // null or error
Expand All @@ -295,20 +289,20 @@ export class HomeworksAccessory {
* With new values from processor. (set externally)
*/
public updateBrightness(targetBrightnessVal: CharacteristicValue) {
this.platform.log.info('[Accessory][updateBrightness] to %i for %s', targetBrightnessVal, this._name);
this._platform.log.info('[Accessory][updateBrightness] to %i for %s', targetBrightnessVal, this._name);

if (targetBrightnessVal === this.dimmerState.Brightness) { //If the value is the same. Ignore to save network traffic.
if (targetBrightnessVal === this._dimmerState.Brightness) { //If the value is the same. Ignore to save network traffic.
return;
}

if (targetBrightnessVal > 0) {
this.dimmerState.On = true;
this._dimmerState.On = true;
} else if (targetBrightnessVal <= 0) {
this.dimmerState.On = false;
this._dimmerState.On = false;
}

this.dimmerState.Brightness = targetBrightnessVal as number;
this.service.updateCharacteristic(this.platform.Characteristic.On, this.dimmerState.On);
this.service.updateCharacteristic(this.platform.Characteristic.Brightness, this.dimmerState.Brightness);
this._dimmerState.Brightness = targetBrightnessVal as number;
this._service.updateCharacteristic(this._platform.Characteristic.On, this._dimmerState.On);
this._service.updateCharacteristic(this._platform.Characteristic.Brightness, this._dimmerState.Brightness);
}
}