From 69c477da7dfc66ec71b7bea0736eef474841ded9 Mon Sep 17 00:00:00 2001 From: Christian Scheffler <1375050+ChrisScheffler@users.noreply.github.com> Date: Thu, 27 Feb 2020 21:03:56 +0100 Subject: [PATCH] added mqtt log events group_added and group_removed (#3016) --- lib/extension/bridgeConfig.js | 2 ++ test/bridgeConfig.test.js | 30 +++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/extension/bridgeConfig.js b/lib/extension/bridgeConfig.js index 708363f9..b4891126 100644 --- a/lib/extension/bridgeConfig.js +++ b/lib/extension/bridgeConfig.js @@ -249,6 +249,7 @@ class BridgeConfig extends BaseExtension { const group = settings.addGroup(name, id); this.zigbee.createGroup(group.ID); + this.mqtt.log('group_added', name); logger.info(`Added group '${name}'`); } @@ -258,6 +259,7 @@ class BridgeConfig extends BaseExtension { assert(entity && entity.type === 'group', `Group '${message}' does not exist`); settings.removeGroup(message); entity.group.removeFromDatabase(); + this.mqtt.log('group_removed', message); logger.info(`Removed group '${name}'`); } diff --git a/test/bridgeConfig.test.js b/test/bridgeConfig.test.js index 186ceb77..bee5bd5f 100644 --- a/test/bridgeConfig.test.js +++ b/test/bridgeConfig.test.js @@ -248,9 +248,15 @@ describe('Bridge config', () => { zigbeeHerdsman.createGroup.mockClear(); MQTT.events.message('zigbee2mqtt/bridge/config/add_group', 'new_group'); await flushPromises(); + expect(MQTT.publish).toHaveBeenCalledWith( + 'zigbee2mqtt/bridge/log', + JSON.stringify({type: 'group_added', message: 'new_group'}), + {qos: 0, retain: false}, + expect.any(Function) + ); expect(settings.getGroup('new_group')).toStrictEqual({"ID": 3, "friendlyName": "new_group", "friendly_name": "new_group", devices: [], optimistic: true}); expect(zigbeeHerdsman.createGroup).toHaveBeenCalledTimes(1); - expect(zigbeeHerdsman.createGroup).toHaveBeenCalledWith(3) + expect(zigbeeHerdsman.createGroup).toHaveBeenCalledWith(3); }); it('Should allow to add groups with json', async () => { @@ -259,7 +265,13 @@ describe('Bridge config', () => { await flushPromises(); expect(settings.getGroup('new_group')).toStrictEqual({"ID": 3, "friendlyName": "new_group", "friendly_name": "new_group", devices: [], optimistic: true}); expect(zigbeeHerdsman.createGroup).toHaveBeenCalledTimes(1); - expect(zigbeeHerdsman.createGroup).toHaveBeenCalledWith(3) + expect(zigbeeHerdsman.createGroup).toHaveBeenCalledWith(3); + expect(MQTT.publish).toHaveBeenCalledWith( + 'zigbee2mqtt/bridge/log', + JSON.stringify({type: 'group_added', message: 'new_group'}), + {qos: 0, retain: false}, + expect.any(Function) + ); }); it('Should allow to add groups with json specifying id', async () => { @@ -268,7 +280,13 @@ describe('Bridge config', () => { await flushPromises(); expect(settings.getGroup('new_group')).toStrictEqual({"ID": 42, "friendlyName": "new_group", "friendly_name": "new_group", devices: [], optimistic: true}); expect(zigbeeHerdsman.createGroup).toHaveBeenCalledTimes(1); - expect(zigbeeHerdsman.createGroup).toHaveBeenCalledWith(42) + expect(zigbeeHerdsman.createGroup).toHaveBeenCalledWith(42); + expect(MQTT.publish).toHaveBeenCalledWith( + 'zigbee2mqtt/bridge/log', + JSON.stringify({type: 'group_added', message: 'new_group'}), + {qos: 0, retain: false}, + expect.any(Function) + ); }); it('Should allow to add groups with json specifying only id', async () => { @@ -286,6 +304,12 @@ describe('Bridge config', () => { await flushPromises(); expect(settings.getGroup('to_be_removed')).toStrictEqual(null); expect(group.removeFromDatabase).toHaveBeenCalledTimes(1); + expect(MQTT.publish).toHaveBeenCalledWith( + 'zigbee2mqtt/bridge/log', + JSON.stringify({type: 'group_removed', message: 'group_1'}), + {qos: 0, retain: false}, + expect.any(Function) + ); }); it('Shouldnt allow add groups without id or friendly_name in json', async () => {