-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarageDoor.ts
81 lines (75 loc) · 2.27 KB
/
garageDoor.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
import {
HormannGarageDoorOpener,
CurrentDoorState,
SerialOptions,
PacketFilterParams,
HCPClient,
SerialHCPClient,
} from "@ljames8/hormann-hcp-client";
import {
Integration,
HomeKitIntegration,
HomeKitIntegrationOptions,
MQTTIntegration,
MQTTIntegrationOptions,
} from "./integrations";
export class GarageDoorController extends HormannGarageDoorOpener {
readonly integrations: Integration[] = [];
mqttIntegration: MQTTIntegration | undefined;
homekitIntegration: HomeKitIntegration | undefined;
constructor(
name: string = "garage_door",
hcpClient: HCPClient,
{
mqttOptions = {},
homekitOptions = {},
}: {
mqttOptions?: MQTTIntegrationOptions;
homekitOptions?: HomeKitIntegrationOptions;
} = {},
) {
super(name, hcpClient);
if (mqttOptions) {
this.mqttIntegration = new MQTTIntegration(this, mqttOptions);
this.integrations.push(this.mqttIntegration);
}
if (homekitOptions) {
this.homekitIntegration = new HomeKitIntegration(this, homekitOptions);
this.integrations.push(this.homekitIntegration);
}
// default print errors
this.on("error", (err) => {
this.logger(err);
});
// register publish handlers when broadcast status updates
this.on("update_door", (updatedState: CurrentDoorState) => {
for (const integration of this.integrations) {
integration.publishCurrentDoorState(updatedState);
}
});
this.on("update_light", (updatedState: boolean) => {
for (const integration of this.integrations) {
integration.publishLightOnState(updatedState);
}
});
}
}
export function createHormannGarageDoorController(
/** factory function to create a serial enabled Hormann garage door controller */
name: string | undefined,
{ path, ...rest }: SerialOptions,
{ packetTimeout = 50, filterBreaks = true, filterMaxLength = true }: PacketFilterParams = {},
{
mqttOptions = {},
homekitOptions = {},
}: {
mqttOptions?: MQTTIntegrationOptions;
homekitOptions?: HomeKitIntegrationOptions;
} = {},
): GarageDoorController {
return new GarageDoorController(
name,
new SerialHCPClient({ path, ...rest }, { packetTimeout, filterBreaks, filterMaxLength }),
{mqttOptions, homekitOptions},
);
}