Skip to content

Commit

Permalink
Implement banning devices. Koenkk#816
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Jan 8, 2019
1 parent f816da0 commit e8bacbc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
4 changes: 4 additions & 0 deletions docs/information/mqtt_topics_and_message_structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"}`.
Expand Down
18 changes: 14 additions & 4 deletions lib/extension/bridgeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -26,6 +27,7 @@ class BridgeConfig {
'devices': this.devices,
'rename': this.rename,
'remove': this.remove,
'ban': this.ban,
};
}

Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions lib/zigbee.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e8bacbc

Please sign in to comment.