Don't use state converter on invalid JSON and the payload is not on, off or toggle. https://github.com/Koenkk/zigbee2mqtt/issues/1348

This commit is contained in:
Koen Kanters
2020-08-17 21:29:11 +02:00
parent d167b149ad
commit 8c5fae900e
2 changed files with 13 additions and 2 deletions
+5 -2
View File
@@ -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...`);
}
}
}
+8
View File
@@ -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);