-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrations.ts
280 lines (243 loc) · 8.11 KB
/
integrations.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import mqtt from "mqtt";
import {
Accessory,
Categories,
Characteristic,
PublishInfo,
Service,
uuid,
} from "hap-nodejs";
import { CurrentDoorState, TargetDoorState } from "@ljames8/hormann-hcp-client";
import type { GarageDoorController } from "./garageDoor";
export abstract class Integration {
constructor(
readonly garageController: GarageDoorController,
protected readonly name: string,
) {}
private notifyOtherIntegrations(fn: () => void): void {
/** wrapper to call fn on other integrations */
for (const integration of this.garageController.integrations) {
if (integration !== this) {
fn();
}
}
}
protected notifyTargetDoorStateSet(newState: TargetDoorState): void {
return this.notifyOtherIntegrations(() =>
this.publishTargetDoorState?.(newState),
);
}
protected notifyLightOnStateSet(newState: boolean): void {
return this.notifyOtherIntegrations(() =>
this.publishLightOnState(newState),
);
}
// get / set handlers for target / current door state and light state
protected handleCurrentDoorStateGet(): CurrentDoorState {
return this.garageController.getCurrentState();
}
protected handleTargetDoorStateGet(): TargetDoorState {
return this.garageController.getTargetState();
}
protected handleTargetDoorStateSet(newState: TargetDoorState): Promise<void> {
// set new target state
return this.garageController.setTargetState(newState).then(() => {
// notify other integrations for new target door state
this.notifyTargetDoorStateSet(newState);
});
}
protected handleLightOnStateGet(): boolean {
return this.garageController.getLightOnState();
}
protected handleLightOnStateSet(newState: boolean): Promise<void> {
// set new lightOn state
return this.garageController.setLightOnState(newState).then(() => {
// notify other integrations for new light on state
this.notifyLightOnStateSet(newState);
});
}
// publish target / current door state and light state updates methods
abstract publishCurrentDoorState(newState: CurrentDoorState): void;
abstract publishTargetDoorState?(newState: TargetDoorState): void;
abstract publishLightOnState(newState: boolean): void;
}
export interface MQTTIntegrationOptions {
name?: string;
brokerUrl?: string;
opts?: mqtt.IClientOptions;
}
export class MQTTIntegration extends Integration {
private client: mqtt.MqttClient;
constructor(
readonly garageController: GarageDoorController,
{
name = garageController.name,
brokerUrl = "mqtt://localhost:1883",
opts,
}: MQTTIntegrationOptions = {},
) {
super(garageController, name);
this.client = mqtt.connect(brokerUrl, opts);
this.setupMQTTDiscovery();
this.listenForCommands();
}
// Setup MQTT Discovery for Home Assistant
setupMQTTDiscovery() {
const garageDoorConfig = {
name: "Garage Door",
command_topic: "garage/door/command",
state_topic: "garage/door/status",
availability_topic: "garage/door/availability",
payload_open: "open",
payload_close: "close",
state_open: "open",
state_closed: "closed",
device_class: "garage",
unique_id: this.name + "_door",
};
const lightConfig = {
name: "Garage Light",
command_topic: "garage/light/command",
state_topic: "garage/light/status",
availability_topic: "garage/light/availability",
payload_on: "on",
payload_off: "off",
unique_id: this.name + "_light",
device_class: "light",
};
// Publish discovery messages
this.client.publish(
"homeassistant/cover/garage_door/config",
JSON.stringify(garageDoorConfig),
{ retain: true },
);
this.client.publish(
"homeassistant/light/garage_light/config",
JSON.stringify(lightConfig),
{ retain: true },
);
// Set availability
this.client.publish("garage/door/availability", "online", {
retain: true,
});
this.client.publish("garage/light/availability", "online", {
retain: true,
});
}
// Listen for MQTT commands
listenForCommands() {
this.client.subscribe("garage/door/command");
this.client.subscribe("garage/light/command");
this.client.on("message", (topic, message) => {
const command = message.toString();
if (topic === "garage/door/command") {
return this.handleTargetDoorStateSet(MQTTIntegration.doorCommandToTargetDoorState(command));
}
if (topic === "garage/light/command") {
return this.handleLightOnStateSet(MQTTIntegration.lightCommandToBoolean(command));
}
});
}
static doorCommandToTargetDoorState(command: string): TargetDoorState {
if (command === "open") {
return TargetDoorState.OPEN;
} else if (command === "close") {
return TargetDoorState.CLOSED;
} else {
throw new Error(`unsupported door command ${command}`);
}
}
static lightCommandToBoolean(command: string): boolean {
return command === "on";
}
publishCurrentDoorState(newState: CurrentDoorState): void {
this.client.publish("garage/door/state", CurrentDoorState[newState]);
}
publishTargetDoorState: undefined;
publishLightOnState(newState: boolean) {
this.client.publish("garage/light/state", newState === true ? "on" : "off");
}
}
export interface HomeKitIntegrationOptions {
name?: string;
publishInfo?: PublishInfo;
}
export class HomeKitIntegration extends Integration {
private accessory: Accessory;
private garageDoorService: Service;
private lightService: Service;
constructor(
garageController: GarageDoorController,
{
name = garageController.name,
publishInfo = {
username: "11:22:33:44:55:66",
pincode: "031-45-154",
port: 51827,
},
}: HomeKitIntegrationOptions = {},
) {
super(garageController, name);
// Initialize the HomeKit accessory
this.accessory = new Accessory(
"Garage Door",
uuid.generate("hap.garage-door"),
);
this.accessory.category = Categories.GARAGE_DOOR_OPENER;
publishInfo.category = Categories.GARAGE_DOOR_OPENER;
// Setup Garage Door Service
this.garageDoorService = new Service.GarageDoorOpener("Garage Door");
this.garageDoorService
.getCharacteristic(Characteristic.TargetDoorState)
.on("set", (value, callback) => {
this.handleTargetDoorStateSet(value as TargetDoorState)
.then(() => callback(null))
.catch((error) => callback(error));
});
// Report current door state to HomeKit
this.garageDoorService
.getCharacteristic(Characteristic.CurrentDoorState)
.on("get", (callback) => {
callback(null, this.handleCurrentDoorStateGet());
});
// Add Garage Door Service to accessory
this.accessory.addService(this.garageDoorService);
// Setup Lightbulb Service
this.lightService = new Service.Lightbulb("Garage Light");
this.lightService
.getCharacteristic(Characteristic.On)
.on("set", (value, callback) => {
this.handleLightOnStateSet(value as boolean)
.then(() => callback(null))
.catch((error) => callback(error));
});
// Report current light state to HomeKit
this.lightService
.getCharacteristic(Characteristic.On)
.on("get", (callback) => {
callback(null, this.handleLightOnStateGet());
});
// Add Lightbulb Service to accessory
this.accessory.addService(this.lightService);
// Publish the HomeKit accessory
this.accessory.publish(publishInfo);
}
// Update door state in HomeKit
publishCurrentDoorState(newState: CurrentDoorState): void {
this.garageDoorService.updateCharacteristic(
Characteristic.CurrentDoorState,
newState,
);
}
// relevant to updatet target door state whenever changed for HomeKit
publishTargetDoorState(newState: TargetDoorState): void {
this.garageDoorService.updateCharacteristic(
Characteristic.TargetDoorState,
newState,
);
}
// Update light state in HomeKit
publishLightOnState(newState: boolean): void {
this.lightService.updateCharacteristic(Characteristic.On, newState);
}
}