From 8c5fae900eb5e7ef2a41005c584a41612ddfe308 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Mon, 17 Aug 2020 21:29:11 +0200 Subject: [PATCH] Don't use state converter on invalid JSON and the payload is not on, off or toggle. https://github.com/Koenkk/zigbee2mqtt/issues/1348 --- lib/extension/publish.js | 7 +++++-- test/publish.test.js | 8 ++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/extension/publish.js b/lib/extension/publish.js index 5ff09059c..e43e4ab6b 100644 --- a/lib/extension/publish.js +++ b/lib/extension/publish.js @@ -112,8 +112,11 @@ class EntityPublish extends Extension { try { json = JSON.parse(message); } catch (e) { - // Cannot be parsed to JSON, assume state message. - json = {state: message}; + if (['on', 'off', 'toggle'].includes(message.toLowerCase())) { + json = {state: message}; + } else { + logger.error(`Invalid JSON '${message}', skipping...`); + } } } diff --git a/test/publish.test.js b/test/publish.test.js index 362f9b6b5..b82a5b57f 100644 --- a/test/publish.test.js +++ b/test/publish.test.js @@ -559,6 +559,14 @@ describe('Publish', () => { expect(MQTT.publish.mock.calls[1]).toEqual(["zigbee2mqtt/wall_switch_double", stringify({state_left: 'OFF'}), {"qos": 0, "retain": false}, expect.any(Function)]); }); + it('Should not use state converter on non-json message when value is not on/off/toggle', async () => { + const device = zigbeeHerdsman.devices.QBKG03LM; + const endpoint = device.getEndpoint(2); + await MQTT.events.message('zigbee2mqtt/wall_switch_double/left/set', 'ON_RANDOM'); + await flushPromises(); + expect(endpoint.command).toHaveBeenCalledTimes(0); + }); + it('Should parse set with postfix topic and attribute', async () => { const device = zigbeeHerdsman.devices.QBKG03LM; const endpoint = device.getEndpoint(2);