fix: publish availability as JSON to fix EasyMQTT parse error
Plain string 'online'/'offline' caused SyntaxError in EasyMQTT which
wildcard-subscribes and JSON.parses all messages. Switch to
{"state":"online"} payload with availability_template in HA discovery.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+27
-3
@@ -3,6 +3,12 @@ const { getClient, getPrefix } = require('./client');
|
||||
const { allInputs, allOutputs } = require('../registry');
|
||||
const { getInputType } = require('../input-types');
|
||||
|
||||
// Availability payload is JSON so EasyMQTT (wildcard subscriber) can parse it.
|
||||
// HA uses availability_template to extract the state field.
|
||||
const AVAIL_ONLINE = JSON.stringify({ state: 'online' });
|
||||
const AVAIL_OFFLINE = JSON.stringify({ state: 'offline' });
|
||||
const AVAIL_TEMPLATE = '{{ value_json.state }}';
|
||||
|
||||
function publishAll() {
|
||||
const client = getClient();
|
||||
if (!client) return;
|
||||
@@ -19,6 +25,9 @@ function publishAll() {
|
||||
state_topic: stateTopic,
|
||||
value_template: '{{ value_json.value }}',
|
||||
availability_topic: availTopic,
|
||||
availability_template: AVAIL_TEMPLATE,
|
||||
payload_available: 'online',
|
||||
payload_not_available: 'offline',
|
||||
device: {
|
||||
identifiers: [`myprimal_${inp.moduleId}`],
|
||||
name: inp.moduleName,
|
||||
@@ -50,6 +59,9 @@ function publishAll() {
|
||||
state_on: '1',
|
||||
state_off: '0',
|
||||
availability_topic: availTopic,
|
||||
availability_template: AVAIL_TEMPLATE,
|
||||
payload_available: 'online',
|
||||
payload_not_available: 'offline',
|
||||
device: {
|
||||
identifiers: [`myprimal_${out.moduleId}`],
|
||||
name: out.moduleName,
|
||||
@@ -61,16 +73,28 @@ function publishAll() {
|
||||
client.publish(configTopic, JSON.stringify(payload), { retain: true });
|
||||
}
|
||||
|
||||
// Publish availability online for all modules
|
||||
// Publish availability for all modules (JSON payload)
|
||||
const seen = new Set();
|
||||
for (const inp of allInputs()) {
|
||||
if (!seen.has(inp.moduleSlug)) {
|
||||
seen.add(inp.moduleSlug);
|
||||
getClient().publish(`${prefix}/${inp.moduleSlug}/availability`, 'online', { retain: true });
|
||||
client.publish(`${prefix}/${inp.moduleSlug}/availability`, AVAIL_ONLINE, { retain: true });
|
||||
}
|
||||
}
|
||||
for (const out of allOutputs()) {
|
||||
if (!seen.has(out.moduleSlug)) {
|
||||
seen.add(out.moduleSlug);
|
||||
client.publish(`${prefix}/${out.moduleSlug}/availability`, AVAIL_ONLINE, { retain: true });
|
||||
}
|
||||
}
|
||||
|
||||
console.log('[discovery] HA autodiscovery published');
|
||||
}
|
||||
|
||||
module.exports = { publishAll };
|
||||
function publishOffline(moduleSlug) {
|
||||
const client = getClient();
|
||||
if (!client) return;
|
||||
client.publish(`${getPrefix()}/${moduleSlug}/availability`, AVAIL_OFFLINE, { retain: true });
|
||||
}
|
||||
|
||||
module.exports = { publishAll, publishOffline };
|
||||
|
||||
Reference in New Issue
Block a user