From b9ba635bea9a57298d817cced02facc355e64a55 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Mon, 27 Jul 2020 22:07:03 +0200 Subject: [PATCH] Fix crash when attribute value is undefined of null (publish empty payload in this case). #3980 --- lib/controller.js | 4 +++- test/controller.test.js | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/controller.js b/lib/controller.js index e43e9d2d1..6b02978a0 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -329,7 +329,9 @@ class Controller { } // Check Array first, since it is also an Object - if (Array.isArray(subPayload)) { + if (subPayload === null || subPayload === undefined) { + message = ''; + } else if (Array.isArray(subPayload)) { message = subPayload.map((x) => `${x}`).join(','); } else if (typeof subPayload === 'object') { return this.iteratePayloadAttributeOutput(`${topicRoot}${key}-`, subPayload, options); diff --git a/test/controller.test.js b/test/controller.test.js index 7196f751c..d0891125e 100644 --- a/test/controller.test.js +++ b/test/controller.test.js @@ -430,7 +430,7 @@ describe('Controller', () => { await controller.start(); settings.set(['experimental', 'output'], 'attribute'); MQTT.publish.mockClear(); - await controller.publishEntityState('bulb', {state: 'ON', brightness: 50, color_temp: 370, color: {r: 100, g: 50, b: 10}, dummy: {1: 'yes', 2: 'no'}}); + await controller.publishEntityState('bulb', {state: 'ON', test: undefined, test1: null, brightness: 50, color_temp: 370, color: {r: 100, g: 50, b: 10}, dummy: {1: 'yes', 2: 'no'}}); await flushPromises(); expect(MQTT.publish).toHaveBeenCalledWith("zigbee2mqtt/bulb/state", "ON", {"qos": 0, "retain": true}, expect.any(Function)); expect(MQTT.publish).toHaveBeenCalledWith("zigbee2mqtt/bulb/brightness", "50", {"qos": 0, "retain": true}, expect.any(Function)); @@ -438,6 +438,8 @@ describe('Controller', () => { expect(MQTT.publish).toHaveBeenCalledWith("zigbee2mqtt/bulb/color", '100,50,10', {"qos": 0, "retain": true}, expect.any(Function)); expect(MQTT.publish).toHaveBeenCalledWith("zigbee2mqtt/bulb/dummy-1", 'yes', {"qos": 0, "retain": true}, expect.any(Function)); expect(MQTT.publish).toHaveBeenCalledWith("zigbee2mqtt/bulb/dummy-2", 'no', {"qos": 0, "retain": true}, expect.any(Function)); + expect(MQTT.publish).toHaveBeenCalledWith("zigbee2mqtt/bulb/test1", '', {"qos": 0, "retain": true}, expect.any(Function)); + expect(MQTT.publish).toHaveBeenCalledWith("zigbee2mqtt/bulb/test", '', {"qos": 0, "retain": true}, expect.any(Function)); }); it('Publish entity state attribute_json output', async () => {