From 9396bde1f3b022e0f634487d1a37d2a5127c8cb3 Mon Sep 17 00:00:00 2001 From: Koenkk Date: Mon, 28 May 2018 21:10:58 +0200 Subject: [PATCH] Add cache_state option to configuration.yaml. #70 --- lib/controller.js | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/controller.js b/lib/controller.js index afcc42862..158458197 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -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);