From ec4c5a37b76ef1bdb1c6360f7aeb7b9f2331f321 Mon Sep 17 00:00:00 2001 From: Adam Simonicek Date: Sat, 4 Mar 2023 21:15:48 -0500 Subject: [PATCH] Added conditions to activate features --- src/accessories/fan-accessory.ts | 7 +++++++ src/accessories/hubspace-accessory.ts | 13 ++++++++++++- src/accessories/light-accessory.ts | 10 +++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) 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))