diff --git a/src/accessories/fan-accessory.ts b/src/accessories/fan-accessory.ts index 8631f9c..2cc5173 100644 --- a/src/accessories/fan-accessory.ts +++ b/src/accessories/fan-accessory.ts @@ -17,9 +17,16 @@ export class FanAccessory extends HubspaceAccessory{ constructor(platform: HubspacePlatform, accessory: PlatformAccessory) { super(platform, accessory, platform.Service.Fanv2); + this.configureActive(); + this.configureRotationSpeed(); + } + + private configureActive(): void{ this.service.getCharacteristic(this.platform.Characteristic.Active) .onGet(this.getActive.bind(this)); + } + private configureRotationSpeed(): void{ this.service.getCharacteristic(this.platform.Characteristic.RotationSpeed) .onGet(this.getRotationSpeed.bind(this)) .onSet(this.setRotationSpeed.bind(this)) diff --git a/src/accessories/hubspace-accessory.ts b/src/accessories/hubspace-accessory.ts index e70bbba..9aa3175 100644 --- a/src/accessories/hubspace-accessory.ts +++ b/src/accessories/hubspace-accessory.ts @@ -1,5 +1,6 @@ import { Logger, PlatformAccessory, Service, WithUUID } from 'homebridge'; import { Device } from '../models/device'; +import { DeviceFunction } from '../models/device-functions'; import { HubspacePlatform } from '../platform'; import { DeviceService } from '../services/device.service'; @@ -38,11 +39,21 @@ export abstract class HubspaceAccessory{ protected readonly platform: HubspacePlatform, protected readonly accessory: PlatformAccessory, service: WithUUID | Service - ) { + ) { this.service = accessory.getService(service as WithUUID) || this.accessory.addService(service as Service); this.log = platform.log; this.deviceService = platform.deviceService; this.device = accessory.context.device; } + + /** + * Checks whether function is supported by device + * @param deviceFunction Function to check + * @returns True if function is supported by the device otherwise false + */ + protected supportsFunction(deviceFunction: DeviceFunction): boolean{ + return this.device.functions.some(fc => fc === deviceFunction); + } + } \ No newline at end of file diff --git a/src/accessories/light-accessory.ts b/src/accessories/light-accessory.ts index 9e01f0b..e813ea4 100644 --- a/src/accessories/light-accessory.ts +++ b/src/accessories/light-accessory.ts @@ -17,10 +17,18 @@ export class LightAccessory extends HubspaceAccessory{ constructor(platform: HubspacePlatform, accessory: PlatformAccessory) { super(platform, accessory, platform.Service.Lightbulb); - // Configure power on/off handlers + this.configurePower(); + this.configureBrightness(); + } + + private configurePower(): void{ this.service.getCharacteristic(this.platform.Characteristic.On) .onGet(this.getOn.bind(this)) .onSet(this.setOn.bind(this)); + } + + private configureBrightness(): void{ + if(!this.supportsFunction(DeviceFunction.Brightness)) return; this.service.getCharacteristic(this.platform.Characteristic.Brightness) .onGet(this.getBrightness.bind(this))