From 2c6176ff1087fbce452a9e82c3b5be12ba811cb7 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Wed, 6 Mar 2019 21:38:49 +0100 Subject: [PATCH] Fix parse topic when topic ends with postfix. https://github.com/Koenkk/zigbee2mqtt/issues/1200 --- lib/extension/devicePublish.js | 2 +- test/devicePublish.test.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/extension/devicePublish.js b/lib/extension/devicePublish.js index 672bc0a7..146cefdf 100644 --- a/lib/extension/devicePublish.js +++ b/lib/extension/devicePublish.js @@ -80,7 +80,7 @@ class DevicePublish { // Check if we have to deal with a postfix. let postfix = ''; - if (postfixes.find((p) => topic.endsWith(p))) { + if (postfixes.find((p) => topic.endsWith(`/${p}`))) { postfix = topic.substr(topic.lastIndexOf('/') + 1, topic.length); // Remove postfix from topic diff --git a/test/devicePublish.test.js b/test/devicePublish.test.js index 9573a319..5f39b020 100644 --- a/test/devicePublish.test.js +++ b/test/devicePublish.test.js @@ -59,6 +59,27 @@ describe('DevicePublish', () => { chai.assert.strictEqual(publishEntityState.getCall(0).args[2], true); }); + it('Should publish messages to zigbee devices', () => { + zigbee.publish.resetHistory(); + publishEntityState.resetHistory(); + sandbox.stub(settings, 'getIeeeAddrByFriendlyName').callsFake(() => '0x12345666'); + zigbee.getDevice = sinon.fake.returns({modelId: 'LCT003'}); + devicePublish.onMQTTMessage('zigbee2mqtt/wohnzimmer.light.wall.right/set', JSON.stringify({state: 'ON'})); + chai.assert.isTrue(zigbee.publish.calledOnce); + chai.assert.strictEqual(zigbee.publish.getCall(0).args[0], '0x12345666'); + chai.assert.strictEqual(zigbee.publish.getCall(0).args[1], 'device'); + chai.assert.strictEqual(zigbee.publish.getCall(0).args[2], 'genOnOff'); + chai.assert.strictEqual(zigbee.publish.getCall(0).args[3], 'on'); + chai.assert.strictEqual(zigbee.publish.getCall(0).args[4], 'functional'); + chai.assert.deepEqual(zigbee.publish.getCall(0).args[5], {}); + chai.assert.deepEqual(zigbee.publish.getCall(0).args[6], cfg.default); + chai.assert.deepEqual(zigbee.publish.getCall(0).args[7], null); + chai.assert.isTrue(publishEntityState.calledOnce); + chai.assert.strictEqual(publishEntityState.getCall(0).args[0], '0x12345666'); + chai.assert.deepEqual(publishEntityState.getCall(0).args[1], {state: 'ON'}); + chai.assert.strictEqual(publishEntityState.getCall(0).args[2], true); + }); + it('Should publish messages to zigbee devices when brightness is in %', () => { zigbee.publish.resetHistory(); publishEntityState.resetHistory(); @@ -571,6 +592,14 @@ describe('DevicePublish', () => { chai.assert.strictEqual(parsed.postfix, 'left'); }); + it('Should parse set with almost postfix topic', () => { + const topic = 'zigbee2mqtt/wohnzimmer.light.wall.right/set'; + const parsed = devicePublish.parseTopic(topic); + chai.assert.strictEqual(parsed.type, 'set'); + chai.assert.strictEqual(parsed.ID, 'wohnzimmer.light.wall.right'); + chai.assert.strictEqual(parsed.postfix, ''); + }); + it('Should parse set with postfix topic', () => { const topic = 'zigbee2mqtt/0x12345689/right/set'; const parsed = devicePublish.parseTopic(topic);