From 5c74342f94774333b42398c9a4373015bf9bbc4d Mon Sep 17 00:00:00 2001 From: Davide Depau Date: Mon, 12 Apr 2021 20:42:30 +0200 Subject: [PATCH] fix(mqtt): Do not disconnect on cfg reload if not connected --- lib/mqtt/MqttController.js | 50 ++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/lib/mqtt/MqttController.js b/lib/mqtt/MqttController.js index fcfe0c9a654..a94c3ff2ed9 100644 --- a/lib/mqtt/MqttController.js +++ b/lib/mqtt/MqttController.js @@ -72,36 +72,38 @@ class MqttController { this.config.onUpdate(async (key) => { if (key === "mqtt") { - const newConfig = this.config.get("mqtt"); - const deconfOptions = { - cleanValues: newConfig.enabled ? true : newConfig.cleanTopicsOnShutdown, - cleanHomie: newConfig.enabled ? true : newConfig.homie.cleanAttributesOnShutdown, - cleanHass: newConfig.enabled ? true : newConfig.homeassistant.cleanAutoconfOnShutdown, - unsubscribe: newConfig.enabled ? true : newConfig.clean - }; - await this.reconfigure(async () => { - // If we're shutting down indefinitely, respect user settings - // If we're just reconfiguring, take everything down with state == init so consumers know what we're up to - await this.robotHandle.deconfigure(deconfOptions); - if (this.hassEnabled) { - await this.hassController.deconfigure(deconfOptions); + if (this.enabled) { + const newConfig = this.config.get("mqtt"); + const deconfOptions = { + cleanValues: newConfig.enabled ? true : newConfig.cleanTopicsOnShutdown, + cleanHomie: newConfig.enabled ? true : newConfig.homie.cleanAttributesOnShutdown, + cleanHass: newConfig.enabled ? true : newConfig.homeassistant.cleanAutoconfOnShutdown, + unsubscribe: newConfig.enabled ? true : newConfig.clean + }; + await this.reconfigure(async () => { + // If we're shutting down indefinitely, respect user settings + // If we're just reconfiguring, take everything down with state == init so consumers know what we're up to + await this.robotHandle.deconfigure(deconfOptions); + if (this.hassEnabled) { + await this.hassController.deconfigure(deconfOptions); + } + + }, {targetState: newConfig.enabled ? HomieCommonAttributes.STATE.INIT : HomieCommonAttributes.STATE.DISCONNECTED}); + + if (!newConfig.enabled) { + try { + await this.setState(HomieCommonAttributes.STATE.LOST); + await this.setState(HomieCommonAttributes.STATE.DISCONNECTED); + } catch (e) { + Logger.warn("Failed to set MQTT state", e); + } } - }, {targetState: newConfig.enabled ? HomieCommonAttributes.STATE.INIT : HomieCommonAttributes.STATE.DISCONNECTED}); - - if (!newConfig.enabled) { - try { - await this.setState(HomieCommonAttributes.STATE.LOST); - await this.setState(HomieCommonAttributes.STATE.DISCONNECTED); - } catch (e) { - Logger.warn("Failed to set MQTT state", e); - } + await this.disconnect(); } this.loadConfig(); - await this.disconnect(); - if (this.enabled) { if (this.hassEnabled) { this.hassController = new HassController({