forked from pgerke/homebridge-freeathome-local-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarySensorAccessory.ts
99 lines (85 loc) · 3.01 KB
/
binarySensorAccessory.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { CharacteristicValue, PlatformAccessory, Service } from "homebridge";
import { FreeAtHomeAccessory } from "./freeAtHomeAccessory";
import { FreeAtHomeContext } from "./freeAtHomeContext";
import { emptyGuid, FreeAtHomeHomebridgePlatform } from "./platform";
/**
* A binary sensor accessory.
* @description
* A binary sensor can be used to control more or less any that free@home device
* that has a binary on/off power state exposed on data point 0000 like, for example,
* binary switches or dimmers.
*/
export class BinarySensorAccessory extends FreeAtHomeAccessory {
private readonly service: Service;
/**
* Constructs a new binary sensor accessory instance.
* @param platform The free@home Homebridge platform controlling the accessory
* @param accessory The platform accessory.
*/
constructor(
readonly platform: FreeAtHomeHomebridgePlatform,
readonly accessory: PlatformAccessory<FreeAtHomeContext>
) {
super(platform, accessory);
// get the Switch service if it exists, otherwise create a new service instance
this.service =
this.accessory.getService(this.platform.Service.Switch) ||
this.accessory.addService(this.platform.Service.Switch);
// register handlers for the On/Off Characteristic
this.service
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setOn.bind(this))
.onGet(this.getOn.bind(this));
}
private async setOn(value: CharacteristicValue): Promise<void> {
// log event
this.platform.log.info(
`${this.accessory.displayName} (Binary Sensor ${
this.serialNumber
}) set characteristic On -> ${value.toString()}`
);
// set data point at SysAP
await this.platform.sysap.setDatapoint(
emptyGuid,
this.accessory.context.deviceSerial,
this.accessory.context.channelId,
"idp0000",
value ? "1" : "0"
);
}
private async getOn(): Promise<CharacteristicValue> {
// get data point from SysAP
const response = await this.platform.sysap.getDatapoint(
emptyGuid,
this.accessory.context.deviceSerial,
this.accessory.context.channelId,
"odp0000"
);
// parse value
const value = !!parseInt(response[emptyGuid].values[0]);
// log event
this.platform.log.info(
`${this.accessory.displayName} (Binary Sensor ${
this.serialNumber
}) get characteristic On -> ${value.toString()}`
);
return value;
}
public override updateDatapoint(datapoint: string, value: string): void {
// ignore unknown data points
if (datapoint !== "odp0000") return;
// parse value
const characteristicValue = !!parseInt(value);
// log event
this.platform.log.info(
`${this.accessory.displayName} (Binary Sensor ${
this.serialNumber
}) updated characteristic On -> ${characteristicValue.toString()}`
);
// asynchoronously update the characteristic
this.service.updateCharacteristic(
this.platform.Characteristic.On,
characteristicValue
);
}
}