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