Skip to content

Commit

Permalink
fix(mqtt): only retain state and configuration, not events. (#249)
Browse files Browse the repository at this point in the history
Co-authored-by: Phil Kulak <[email protected]>
  • Loading branch information
Phil Kulak and pkulak authored Oct 27, 2022
1 parent 41b7a98 commit d5de0ee
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions api/src/util/mqtt.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ module.exports.connect = () => {
};

module.exports.available = async (state) => {
if (CLIENT) this.publish({ topic: 'double-take/available', message: state });
if (CLIENT) this.publish({ topic: 'double-take/available', retain: true, message: state });
};

module.exports.subscribe = () => {
Expand Down Expand Up @@ -196,18 +196,21 @@ module.exports.recognize = (data) => {

messages.push({
topic: `${MQTT.TOPICS.CAMERAS}/${camera}/person`,
retain: true,
message: counts.person.toString(),
});

if (unknowns.length) {
messages.push({
topic: `${MQTT.TOPICS.MATCHES}/unknown`,
retain: false,
message: JSON.stringify(payload.unknown),
});

if (MQTT.TOPICS.HOMEASSISTANT) {
messages.push({
topic: `${MQTT.TOPICS.HOMEASSISTANT}/sensor/double-take/unknown/config`,
retain: true,
message: JSON.stringify({
name: 'double_take_unknown',
icon: 'mdi:account',
Expand All @@ -221,6 +224,7 @@ module.exports.recognize = (data) => {

messages.push({
topic: `${MQTT.TOPICS.HOMEASSISTANT}/sensor/double-take/unknown/state`,
retain: true,
message: JSON.stringify(payload.unknown),
});
}
Expand All @@ -232,6 +236,7 @@ module.exports.recognize = (data) => {

messages.push({
topic: `${MQTT.TOPICS.MATCHES}/${topic}`,
retain: false,
message: JSON.stringify({
...payload.match,
match,
Expand All @@ -241,6 +246,7 @@ module.exports.recognize = (data) => {
if (MQTT.TOPICS.HOMEASSISTANT) {
messages.push({
topic: `${MQTT.TOPICS.HOMEASSISTANT}/sensor/double-take/${topic}/config`,
retain: true,
message: JSON.stringify({
name: `double_take_${name}`,
icon: 'mdi:account',
Expand All @@ -254,6 +260,7 @@ module.exports.recognize = (data) => {

messages.push({
topic: `${MQTT.TOPICS.HOMEASSISTANT}/sensor/double-take/${topic}/state`,
retain: true,
message: JSON.stringify({
...payload.match,
match,
Expand All @@ -265,12 +272,14 @@ module.exports.recognize = (data) => {
if (matches.length || misses.length || unknowns.length) {
messages.push({
topic: `${MQTT.TOPICS.CAMERAS}/${camera}`,
retain: false,
message: JSON.stringify(payload.camera),
});

if (MQTT.TOPICS.HOMEASSISTANT) {
messages.push({
topic: `${MQTT.TOPICS.HOMEASSISTANT}/sensor/double-take/${camera}/config`,
retain: true,
message: JSON.stringify({
name: `double_take_${camera}`,
icon: 'mdi:camera',
Expand All @@ -284,6 +293,7 @@ module.exports.recognize = (data) => {

messages.push({
topic: `${MQTT.TOPICS.HOMEASSISTANT}/sensor/double-take/${camera}/state`,
retain: true,
message: JSON.stringify(payload.camera),
});
}
Expand All @@ -293,10 +303,11 @@ module.exports.recognize = (data) => {

clearTimeout(PERSON_RESET_TIMEOUT[camera]);
PERSON_RESET_TIMEOUT[camera] = setTimeout(() => {
this.publish({ topic: `${MQTT.TOPICS.CAMERAS}/${camera}/person`, message: '0' });
this.publish({ topic: `${MQTT.TOPICS.CAMERAS}/${camera}/person`, retain: true, message: '0' });
if (MQTT.TOPICS.HOMEASSISTANT) {
this.publish({
topic: `${MQTT.TOPICS.HOMEASSISTANT}/sensor/double-take/${camera}/state`,
retain: true,
message: JSON.stringify(payload.cameraReset),
});
}
Expand All @@ -315,7 +326,7 @@ module.exports.publish = (data) => {
if (!single && !multiple) console.error('MQTT: publish error');

const messages = single ? [{ ...data }] : data;
messages.forEach((message) => CLIENT.publish(message.topic, message.message, { retain: true }));
messages.forEach((message) => CLIENT.publish(message.topic, message.message, { retain: message.retain === true }));
};

module.exports.status = () => ({
Expand Down

0 comments on commit d5de0ee

Please sign in to comment.