generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shadeAccessory.ts
75 lines (58 loc) · 2.65 KB
/
shadeAccessory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
import { Shade } from './hub';
import { PowerViewHomebridgePlatform } from './platform';
export class ShadeAccessory {
private windowCoveringService: Service;
constructor(
private readonly platform: PowerViewHomebridgePlatform,
private readonly accessory: PlatformAccessory,
private readonly shade: Shade,
) {
// set accessory information
this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Hunter Douglas')
.setCharacteristic(this.platform.Characteristic.Model, 'Default-Model')
.setCharacteristic(this.platform.Characteristic.SerialNumber, 'Default-Serial');
this.windowCoveringService = this.accessory.getService(this.platform.Service.WindowCovering) ||
this.accessory.addService(this.platform.Service.WindowCovering);
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.CurrentPosition)
.onGet(this.getCurrentPosition.bind(this));
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.PositionState)
.onGet(this.getPositionState.bind(this));
this.windowCoveringService.getCharacteristic(this.platform.Characteristic.TargetPosition)
.onGet(this.getTargetPosition.bind(this))
.onSet(this.setTargetPosition.bind(this));
}
async getCurrentPosition(): Promise<CharacteristicValue> {
this.platform.log.debug('Triggered GET CurrentPosition');
return this.shade.currentPositions.primary * 100;
}
async getPositionState(): Promise<CharacteristicValue> {
this.platform.log.debug('Triggered GET PositionState');
switch (true) {
case this.shade.currentPositions.primary > this.shade.targetPositions.primary:
return this.platform.Characteristic.PositionState.INCREASING;
case this.shade.currentPositions.primary < this.shade.targetPositions.primary:
return this.platform.Characteristic.PositionState.DECREASING;
default:
return this.platform.Characteristic.PositionState.STOPPED;
}
}
async getTargetPosition(): Promise<CharacteristicValue> {
this.platform.log.debug('Triggered GET TargetPosition');
return this.shade.targetPositions.primary * 100;
}
setTargetPosition(value: CharacteristicValue) {
// in homekit land
// 100 is open
// 0 is closed?
// in powerview land
// 1 is open
// 0 is closed
this.platform.log.debug('Set Target Position -> ', value);
if (typeof value === 'number') {
const newValue = value / 100;
this.shade.setTargetPosition({ primary: newValue });
}
}
}