Skip to content

Commit

Permalink
fix(ignore): Fixed additional case of calculation of group state when…
Browse files Browse the repository at this point in the history
… including no… (#25104)
  • Loading branch information
IIIEII authored Dec 7, 2024
1 parent 639e3bb commit d891320
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
9 changes: 4 additions & 5 deletions lib/extension/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export default class Groups extends Extension {
if (
group.zh.hasMember(endpoint) &&
!equals(this.lastOptimisticState[group.ID], payload) &&
this.shouldPublishPayloadForGroup(group, payload, endpointName)
this.shouldPublishPayloadForGroup(group, payload)
) {
this.lastOptimisticState[group.ID] = payload;

Expand Down Expand Up @@ -142,7 +142,7 @@ export default class Groups extends Extension {
await this.publishEntityState(device, memberPayload, reason);

for (const zigbeeGroup of groups) {
if (zigbeeGroup.zh.hasMember(member) && this.shouldPublishPayloadForGroup(zigbeeGroup, memberPayload, endpointName)) {
if (zigbeeGroup.zh.hasMember(member) && this.shouldPublishPayloadForGroup(zigbeeGroup, payload)) {
groupsToPublish.add(zigbeeGroup);
}
}
Expand All @@ -157,12 +157,11 @@ export default class Groups extends Extension {
}
}

private shouldPublishPayloadForGroup(group: Group, payload: KeyValue, endpointName: string | undefined): boolean {
const stateKey = endpointName ? `state_${endpointName}` : 'state';
private shouldPublishPayloadForGroup(group: Group, payload: KeyValue): boolean {
return (
group.options.off_state === 'last_member_state' ||
!payload ||
(payload[stateKey] !== 'OFF' && payload[stateKey] !== 'CLOSE') ||
(payload.state !== 'OFF' && payload.state !== 'CLOSE') ||
this.areAllMembersOffOrClosed(group)
);
}
Expand Down
29 changes: 27 additions & 2 deletions test/extensions/groups.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ describe('Extension: Groups', () => {
expect(mockMQTT.publishAsync).toHaveBeenNthCalledWith(2, 'zigbee2mqtt/bulb_color', stringify({state: 'OFF'}), {retain: false, qos: 0});
});

it('Should not publish state change off if any lights within are still on when changed via device with non default-ep', async () => {
it('Should not publish state change off if any lights within with non default-ep are still on when changed via device', async () => {
const device_1 = devices.bulb_color;
const device_2 = devices.QBKG03LM;
const endpoint_1 = device_1.getEndpoint(1)!;
const endpoint_2 = device_2.getEndpoint(3)!;
const endpoint_2 = device_2.getEndpoint(2)!;
const group = groups.group_1;
group.members.push(endpoint_1);
group.members.push(endpoint_2);
Expand All @@ -285,6 +285,31 @@ describe('Extension: Groups', () => {
expect(mockMQTT.publishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'OFF'}), {retain: false, qos: 0});
});

it('Should not publish state change off if any lights within are still on when changed via device with non default-ep', async () => {
const device_1 = devices.bulb_color;
const device_2 = devices.QBKG03LM;
const endpoint_1 = device_1.getEndpoint(1)!;
const endpoint_2 = device_2.getEndpoint(2)!;
const endpoint_3 = device_2.getEndpoint(3)!;
endpoint_3.removeFromGroup(groups.ha_discovery_group);
const group = groups.group_1;
group.members.push(endpoint_1);
group.members.push(endpoint_2);
group.members.push(endpoint_3);

await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({state: 'ON'}));
await flushPromises();
mockMQTT.publishAsync.mockClear();

await mockMQTTEvents.message('zigbee2mqtt/wall_switch_double/set', stringify({state_left: 'OFF'}));
await flushPromises();
expect(mockMQTT.publishAsync).toHaveBeenCalledTimes(1);
expect(mockMQTT.publishAsync).toHaveBeenCalledWith('zigbee2mqtt/wall_switch_double', stringify({state_left: 'OFF', state_right: 'ON'}), {
retain: false,
qos: 0,
});
});

it('Should publish state change off if all lights within turn off with non default-ep', async () => {
const device_1 = devices.bulb_color;
const device_2 = devices.QBKG03LM;
Expand Down

0 comments on commit d891320

Please sign in to comment.