diff --git a/README.md b/README.md index 0dc9742..799b205 100644 --- a/README.md +++ b/README.md @@ -141,9 +141,10 @@ Further investigation is needed, but for now it still works. For now this accessory only supports turning the fan on and off. Further improvements are possible, but i don't have the needed hardware for testing. #### Characteristics in addition to [common characteristics](#common-accessories-characteristics) -| Parameter | Possible values | Mandatory | Description | -|:----------|:----------------|:----------|:---------------------------------------| -| Active | \ | Yes | SHNG item to set and get the fan state | +| Parameter | Possible values | Mandatory | Description | +|:--------------|:----------------|:----------|:------------------------------------------------| +| Active | \ | Yes | SHNG item to set and get the fan state. | +| RotationSpeed | \ | No | SHNG item to set and get the fan rotation speed | #### Example: diff --git a/src/Accessories/Fan.ts b/src/Accessories/Fan.ts index a8decb0..2fe8a2b 100644 --- a/src/Accessories/Fan.ts +++ b/src/Accessories/Fan.ts @@ -1,3 +1,4 @@ +import { Int16 } from 'hap-nodejs'; import { AccessoryPlugin, CharacteristicValue, @@ -13,6 +14,7 @@ export class Fan implements AccessoryPlugin { public name: string; private active = false; + private rotationSpeed = 100; constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) { this.name = accessory.name; @@ -22,14 +24,20 @@ export class Fan implements AccessoryPlugin { this.deviceService.getCharacteristic(this.platform.Characteristic.Active) .onGet(this.getActive.bind(this)) .onSet(this.setActive.bind(this)); + this.platform.shng.addMonitor(accessory.active, this.shngActiveCallback.bind(this)); + + if (accessory.rotationspeed) { + this.deviceService.getCharacteristic(this.platform.Characteristic.RotationSpeed) + .onGet(this.getRotationSpeed.bind(this)) + .onSet(this.setRotationSpeed.bind(this)); + this.platform.shng.addMonitor(accessory.rotationspeed, this.shngRotationSpeedCallback.bind(this)); + } this.informationService = new this.platform.Service.AccessoryInformation() .setCharacteristic(this.platform.Characteristic.Manufacturer, accessory.manufacturer) .setCharacteristic(this.platform.Characteristic.Model, accessory.model) .setCharacteristic(this.platform.Characteristic.SerialNumber, accessory.active); - - this.platform.shng.addMonitor(accessory.active, this.shngCallback.bind(this)); this.platform.log.info("Fan '%s' created!", accessory.name); } @@ -52,8 +60,19 @@ export class Fan implements AccessoryPlugin { this.platform.shng.setItem(this.accessory.active, this.active); } - shngCallback(value: unknown): void { - this.platform.log.debug('shngCallback:', this.accessory.name, '=', value, '(' + typeof value + ')'); + getRotationSpeed(): Nullable { + this.platform.log.info('getRotationSpeed:', this.accessory.name, 'is currently', this.rotationSpeed); + return this.rotationSpeed; + } + + setRotationSpeed(value: CharacteristicValue) { + this.rotationSpeed = value as number; + this.platform.log.info('setRotationSpeed:', this.accessory.name, 'was set to', this.rotationSpeed); + this.platform.shng.setItem(this.accessory.rotationspeed, this.rotationSpeed); + } + + shngActiveCallback(value: unknown): void { + this.platform.log.debug('shngActiveCallback:', this.accessory.name, '=', value, '(' + typeof value + ')'); if (typeof value === 'boolean') { this.active = value; this.deviceService.updateCharacteristic(this.platform.Characteristic.Active, this.active); @@ -61,4 +80,14 @@ export class Fan implements AccessoryPlugin { this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value); } } + + shngRotationSpeedCallback(value: unknown): void { + this.platform.log.debug('shngRotationSpeedCallback:', this.accessory.name, '=', value, '(' + typeof value + ')'); + if (typeof value === 'number') { + this.rotationSpeed = value; + this.deviceService.updateCharacteristic(this.platform.Characteristic.RotationSpeed, this.rotationSpeed); + } else { + this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value); + } + } }