Skip to content

Commit

Permalink
Added conditions to activate features
Browse files Browse the repository at this point in the history
  • Loading branch information
sajmonr committed Mar 5, 2023
1 parent 7902da6 commit ec4c5a3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/accessories/fan-accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
13 changes: 12 additions & 1 deletion src/accessories/hubspace-accessory.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -38,11 +39,21 @@ export abstract class HubspaceAccessory{
protected readonly platform: HubspacePlatform,
protected readonly accessory: PlatformAccessory,
service: WithUUID<typeof Service> | Service
) {
) {
this.service = accessory.getService(service as WithUUID<typeof Service>) || 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);
}

}
10 changes: 9 additions & 1 deletion src/accessories/light-accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit ec4c5a3

Please sign in to comment.