Skip to content

Commit

Permalink
fix(mqtt): Do not disconnect on cfg reload if not connected
Browse files Browse the repository at this point in the history
  • Loading branch information
depau authored and Hypfer committed Apr 12, 2021
1 parent 2b5d3e6 commit 5c74342
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions lib/mqtt/MqttController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 5c74342

Please sign in to comment.