Skip to content

Commit

Permalink
Don’t publish emtpy messages. Koenkk#1599
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Jun 19, 2019
1 parent a36ed20 commit 304b2ef
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,12 @@ class Controller {
messagePayload.device = this.getDeviceInfoForMqtt(entityID);
}

if (settings.get().experimental.output === 'json') {
this.mqtt.publish(entity.friendlyName, JSON.stringify(messagePayload), options);
} else if (settings.get().experimental.output === 'attribute') {
this.iteratePayloadForAttrOutput(entity.friendlyName+'/', messagePayload, options);
if (Object.entries(messagePayload).length) {
if (settings.get().experimental.output === 'json') {
this.mqtt.publish(entity.friendlyName, JSON.stringify(messagePayload), options);
} else if (settings.get().experimental.output === 'attribute') {
this.iteratePayloadForAttrOutput(entity.friendlyName+'/', messagePayload, options);
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions test/controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,22 @@ describe('Controller', () => {
expect(JSON.parse(mqttPublish.mock.calls[1][1])).toStrictEqual({humidity: 2});
expect(JSON.parse(mqttPublish.mock.calls[2][1])).toStrictEqual({temperature: 3});
});

it('Should not send empty messages', () => {
jest.spyOn(settings, 'get').mockReturnValue({
mqtt: {
include_device_information: false,
},
advanced: {
cache_state: true,
},
experimental: {
output: 'json',
},
});

controller.publishEntityState('0x12345678', {});
expect(mqttPublish).toHaveBeenCalledTimes(0);
});
});
});

0 comments on commit 304b2ef

Please sign in to comment.