Add cache_state option to configuration.yaml. #70

This commit is contained in:
Koenkk
2018-05-28 21:10:58 +02:00
parent ae01f30f71
commit 9396bde1f3
+22 -7
View File
@@ -13,6 +13,19 @@ const mqttDevicePrefixRegex = new RegExp(`${settings.get().mqtt.base_topic}/\\w+
const pollInterval = 60 * 1000; // seconds * 1000.
const softResetTimeout = 3600 * 1000; // seconds * 1000.
/**
* Home Assistant requires ALL attributes to be present in ALL MQTT messages send by the device.
* https://community.home-assistant.io/t/missing-value-with-mqtt-only-last-data-set-is-shown/47070/9
*
* Therefore zigbee2mqtt BY DEFAULT caches all values and resend it with every message.
* advanced.cache_state in configuration.yaml allows to configure this.
* https://github.com/Koenkk/zigbee2mqtt/wiki/Configuration
*/
const cacheState = settings.get().advanced && settings.get().advanced.cache_state === false ? false : true;
if (settings.get().homeassistant && !cacheState) {
logger.warn('In order for Home Assistant integration to work properly set `cache_state: true');
}
class Controller {
constructor() {
this.zigbee = new Zigbee();
@@ -320,14 +333,16 @@ class Controller {
}
mqttPublishDeviceState(deviceID, payload, cache) {
// Add cached state to payload
if (this.stateCache[deviceID]) {
payload = {...this.stateCache[deviceID], ...payload};
}
if (cacheState) {
// Add cached state to payload
if (this.stateCache[deviceID]) {
payload = {...this.stateCache[deviceID], ...payload};
}
// Update state cache with new state.
if (cache) {
this.stateCache[deviceID] = payload;
// Update state cache with new state.
if (cache) {
this.stateCache[deviceID] = payload;
}
}
const deviceSettings = settings.getDevice(deviceID);