Skip to content
Snippets Groups Projects
Commit e01aed24 authored by KERDREUX Jerome's avatar KERDREUX Jerome
Browse files

Added extension to dump devices states

parent 28074d42
No related branches found
No related tags found
No related merge requests found
class DumpStateExtension {
constructor(zigbee, mqtt, state, publishEntityState, eventBus, settings, logger) {
this.state = state;
this.zigbee = zigbee;
this.logger = logger;
this.mqttBaseTopic = settings.get().mqtt.base_topic;
this.eventBus = eventBus;
this.mqtt = mqtt;
}
start() {
const mqttHandler = (topic, message) => {
if (topic.startsWith(`${this.mqttBaseTopic}/`) && topic.endsWith('/dump')) {
const deviceId = topic.slice(this.mqttBaseTopic.length + 1, -5);
const queriedDeviceState = this.state.state[deviceId];
console.log('Dumping:', { deviceId, });
if (queriedDeviceState) {
this.mqtt.publish(`${deviceId}`, JSON.stringify(queriedDeviceState));
} else {
this.logger.error(`Device with ID ${deviceId} not found.`);
}
}
};
this.eventBus.onMQTTMessagePublished(this, data => mqttHandler(data.topic, data.payload), this.constructor.name);
this.eventBus.onMQTTMessage(this, data => mqttHandler(data.topic, data.message), this.constructor.name);
}
async stop() {
this.eventBus.removeListeners(this.constructor.name);
}
}
module.exports = DumpStateExtension;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment