Fix parse topic when topic ends with postfix. https://github.com/Koenkk/zigbee2mqtt/issues/1200

This commit is contained in:
Koen Kanters 2019-03-06 21:38:49 +01:00
parent 83b8fc9778
commit 2c6176ff10
2 changed files with 30 additions and 1 deletions

View File

@ -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

View File

@ -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);