mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2024-11-16 18:39:09 -07:00
Add force_disable_retain option (#4948)
* support configurable retain on mqtt settings since some servers throw errors and drop the connections if you try (e.g. aws iot) * Revert "support configurable retain on mqtt settings since some servers throw errors and drop the connections if you try (e.g. aws iot)" This reverts commit 40d3a9c0bc84c83d2b167e2e3c3a06ca6df80f47. * support configurable retain on mqtt settings * support subscribing to the whole base prefix * Update mqtt.js * Fixes * Fixes * Fix tests * Updates Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
This commit is contained in:
parent
9c8323326d
commit
aabb88fbf4
@ -19,7 +19,7 @@ class MQTT extends events.EventEmitter {
|
|||||||
will: {
|
will: {
|
||||||
topic: `${settings.get().mqtt.base_topic}/bridge/state`,
|
topic: `${settings.get().mqtt.base_topic}/bridge/state`,
|
||||||
payload: 'offline',
|
payload: 'offline',
|
||||||
retain: true,
|
retain: settings.get().mqtt.force_disable_retain ? false : true,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -128,8 +128,13 @@ class MQTT extends events.EventEmitter {
|
|||||||
logger.info(`MQTT publish: topic '${topic}', payload '${payload}'`);
|
logger.info(`MQTT publish: topic '${topic}', payload '${payload}'`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const actualOptions = {...options};
|
||||||
|
if (settings.get().mqtt.force_disable_retain) {
|
||||||
|
actualOptions.retain = false;
|
||||||
|
}
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
this.client.publish(topic, payload, options, () => {
|
this.client.publish(topic, payload, actualOptions, () => {
|
||||||
this.emit('publishedMessage', {topic, payload, options});
|
this.emit('publishedMessage', {topic, payload, options});
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
|
@ -17,6 +17,11 @@ const defaults = {
|
|||||||
permit_join: false,
|
permit_join: false,
|
||||||
mqtt: {
|
mqtt: {
|
||||||
include_device_information: false,
|
include_device_information: false,
|
||||||
|
/**
|
||||||
|
* Configurable force disable retain flag on mqtt publish.
|
||||||
|
* https://github.com/Koenkk/zigbee2mqtt/pull/4948
|
||||||
|
*/
|
||||||
|
force_disable_retain: false,
|
||||||
},
|
},
|
||||||
serial: {
|
serial: {
|
||||||
disable_led: false,
|
disable_led: false,
|
||||||
@ -153,6 +158,7 @@ const schema = {
|
|||||||
reject_unauthorized: {type: 'boolean'},
|
reject_unauthorized: {type: 'boolean'},
|
||||||
include_device_information: {type: 'boolean'},
|
include_device_information: {type: 'boolean'},
|
||||||
version: {type: 'number'},
|
version: {type: 'number'},
|
||||||
|
force_disable_retain: {type: 'boolean'},
|
||||||
},
|
},
|
||||||
required: ['base_topic', 'server'],
|
required: ['base_topic', 'server'],
|
||||||
},
|
},
|
||||||
|
File diff suppressed because one or more lines are too long
@ -598,4 +598,24 @@ describe('Controller', () => {
|
|||||||
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'test', { retain: false, qos: 0 }, expect.any(Function));
|
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'test', { retain: false, qos: 0 }, expect.any(Function));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Start controller with force_disable_retain', async () => {
|
||||||
|
settings.set(['mqtt', 'force_disable_retain'], true);
|
||||||
|
await controller.start();
|
||||||
|
await flushPromises();
|
||||||
|
expect(MQTT.connect).toHaveBeenCalledTimes(1);
|
||||||
|
const expected = {
|
||||||
|
"will": { "payload": "offline", "retain": false, "topic": "zigbee2mqtt/bridge/state" },
|
||||||
|
}
|
||||||
|
expect(MQTT.connect).toHaveBeenCalledWith("mqtt://localhost", expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should prevent any message being published with retain flag when force_disable_retain is set', async () => {
|
||||||
|
settings.set(['mqtt', 'force_disable_retain'], true);
|
||||||
|
await controller.mqtt.connect()
|
||||||
|
MQTT.publish.mockClear();
|
||||||
|
await controller.mqtt.publish('fo', 'bar', { retain: true })
|
||||||
|
await flushPromises();
|
||||||
|
expect(MQTT.publish).toHaveBeenCalledTimes(1);
|
||||||
|
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/fo', 'bar', { retain: false, qos: 0 }, expect.any(Function));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -153,6 +153,7 @@ describe('Settings', () => {
|
|||||||
|
|
||||||
const expected = {
|
const expected = {
|
||||||
include_device_information: false,
|
include_device_information: false,
|
||||||
|
force_disable_retain: false,
|
||||||
password: "mysecretpassword",
|
password: "mysecretpassword",
|
||||||
server: "my.mqtt.server",
|
server: "my.mqtt.server",
|
||||||
user: "mysecretusername",
|
user: "mysecretusername",
|
||||||
|
Loading…
Reference in New Issue
Block a user