Implement banning devices. #816

This commit is contained in:
Koenkk 2019-01-08 19:00:02 +01:00
parent 84f9d09b3f
commit 35b9e6204d
3 changed files with 20 additions and 6 deletions

View File

@ -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. * `"pairing"`: logging when device is connecting to the network.
* `"device_connected"`: send when a new device connects 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_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). * `"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 ## 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 ## 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. 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 ## zigbee2mqtt/bridge/config/rename
Allows you to change the `friendly_name` of a device on the fly. Allows you to change the `friendly_name` of a device on the fly.
Format should be: `{"old": "OLD_FRIENDLY_NAME", "new": "NEW_FRIENDLY_NAME"}`. Format should be: `{"old": "OLD_FRIENDLY_NAME", "new": "NEW_FRIENDLY_NAME"}`.

View File

@ -18,6 +18,7 @@ class BridgeConfig {
this.devices = this.devices.bind(this); this.devices = this.devices.bind(this);
this.rename = this.rename.bind(this); this.rename = this.rename.bind(this);
this.remove = this.remove.bind(this); this.remove = this.remove.bind(this);
this.ban = this.ban.bind(this);
// Set supported options // Set supported options
this.supportedOptions = { this.supportedOptions = {
@ -26,6 +27,7 @@ class BridgeConfig {
'devices': this.devices, 'devices': this.devices,
'rename': this.rename, 'rename': this.rename,
'remove': this.remove, 'remove': this.remove,
'ban': this.ban,
}; };
} }
@ -89,6 +91,14 @@ class BridgeConfig {
} }
remove(topic, message) { remove(topic, message) {
this.removeOrBan(false, message);
}
ban(topic, message) {
this.removeOrBan(true, message);
}
removeOrBan(ban, message) {
message = message.toString(); message = message.toString();
const IDByFriendlyName = settings.getIeeeAddrByFriendlyName(message); const IDByFriendlyName = settings.getIeeeAddrByFriendlyName(message);
const deviceID = IDByFriendlyName ? IDByFriendlyName : message; const deviceID = IDByFriendlyName ? IDByFriendlyName : message;
@ -101,17 +111,17 @@ class BridgeConfig {
// Remove from state // Remove from state
this.state.remove(deviceID); this.state.remove(deviceID);
logger.info(`Successfully removed ${deviceID}`); logger.info(`Successfully ${ban ? 'banned' : 'removed'} ${deviceID}`);
this.mqtt.log('device_removed', message); this.mqtt.log(ban ? 'device_banned' : 'device_removed', message);
}; };
// Remove from zigbee network. // Remove from zigbee network.
if (device) { if (device) {
this.zigbee.removeDevice(deviceID, (error) => { this.zigbee.removeDevice(deviceID, ban, (error) => {
if (!error) { if (!error) {
cleanup(); cleanup();
} else { } else {
logger.error(`Failed to remove ${deviceID}`); logger.error(`Failed to ${ban ? 'ban' : 'remove'} ${deviceID}`);
} }
}); });
} else { } else {

View File

@ -137,8 +137,8 @@ class Zigbee {
return this.getDevices().filter((device) => device.type !== 'Coordinator'); return this.getDevices().filter((device) => device.type !== 'Coordinator');
} }
removeDevice(deviceID, callback) { removeDevice(deviceID, ban, callback) {
this.shepherd.remove(deviceID, (error) => { this.shepherd.remove(deviceID, {reJoin: !ban}, (error) => {
if (error) { if (error) {
logger.warn(`Failed to remove '${deviceID}', trying force remove...`); logger.warn(`Failed to remove '${deviceID}', trying force remove...`);
this.forceRemove(deviceID, callback); this.forceRemove(deviceID, callback);