Fix optimistic group state not published on startup. https://github.com/Koenkk/zigbee2mqtt/issues/3461

This commit is contained in:
Koen Kanters 2020-10-10 16:41:02 +02:00
parent ad0905607b
commit 723e3a3964

View File

@ -4,6 +4,7 @@ const Extension = require('./extension');
const utils = require('../util/utils'); const utils = require('../util/utils');
const postfixes = utils.getEndpointNames(); const postfixes = utils.getEndpointNames();
const stringify = require('json-stable-stringify-without-jsonify'); const stringify = require('json-stable-stringify-without-jsonify');
const equals = require('fast-deep-equal/es6');
const topicRegex = const topicRegex =
new RegExp(`^${settings.get().mqtt.base_topic}/bridge/request/group/members/(remove|add|remove_all)$`); new RegExp(`^${settings.get().mqtt.base_topic}/bridge/request/group/members/(remove|add|remove_all)$`);
@ -15,6 +16,7 @@ class Groups extends Extension {
super(zigbee, mqtt, state, publishEntityState, eventBus); super(zigbee, mqtt, state, publishEntityState, eventBus);
this.onStateChange = this.onStateChange.bind(this); this.onStateChange = this.onStateChange.bind(this);
this.legacyApi = settings.get().advanced.legacy_api; this.legacyApi = settings.get().advanced.legacy_api;
this.lastOptimisticState = {};
} }
onMQTTConnected() { onMQTTConnected() {
@ -120,8 +122,9 @@ class Groups extends Extension {
if (resolvedEntity.type === 'device') { if (resolvedEntity.type === 'device') {
for (const zigbeeGroup of zigbeeGroups) { for (const zigbeeGroup of zigbeeGroups) {
if (zigbeeGroup.hasMember(resolvedEntity.endpoint) && if (zigbeeGroup.hasMember(resolvedEntity.endpoint) &&
!utils.equalsPartial(this.state.get(zigbeeGroup.groupID) || {}, payload)) { !equals(this.lastOptimisticState[zigbeeGroup.groupID], payload)) {
if (!payload || payload.state !== 'OFF' || this.areAllMembersOff(zigbeeGroup)) { if (!payload || payload.state !== 'OFF' || this.areAllMembersOff(zigbeeGroup)) {
this.lastOptimisticState[zigbeeGroup.groupID] = payload;
await this.publishEntityState(zigbeeGroup.groupID, payload, reason); await this.publishEntityState(zigbeeGroup.groupID, payload, reason);
} }
} }