forked from pgerke/homebridge-freeathome-local-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotionSensorAccessory.ts
74 lines (64 loc) · 2.41 KB
/
motionSensorAccessory.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
import { PlatformAccessory, Service } from "homebridge";
import { FreeAtHomeAccessory } from "./freeAtHomeAccessory";
import { FreeAtHomeContext } from "./freeAtHomeContext";
import { FreeAtHomeHomebridgePlatform } from "./platform";
/** A motion sensor accessory. */
export class MotionSensorAccessory extends FreeAtHomeAccessory {
private readonly service: Service;
private motionDetected: boolean;
private resetTimeout?: NodeJS.Timeout;
/**
* Constructs a new motion 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);
// set initial state
this.motionDetected = !!parseInt(
this.accessory.context.channel.outputs?.odp0000.value ?? "0"
);
this.processAutoResetTimer();
// get the MotionSensor service if it exists, otherwise create a new service instance
this.service =
this.accessory.getService(this.platform.Service.MotionSensor) ||
this.accessory.addService(this.platform.Service.MotionSensor);
// register handlers for the motion detected characteristic
this.service
.getCharacteristic(this.platform.Characteristic.MotionDetected)
.onGet(() => this.motionDetected);
}
public override updateDatapoint(datapoint: string, value: string): void {
// ignore unknown data points
if (datapoint !== "odp0000") return;
// do the update
this.motionDetected = !!parseInt(value);
this.processAutoResetTimer();
this.doUpdateDatapoint(
"Motion Sensor",
this.service,
this.platform.Characteristic.MotionDetected,
this.motionDetected
);
}
private processAutoResetTimer(): void {
// Set, reset or cancel a timer, if automatic reset is enabled
if (this.platform.config.motionSensorAutoReset as boolean) {
// If a timer is running, clear it
if (!!this.resetTimeout) {
clearTimeout(this.resetTimeout);
this.resetTimeout = undefined;
}
// Set a new reset timer, if motion was detected.
if (this.motionDetected) {
this.resetTimeout = setTimeout(
() => this.updateDatapoint("odp0000", "0"),
this.platform.config.motionSensorDefaultResetTimer as number
);
}
}
}
}