From fcb755cd33aa379f7147599514f6be4cc1b7e6e1 Mon Sep 17 00:00:00 2001 From: aivs Date: Wed, 2 Feb 2022 21:07:43 +0000 Subject: [PATCH] HK feature to remove phantom devices --- modules/HomeKitGate/index.js | 18 +++++++++++++++++- modules/HomeKitGate/module.json | 17 +++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/modules/HomeKitGate/index.js b/modules/HomeKitGate/index.js index 79c5d300..f6217835 100644 --- a/modules/HomeKitGate/index.js +++ b/modules/HomeKitGate/index.js @@ -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 @@ -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"); @@ -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) { @@ -552,7 +567,6 @@ HomeKitGate.prototype.init = function (config) { } } - this.onDeviceWipedOut = function (vDevId) { console.log("HK: wipe out", vDevId); @@ -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); } @@ -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(); } diff --git a/modules/HomeKitGate/module.json b/modules/HomeKitGate/module.json index 9b73e6c0..b4939226 100755 --- a/modules/HomeKitGate/module.json +++ b/modules/HomeKitGate/module.json @@ -17,6 +17,7 @@ "description": "__m_descr__", "name": "RaZberry", "pin": "", + "hkDevicesArray": [], "skippedDevices": [] }, "schema": { @@ -36,6 +37,13 @@ "datasource": "namespaces", "enum": "namespaces:devices_all:deviceId", "required": false + }, + "hkDevicesArray" : { + "type": "array", + "items": { + "type": "string", + "required": false + } } }, "required": false @@ -55,6 +63,15 @@ "datasource": "namespaces", "field": "optionLabels", "optionLabels": "namespaces:devices_all:deviceName" + }, + "hkDevicesArray": { + "label": "__l_hkDevicesArray__", + "fields": { + "item": { + "type": "text", + "readonly": true + } + } } } },