Skip to content

Commit

Permalink
Moves to simpler constructor signature
Browse files Browse the repository at this point in the history
  • Loading branch information
mzbik committed Mar 8, 2022
1 parent fb16167 commit 6c6829b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
27 changes: 12 additions & 15 deletions src/homeworksAccessory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Service, PlatformAccessory, CharacteristicValue, CharacteristicSetCallback, CharacteristicGetCallback } from 'homebridge';
import { HomeworksPlatform } from './platform';
import { ConfigDevice} from './Schemas/device';

interface SetLutronBrightnessCallback { (value: number, isDimmable:boolean, Accessory:HomeworksAccessory): void }


Expand All @@ -25,14 +27,13 @@ export class HomeworksAccessory {
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 _config: ConfigDevice,
) {

//Assign local variables
this._name = _accessory.context.device.name;
this._deviceType = this._deviceType || 'light';
// Default is light
this._config.deviceType = this._config.deviceType || 'light';

//Set Info
this._accessory.getService(this._platform.Service.AccessoryInformation)!
Expand All @@ -41,7 +42,7 @@ export class HomeworksAccessory {
.setCharacteristic(this._platform.Characteristic.SerialNumber, 'n/a');
// .setCharacteristic(this.platform.Characteristic.FirmwareRevision, '0.2');

if (this._deviceType === 'shade') {
if (this._config.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);
Expand Down Expand Up @@ -76,7 +77,7 @@ export class HomeworksAccessory {
.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) {
if (this._config.isDimmable) {
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 All @@ -92,7 +93,7 @@ export class HomeworksAccessory {
* getIntegrationId()
*/
public getIntegrationId() {
return this._integrationId;
return this._config.integrationID;
}

/**
Expand Down Expand Up @@ -120,7 +121,7 @@ export class HomeworksAccessory {
* getIsDimable()
*/
public getIsDimmable() {
return this._dimmable;
return this._config.isDimmable;
}

//*************************************
Expand Down Expand Up @@ -154,20 +155,16 @@ export class HomeworksAccessory {
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.getIsDimmable());

callback(null);
}

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

if (isOn) {
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 %s', this.getName(), isOn ? 'ON' : 'OFF');

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

Expand Down
3 changes: 2 additions & 1 deletion src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ export class HomeworksPlatform implements DynamicPlatformPlugin {

this.log.info('[Platform] Registering: %s as %s Dimmable: %s', loadedAccessory.displayName, confDevice.name, isDimmable);
// eslint-disable-next-line max-len
const hwa = new HomeworksAccessory(this, loadedAccessory, loadedAccessory.UUID, confDevice.integrationID, confDevice.deviceType, isDimmable);
const hwa =
new HomeworksAccessory(this, loadedAccessory, loadedAccessory.UUID, confDevice);
this.homeworksAccessories.push(hwa);
hwa.lutronBrightnessChangeCallback = brightnessChangeCallback;
allAddedAccesories.push(loadedAccessory);
Expand Down

0 comments on commit 6c6829b

Please sign in to comment.