Files
myprimal/src/mqtt/config-bridge.js
T
arnaudne 941abd98d5 feat: initial myprimal MQTT bridge service
Dynamic MySolem/MyIndygo IoT bridge with MQTT, Home Assistant
autodiscovery, and Homebridge EasyMQTT compatibility.

- Device discovery via MySolem API on startup (all modules/inputs/outputs)
- Configurable poll engine (fast/normal/slow buckets, per-input overrides)
- MQTT retained state publishing + HA autodiscovery payloads
- Unified manual command routing (linesControl vs sendManualModuleCommand)
- Runtime config via MQTT $config/set, persisted to config.json
- Expression override system (API-provided > hardcoded > passthrough)
- Minimal REST API: /health, /state, /control, /rediscover
- Docker deployment (Dockerfile + docker-compose.yml)
- 38 unit tests

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-28 01:05:24 +02:00

42 lines
1.0 KiB
JavaScript

'use strict';
const { getClient, getPrefix, publish } = require('./client');
const { getConfig, patchConfig, on: onConfigChange } = require('../config');
const poller = require('../poller');
function subscribe() {
const client = getClient();
if (!client) return;
const prefix = getPrefix();
const setTopic = `${prefix}/$config/set`;
client.subscribe(setTopic);
client.on('message', (topic, buf) => {
if (topic !== setTopic) return;
let patch;
try {
patch = JSON.parse(buf.toString());
} catch {
console.warn('[config-bridge] Invalid JSON on $config/set');
return;
}
patchConfig(patch);
console.log('[config-bridge] Config updated:', JSON.stringify(patch));
});
// Republish current config on connect
publishConfig();
// Republish + reload poller when config changes
onConfigChange('change', () => {
publishConfig();
poller.reload();
});
}
function publishConfig() {
publish('$config', getConfig());
}
module.exports = { subscribe, publishConfig };