-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomekit-unpair.js
More file actions
47 lines (43 loc) · 1.6 KB
/
Copy pathhomekit-unpair.js
File metadata and controls
47 lines (43 loc) · 1.6 KB
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
const { Console } = require('console');
module.exports = function (RED) {
function PairRemoveNode(config) {
const { HttpClient } = require('hap-controller');
RED.nodes.createNode(this, config);
var node = this;
this.PairingDataDB = RED.nodes.getNode(config.pairingData);
// Handle incoming messages
node.on('input', async function (msg) {
service = msg.payload.HomeKitAccessory;
if (service != null) {
if (!service.availableToPair) {
try {
if (msg.payload.pairingData == null) {
service.pairingData = await this.PairingDataDB.GetData(msg.payload.id).pairingData;
}
else {
service.pairingData = msg.payload.pairingData.pairingData;
}
//remove pairing data from device
const client = new HttpClient(service.id, service.address, service.port, service.pairingData);
await client.removePairing(client.pairingProtocol.iOSDevicePairingID);
client.close();
//remove pairing data from DB
await this.PairingDataDB.RemoveData(service.id);
//Update and send msg
node.send([msg, null]);
}
catch (e) {
console.error(`${msg.payload.name}:`, e);
node.send[null, e];
}
}
else {
var sendMsg = { payload: { error: "Can't unpair accessory. It is already unpaired.", device: (service) } }
node.send(null, sendMsg);
}
}
});
}
// Register the node type
RED.nodes.registerType("homekit-unpair", PairRemoveNode);
};