diff --git a/lib/extension/publish.js b/lib/extension/publish.js index aa0d044164..985b6b83a9 100644 --- a/lib/extension/publish.js +++ b/lib/extension/publish.js @@ -223,7 +223,8 @@ class EntityPublish extends Extension { if (topic.type === 'set' && converter.convertSet) { logger.debug(`Publishing '${topic.type}' '${key}' to '${resolvedEntity.name}'`); const result = await converter.convertSet(actualTarget, key, value, meta); - if (result && result.state) { + const optimistic = !options.hasOwnProperty('optimistic') || options.optimistic; + if (result && result.state && optimistic) { const msg = result.state; if (endpointName) { @@ -238,7 +239,7 @@ class EntityPublish extends Extension { this.publishEntityState(resolvedEntity.settings.ID, msg); } - if (result && result.membersState) { + if (result && result.membersState && optimistic) { for (const [ieeeAddr, state] of Object.entries(result.membersState)) { this.publishEntityState(ieeeAddr, state); } diff --git a/test/publish.test.js b/test/publish.test.js index 5504796c37..eb077e9b29 100644 --- a/test/publish.test.js +++ b/test/publish.test.js @@ -396,6 +396,17 @@ describe('Publish', () => { expect(zigbeeHerdsman.groups.group_2.command).toHaveBeenCalledWith("genOnOff", "on", {}, {}); }); + it('Shouldnt publish new state when optimistic = false', async () => { + const device = zigbeeHerdsman.devices.bulb_color; + const endpoint = device.getEndpoint(1); + settings.set(['devices', device.ieeeAddr, 'optimistic'], false); + await MQTT.events.message('zigbee2mqtt/bulb_color/set', stringify({brightness: '200'})); + await flushPromises(); + expect(endpoint.command).toHaveBeenCalledTimes(1); + expect(endpoint.command).toHaveBeenCalledWith("genLevelCtrl", "moveToLevelWithOnOff", {"level": 200, "transtime": 0}, {}); + expect(MQTT.publish).toHaveBeenCalledTimes(0); + }); + it('Should handle non-valid topics', async () => { await MQTT.events.message('zigbee2mqtt1/bulb_color/set', stringify({state: 'ON'})); await flushPromises();