From e8bacbc7650375c49a6b2e805aaa01c3883f1447 Mon Sep 17 00:00:00 2001 From: Koenkk Date: Tue, 8 Jan 2019 19:00:02 +0100 Subject: [PATCH] Implement banning devices. #816 --- .../mqtt_topics_and_message_structure.md | 4 ++++ lib/extension/bridgeConfig.js | 18 ++++++++++++++---- lib/zigbee.js | 4 ++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/docs/information/mqtt_topics_and_message_structure.md b/docs/information/mqtt_topics_and_message_structure.md index a0456d794a..e477023c35 100644 --- a/docs/information/mqtt_topics_and_message_structure.md +++ b/docs/information/mqtt_topics_and_message_structure.md @@ -12,6 +12,7 @@ zigbee2mqtt will output log to this endpoint. Message are always in the form of * `"pairing"`: logging when device is connecting to the network. * `"device_connected"`: send when a new device connects to the network. * `"device_removed"`: send when a device is removed from the network. +* `"device_banned"`: send when a device is banned from the network. * `"devices"`: a list of all devices, this message can be triggered by sending a message to `zigbee2mqtt/bridge/config/devices` (payload doesn't matter). ## zigbee2mqtt/bridge/config/permit_join @@ -25,6 +26,9 @@ Allows you to switch the `log_level` during runtime. This is not persistent (wil ## zigbee2mqtt/bridge/config/remove Allows you to remove devices from the network. Payload should be the `friendly_name`, e.g. `0x00158d0001b79111`. On successful remove a [`device_removed`](https://koenkk.github.io/zigbee2mqtt/information/mqtt_topics_and_message_structure.html#zigbee2mqttbridgelog) message is send. +## zigbee2mqtt/bridge/config/ban +Allows you to ban devices from the network. Payload should be the `friendly_name`, e.g. `0x00158d0001b79111`. On successful ban a [`device_banned`](https://koenkk.github.io/zigbee2mqtt/information/mqtt_topics_and_message_structure.html#zigbee2mqttbridgelog) message is send. + ## zigbee2mqtt/bridge/config/rename Allows you to change the `friendly_name` of a device on the fly. Format should be: `{"old": "OLD_FRIENDLY_NAME", "new": "NEW_FRIENDLY_NAME"}`. diff --git a/lib/extension/bridgeConfig.js b/lib/extension/bridgeConfig.js index 99697850cd..1ccefa0d48 100644 --- a/lib/extension/bridgeConfig.js +++ b/lib/extension/bridgeConfig.js @@ -18,6 +18,7 @@ class BridgeConfig { this.devices = this.devices.bind(this); this.rename = this.rename.bind(this); this.remove = this.remove.bind(this); + this.ban = this.ban.bind(this); // Set supported options this.supportedOptions = { @@ -26,6 +27,7 @@ class BridgeConfig { 'devices': this.devices, 'rename': this.rename, 'remove': this.remove, + 'ban': this.ban, }; } @@ -89,6 +91,14 @@ class BridgeConfig { } remove(topic, message) { + this.removeOrBan(false, message); + } + + ban(topic, message) { + this.removeOrBan(true, message); + } + + removeOrBan(ban, message) { message = message.toString(); const IDByFriendlyName = settings.getIeeeAddrByFriendlyName(message); const deviceID = IDByFriendlyName ? IDByFriendlyName : message; @@ -101,17 +111,17 @@ class BridgeConfig { // Remove from state this.state.remove(deviceID); - logger.info(`Successfully removed ${deviceID}`); - this.mqtt.log('device_removed', message); + logger.info(`Successfully ${ban ? 'banned' : 'removed'} ${deviceID}`); + this.mqtt.log(ban ? 'device_banned' : 'device_removed', message); }; // Remove from zigbee network. if (device) { - this.zigbee.removeDevice(deviceID, (error) => { + this.zigbee.removeDevice(deviceID, ban, (error) => { if (!error) { cleanup(); } else { - logger.error(`Failed to remove ${deviceID}`); + logger.error(`Failed to ${ban ? 'ban' : 'remove'} ${deviceID}`); } }); } else { diff --git a/lib/zigbee.js b/lib/zigbee.js index 4936bb2e36..4968bd4370 100644 --- a/lib/zigbee.js +++ b/lib/zigbee.js @@ -137,8 +137,8 @@ class Zigbee { return this.getDevices().filter((device) => device.type !== 'Coordinator'); } - removeDevice(deviceID, callback) { - this.shepherd.remove(deviceID, (error) => { + removeDevice(deviceID, ban, callback) { + this.shepherd.remove(deviceID, {reJoin: !ban}, (error) => { if (error) { logger.warn(`Failed to remove '${deviceID}', trying force remove...`); this.forceRemove(deviceID, callback);