Don’t create state when cache_state is set to false. #1287

This commit is contained in:
Koen Kanters 2019-03-19 20:02:17 +01:00
parent 61d85af3fd
commit 5c9b31b2fc
3 changed files with 17 additions and 7 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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));
}
});