Skip to content

Commit

Permalink
HK feature to remove phantom devices
Browse files Browse the repository at this point in the history
  • Loading branch information
aivs committed Feb 2, 2022
1 parent 5be06df commit fcb755c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
18 changes: 17 additions & 1 deletion modules/HomeKitGate/index.js
Expand Up @@ -37,6 +37,13 @@ HomeKitGate.prototype.init = function (config) {
this.config.hkDevices = {};
}

// If hkDevicesArray doesn't contain an vDevId, then remove it from the hkDevices
Object.keys(this.config.hkDevices).forEach(function(vDevId) {
if (self.config.hkDevicesArray.indexOf(vDevId) == -1) {
delete self.config.hkDevices[vDevId];
}
});
this.saveConfig();
updateSkippedDevicesList();

// define functions and helpers
Expand Down Expand Up @@ -505,6 +512,7 @@ HomeKitGate.prototype.init = function (config) {
// Add tag "homekit-skip" for all skipped devices from config
self.config.skippedDevices.forEach(function(vDevId) {
delete self.config.hkDevices[vDevId];
removeFromHkDevicesArray(vDevId);
var vDev = self.controller.devices.get(vDevId);
if (vDev !== null && vDev.get("tags").indexOf("homekit-skip") === -1 ) {
vDev.addTag("homekit-skip");
Expand All @@ -522,6 +530,13 @@ HomeKitGate.prototype.init = function (config) {
});
}

function removeFromHkDevicesArray(vDevid) {
var index = self.config.hkDevicesArray.indexOf(vDevid);
if (index !== -1) {
self.config.hkDevicesArray.splice(index, 1);
}
}

this.hkDeviceRemove = function (vDevId) {
var m = self.mapping[vDevId];
if (m) {
Expand Down Expand Up @@ -552,7 +567,6 @@ HomeKitGate.prototype.init = function (config) {
}
}


this.onDeviceWipedOut = function (vDevId) {
console.log("HK: wipe out", vDevId);

Expand Down Expand Up @@ -583,6 +597,7 @@ HomeKitGate.prototype.init = function (config) {
if (vDev.get("tags").indexOf("homekit-skip") !== -1 && self.config.skippedDevices.indexOf(vDev.id) === -1) {
self.config.skippedDevices.push(vDev.id);
delete self.config.hkDevices[vDev.id];
removeFromHkDevicesArray(vDevId);
self.saveConfig();
self.onDeviceWipedOut(vDev.id);
}
Expand Down Expand Up @@ -725,6 +740,7 @@ HomeKitGate.prototype.addDevice = function(vDevT) {
if (!_.isEqual(_.omit(this.config.hkDevices[vDevT.id], "aid"), _.omit(vDevT, "aid"))) {
var aid = (this.config.hkDevices[vDevT.id] && this.config.hkDevices[vDevT.id].aid) || (1 + Math.max(0, Math.max.apply(null, Object.keys(this.config.hkDevices).map(function(k) { return self.config.hkDevices[k].aid; }))));
this.config.hkDevices[vDevT.id] = vDevT;
this.config.hkDevicesArray.push(vDevT.id);
this.config.hkDevices[vDevT.id].aid = aid;
this.saveConfig();
}
Expand Down
17 changes: 17 additions & 0 deletions modules/HomeKitGate/module.json
Expand Up @@ -17,6 +17,7 @@
"description": "__m_descr__",
"name": "RaZberry",
"pin": "",
"hkDevicesArray": [],
"skippedDevices": []
},
"schema": {
Expand All @@ -36,6 +37,13 @@
"datasource": "namespaces",
"enum": "namespaces:devices_all:deviceId",
"required": false
},
"hkDevicesArray" : {
"type": "array",
"items": {
"type": "string",
"required": false
}
}
},
"required": false
Expand All @@ -55,6 +63,15 @@
"datasource": "namespaces",
"field": "optionLabels",
"optionLabels": "namespaces:devices_all:deviceName"
},
"hkDevicesArray": {
"label": "__l_hkDevicesArray__",
"fields": {
"item": {
"type": "text",
"readonly": true
}
}
}
}
},
Expand Down

0 comments on commit fcb755c

Please sign in to comment.