Make code more readable
This commit is contained in:
parent
f6450f9463
commit
0167799c54
@ -20,7 +20,7 @@ type RGB = {
|
|||||||
B: number;
|
B: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum LightType { ONOFF, DIMMABLE, RGB, RGBW }
|
enum LightType { ONOFF, DIMMABLE, RGB, RGBW, HSB }
|
||||||
|
|
||||||
export class Lightbulb implements AccessoryPlugin {
|
export class Lightbulb implements AccessoryPlugin {
|
||||||
private readonly deviceService: Service;
|
private readonly deviceService: Service;
|
||||||
@ -49,15 +49,31 @@ export class Lightbulb implements AccessoryPlugin {
|
|||||||
this.platform.shng.addMonitor(accessory.on, this.shngOnCallback.bind(this));
|
this.platform.shng.addMonitor(accessory.on, this.shngOnCallback.bind(this));
|
||||||
|
|
||||||
if (accessory.brightness) {
|
if (accessory.brightness) {
|
||||||
|
this.lightType = LightType.DIMMABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (accessory.hue && accessory.saturation && accessory.brightness) {
|
||||||
|
this.lightType = LightType.HSB;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (accessory.r && accessory.g && accessory.b) {
|
||||||
|
this.lightType = LightType.RGB;
|
||||||
|
if (accessory.w) {
|
||||||
|
this.lightType = LightType.RGBW;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Characteristic dimmable is valid for every light except simple ON/OFF
|
||||||
|
if (this.lightType !== LightType.ONOFF) {
|
||||||
this.deviceService.getCharacteristic(this.platform.Characteristic.Brightness)
|
this.deviceService.getCharacteristic(this.platform.Characteristic.Brightness)
|
||||||
.onGet(this.getBrightness.bind(this))
|
.onGet(this.getBrightness.bind(this))
|
||||||
.onSet(this.setBrightness.bind(this));
|
.onSet(this.setBrightness.bind(this));
|
||||||
this.platform.shng.addMonitor(accessory.brightness, this.shngBrightnessCallback.bind(this));
|
this.platform.shng.addMonitor(accessory.brightness, this.shngBrightnessCallback.bind(this));
|
||||||
this.lightType = LightType.DIMMABLE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If RGB or RGBW light
|
|
||||||
if (accessory.r && accessory.g && accessory.b) {
|
// If HSB, RGB or RGBW light
|
||||||
|
if (this.lightType === LightType.HSB || this.lightType === LightType.RGB || this.lightType === LightType.RGBW) {
|
||||||
this.deviceService.getCharacteristic(this.platform.Characteristic.Hue)
|
this.deviceService.getCharacteristic(this.platform.Characteristic.Hue)
|
||||||
.onGet(this.getHue.bind(this))
|
.onGet(this.getHue.bind(this))
|
||||||
.onSet(this.setHue.bind(this));
|
.onSet(this.setHue.bind(this));
|
||||||
@ -65,18 +81,18 @@ export class Lightbulb implements AccessoryPlugin {
|
|||||||
this.deviceService.getCharacteristic(this.platform.Characteristic.Saturation)
|
this.deviceService.getCharacteristic(this.platform.Characteristic.Saturation)
|
||||||
.onGet(this.getSaturation.bind(this))
|
.onGet(this.getSaturation.bind(this))
|
||||||
.onSet(this.setSaturation.bind(this));
|
.onSet(this.setSaturation.bind(this));
|
||||||
this.platform.shng.addMonitor(accessory.R, this.shngRCallback.bind(this));
|
if (this.lightType === LightType.RGB || this.lightType === LightType.RGBW) {
|
||||||
this.platform.shng.addMonitor(accessory.G, this.shngGCallback.bind(this));
|
this.platform.shng.addMonitor(accessory.R, this.shngRCallback.bind(this));
|
||||||
this.platform.shng.addMonitor(accessory.B, this.shngBCallback.bind(this));
|
this.platform.shng.addMonitor(accessory.G, this.shngGCallback.bind(this));
|
||||||
this.lightType = LightType.RGB;
|
this.platform.shng.addMonitor(accessory.B, this.shngBCallback.bind(this));
|
||||||
|
if (this.lightType === LightType.RGBW) {
|
||||||
if (accessory.w) {
|
this.platform.shng.addMonitor(accessory.W, this.shngWCallback.bind(this));
|
||||||
this.platform.shng.addMonitor(accessory.W, this.shngWCallback.bind(this));
|
}
|
||||||
this.lightType = LightType.RGBW;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
this.informationService =
|
this.informationService =
|
||||||
new this.platform.Service.AccessoryInformation()
|
new this.platform.Service.AccessoryInformation()
|
||||||
.setCharacteristic(this.platform.Characteristic.Manufacturer, accessory.manufacturer)
|
.setCharacteristic(this.platform.Characteristic.Manufacturer, accessory.manufacturer)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user