forked from kiwi-cam/homebridge-broadlink-rm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
platform.js
executable file
·323 lines (284 loc) · 10.2 KB
/
platform.js
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* eslint-disable indent */
const { HomebridgePlatform } = require("./base");
const { assert } = require("chai");
const npmPackage = require("./package.json");
const Accessory = require("./accessories");
const checkForUpdates = require("./helpers/checkForUpdates");
const broadlink = require("./helpers/broadlink");
const { discoverDevices } = require("./helpers/getDevice");
const { createAccessory } = require("./helpers/accessoryCreator");
const http = require("http");
const classTypes = {
"air-conditioner": Accessory.AirCon,
"air-purifier": Accessory.AirPurifier,
"humidifier-dehumidifier": Accessory.HumidifierDehumidifier,
"learn-ir": Accessory.LearnCode,
"learn-code": Accessory.LearnCode,
switch: Accessory.Switch,
"garage-door-opener": Accessory.GarageDoorOpener,
lock: Accessory.Lock,
"switch-multi": Accessory.SwitchMulti,
"switch-multi-repeat": Accessory.SwitchMultiRepeat,
"switch-repeat": Accessory.SwitchRepeat,
fan: Accessory.Fan,
fanv1: Accessory.Fanv1,
outlet: Accessory.Outlet,
light: Accessory.Light,
window: Accessory.Window,
"window-covering": Accessory.WindowCovering,
tv: Accessory.TV,
temperatureSensor: Accessory.TemperatureSensor,
humiditySensor: Accessory.HumiditySensor,
"heater-cooler": Accessory.HeaterCooler,
};
let homebridgeRef;
const BroadlinkRMPlatform = class extends HomebridgePlatform {
constructor(log, config = {}) {
super(log, config, homebridgeRef);
this.instances = [];
}
addAccessories(accessories) {
const { config, log, logLevel } = this;
this.discoverBroadlinkDevices();
this.showMessage();
setTimeout(() => checkForUpdates(log), 1800);
this.createHttpService();
if (!config.accessories) {
config.accessories = [];
}
// Add a Learn Code accessory if none exist in the config
const learnIRAccessories =
config && config.accessories && Array.isArray(config.accessories)
? config.accessories.filter(
(accessory) =>
accessory.type === "learn-ir" || accessory.type === "learn-code"
)
: [];
if (learnIRAccessories.length === 0) {
if (!config.hideLearnButton) {
const learnCodeAccessory = new Accessory.LearnCode(log, {
name: "Learn",
scanFrequency: false,
});
accessories.push(learnCodeAccessory);
}
if (!config.hideScanFrequencyButton) {
const scanFrequencyAccessory = new Accessory.LearnCode(log, {
name: "Scan Frequency",
scanFrequency: true,
});
accessories.push(scanFrequencyAccessory);
}
}
// Iterate through the config accessories
let tvs = [];
config.accessories.forEach((accessory) => {
if (!accessory.type) {
throw new Error(
`Each accessory must be configured with a "type". e.g. "switch"`
);
}
if (accessory.disabled) {
return;
}
if (!classTypes[accessory.type]) {
throw new Error(
`homebridge-broadlink-rm-pro-http doesn't support accessories of type "${accessory.type}".`
);
}
const homeKitAccessory = new classTypes[accessory.type](log, accessory);
if (classTypes[accessory.type] === classTypes.tv) {
if (accessory.subType.toLowerCase() === "stb") {
homeKitAccessory.subType =
homebridgeRef.hap.Accessory.Categories.TV_SET_TOP_BOX;
}
if (accessory.subType.toLowerCase() === "receiver") {
homeKitAccessory.subType =
homebridgeRef.hap.Accessory.Categories.AUDIO_RECEIVER;
}
if (accessory.subType.toLowerCase() === "stick") {
homeKitAccessory.subType =
homebridgeRef.hap.Accessory.Categories.TV_STREAMING_STICK;
}
if (logLevel <= 1) {
log(
`\x1b[34m[DEBUG]\x1b[0m Adding Accessory ${accessory.type} (${accessory.subType})`
);
}
tvs.push(homeKitAccessory);
return;
}
if (logLevel <= 1) {
log(
`\x1b[34m[DEBUG]\x1b[0m Adding Accessory ${accessory.type} (${accessory.subType})`
);
}
this.instances.push(homeKitAccessory);
accessories.push(homeKitAccessory);
});
if (tvs.length > 0) {
if (tvs.length > 0) {
const TV = homebridgeRef.hap.Accessory.Categories.TELEVISION;
homebridgeRef.publishExternalAccessories(
"homebridge-broadlink-rm-pro-http",
tvs.map((tv) =>
createAccessory(tv, tv.name, TV, homebridgeRef, tv.subType)
)
);
log("");
log(
`**************************************************************************************************************`
);
log(`You added TVs in your configuration!`);
log(
`Due to a HomeKit limitation you need to add any TVs to the Home app by using the Add Accessory function.`
);
log(
`There you'll find your TVs and you can use the same PIN as you using for this HomeBridge instance.`
);
log(
`**************************************************************************************************************`
);
log("");
}
}
}
discoverBroadlinkDevices() {
const { config, log, logLevel } = this;
const { hosts } = config;
if (!hosts) {
if (logLevel <= 2) {
log(
`\x1b[35m[INFO]\x1b[0m Automatically discovering Broadlink RM devices.`
);
}
discoverDevices(true, log, logLevel, config.deviceDiscoveryTimeout);
return;
}
discoverDevices(false, log, logLevel);
if (logLevel <= 2) {
log(
`\x1b[35m[INFO]\x1b[0m Automatic Broadlink RM device discovery has been disabled as the "hosts" option has been set.`
);
}
assert.isArray(
hosts,
`\x1b[31m[CONFIG ERROR] \x1b[33mhosts\x1b[0m should be an array of objects.`
);
hosts.forEach((host) => {
assert.isObject(
host,
`\x1b[31m[CONFIG ERROR] \x1b[0m Each item in the \x1b[33mhosts\x1b[0m array should be an object.`
);
const { address, isRFSupported, isRM4, mac } = host;
assert(
address,
`\x1b[31m[CONFIG ERROR] \x1b[0m Each object in the \x1b[33mhosts\x1b[0m option should contain a value for \x1b[33maddress\x1b[0m (e.g. "192.168.1.23").`
);
assert(
mac,
`\x1b[31m[CONFIG ERROR] \x1b[0m Each object in the \x1b[33mhosts\x1b[0m option should contain a unique value for \x1b[33mmac\x1b[0m (e.g. "34:ea:34:e7:d7:28").`
);
//Create manual device type
let deviceType = 0x2221;
deviceType = isRFSupported ? deviceType | 0x2 : deviceType;
deviceType = isRM4 ? deviceType | 0x4 : deviceType;
broadlink.addDevice({ address, port: 80 }, mac.toLowerCase(), deviceType);
});
}
createHttpService() {
this.requestServer = http.createServer((req, res) => {
// Set CORS headers
res.setHeader("Access-Control-Allow-Origin", "*"); // This allows all origins
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, OPTIONS, PUT, PATCH, DELETE"
);
res.setHeader(
"Access-Control-Allow-Headers",
"X-Requested-With,content-type"
);
res.setHeader("Access-Control-Allow-Credentials", "true");
// Handle preflight OPTIONS request
if (req.method === "OPTIONS") {
res.writeHead(200);
res.end();
return;
}
this.handleRequest(req, res);
});
this.requestServer.listen(18084, () =>
this.log.info("Http server listening on 18084...")
);
}
async handleRequest(request, response) {
try {
if (request.url.includes("/exec")) {
const [_url, query] = request.url ? request.url.split("?") : [];
const nameRaw = query ? query.replace("name=", "") : "";
let decoded = decodeURIComponent(nameRaw);
// replace _all_ %20 with spaces
decoded = decoded.replace(/%20/g, " ");
const instance = this.instances.find((i) => {
return i.config.name === decoded;
});
if (instance) {
await instance.exec();
} else {
this.log.error("Could not find instance for name:", decoded);
}
}
response.writeHead(204); // 204 No content
response.end();
} catch (e) {
this.log.error(e);
response.writeHead(500);
response.end();
}
}
showMessage() {
const { config, log } = this;
if (
config &&
(config.hideWelcomeMessage || config.isUnitTest || this.logLevel >= 4)
) {
log(
`\x1b[35m[INFO]\x1b[0m Running Homebridge Broadlink RM Plugin version \x1b[32m${npmPackage.version}\x1b[0m`
);
return;
}
setTimeout(() => {
log("");
log(
`**************************************************************************************************************`
);
log(
`** Welcome to version \x1b[32m${npmPackage.version}\x1b[0m of the \x1b[34mHomebridge Broadlink RM Plugin\x1b[0m!`
);
log("** ");
log(
`** Find out what's in the latest release here: \x1b[4mhttps://github.com/kiwi-cam/homebridge-broadlink-rm/blob/master/CHANGELOG.md\x1b[0m`
);
log(`** `);
log(
`** If you like this plugin then please star it on GitHub or better yet`
);
log(
`** buy me a drink using Paypal \x1b[4mhttps://paypal.me/kiwicamRM\x1b[0m.`
);
log(`**`);
log(
`** You can disable this message by adding "hideWelcomeMessage": true to the config (see config-sample.json).`
);
log(`**`);
log(
`**************************************************************************************************************`
);
log("");
}, 1500);
}
};
BroadlinkRMPlatform.setHomebridge = (homebridge) => {
homebridgeRef = homebridge;
};
module.exports = BroadlinkRMPlatform;