diff --git a/lib/controller.js b/lib/controller.js index 411e27ca..71581957 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -27,7 +27,10 @@ class Controller { constructor() { this.zigbee = new Zigbee(); this.mqtt = new MQTT(); - this.state = new State(); + + if (settings.get().advanced.cache_state) { + this.state = new State(); + } // Bind methods this.onMQTTConnected = this.onMQTTConnected.bind(this); @@ -155,7 +158,9 @@ class Controller { } start() { - this.state.start(); + if (this.state) { + this.state.start(); + } this.startupLogVersion(() => { this.zigbee.start(this.onZigbeeMessage, (error) => { @@ -175,7 +180,10 @@ class Controller { this.extensions.filter((e) => e.stop).forEach((e) => e.stop()); // Wrap-up - this.state.stop(); + if (this.state) { + this.state.stop(); + } + this.mqtt.disconnect(); this.zigbee.stop(callback); } @@ -210,7 +218,7 @@ class Controller { sendAllCachedStates() { this.zigbee.getAllClients().forEach((device) => { - if (this.state.exists(device.ieeeAddr)) { + if (this.state && this.state.exists(device.ieeeAddr)) { this.publishEntityState(device.ieeeAddr, this.state.get(device.ieeeAddr)); } }); @@ -221,7 +229,7 @@ class Controller { const appSettings = settings.get(); let messagePayload = {...payload}; - if (appSettings.advanced.cache_state) { + if (this.state) { // Add cached state to payload if (this.state.exists(entityID)) { messagePayload = objectAssignDeep.noMutate(this.state.get(entityID), payload); diff --git a/lib/extension/bridgeConfig.js b/lib/extension/bridgeConfig.js index b38e6763..f41e088c 100644 --- a/lib/extension/bridgeConfig.js +++ b/lib/extension/bridgeConfig.js @@ -205,7 +205,9 @@ class BridgeConfig { settings.removeDevice(deviceID); // Remove from state - this.state.remove(deviceID); + if (this.state) { + this.state.remove(deviceID); + } logger.info(`Successfully ${ban ? 'banned' : 'removed'} ${deviceID}`); this.mqtt.log(ban ? 'device_banned' : 'device_removed', message); diff --git a/lib/extension/homeassistant.js b/lib/extension/homeassistant.js index 61817072..60240501 100644 --- a/lib/extension/homeassistant.js +++ b/lib/extension/homeassistant.js @@ -774,7 +774,7 @@ class HomeAssistant { const timer = setTimeout(() => { // Publish all device states. this.zigbee.getAllClients().forEach((device) => { - if (this.state.exists(device.ieeeAddr)) { + if (this.state && this.state.exists(device.ieeeAddr)) { this.publishEntityState(device.ieeeAddr, this.state.get(device.ieeeAddr)); } });