Skip to content

Commit

Permalink
Don’t create state when cache_state is set to false. Koenkk#1287
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Mar 19, 2019
1 parent a7abe2c commit 2a3f91d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
18 changes: 13 additions & 5 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -155,7 +158,9 @@ class Controller {
}

start() {
this.state.start();
if (this.state) {
this.state.start();
}

this.startupLogVersion(() => {
this.zigbee.start(this.onZigbeeMessage, (error) => {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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));
}
});
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion lib/extension/bridgeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/extension/homeassistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});
Expand Down

0 comments on commit 2a3f91d

Please sign in to comment.