Skip to content

fix: Only use endpoint-specific states if the device definition uses them #26019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jan 27, 2025
10 changes: 9 additions & 1 deletion lib/extension/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,15 @@ export default class Groups extends Extension {
if (this.state.exists(device)) {
const state = this.state.get(device);
const endpointNames = device.isDevice() && device.getEndpointNames();
const stateKey = endpointNames && endpointNames.length >= member.ID ? `state_${endpointNames[member.ID - 1]}` : 'state';
const stateKey =
endpointNames &&
endpointNames.length >= member.ID &&
device.definition &&
device.definition.meta &&
device.definition.meta.multiEndpoint &&
(!device.definition.meta.multiEndpointSkip || !device.definition.meta.multiEndpointSkip.includes('state'))
? `state_${endpointNames[member.ID - 1]}`
: 'state';

if (state[stateKey] === 'ON' || state[stateKey] === 'OPEN') {
return false;
Expand Down
26 changes: 26 additions & 0 deletions test/extensions/groups.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ returnDevices.push(
devices.bulb_color_2.ieeeAddr,
devices.bulb_2.ieeeAddr,
devices.GLEDOPTO_2ID.ieeeAddr,
devices.InovelliVZM31SN.ieeeAddr,
);

describe('Extension: Groups', () => {
Expand Down Expand Up @@ -335,6 +336,31 @@ describe('Extension: Groups', () => {
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_1', stringify({state: 'OFF'}), {retain: false, qos: 0});
});

it('Should publish state change off if all lights within turn off with non default-ep, but device state does not use them', async () => {
const device_1 = devices.bulb_color;
const device_2 = devices.InovelliVZM31SN;
const endpoint_1 = device_1.getEndpoint(1)!;
const endpoint_2 = device_2.getEndpoint(2)!;
const group = groups.group_1;
group.members.push(endpoint_1);
group.members.push(endpoint_2);
settings.set(['groups'], {
1: {friendly_name: 'group_1', retain: false},
});

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

await mockMQTTEvents.message('zigbee2mqtt/bulb_color/set', stringify({state: 'OFF'}));
await mockMQTTEvents.message('zigbee2mqtt/wall_switch_double/set', stringify({state_left: 'OFF'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(3);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'OFF'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/wall_switch_double', stringify({state_left: 'OFF'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_1', stringify({state: 'OFF'}), {retain: false, qos: 0});
});

it('Should not publish state change off if any lights within are still on when changed via shared group', async () => {
const device_1 = devices.bulb_color;
const device_2 = devices.bulb;
Expand Down
19 changes: 19 additions & 0 deletions test/mocks/zigbeeHerdsman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,25 @@ export const devices = {
undefined,
CUSTOM_CLUSTERS,
),
InovelliVZM31SN: new Device(
'Router',
'0xb43a31fffe2f1f6a',
59545,
4655,
[
new Endpoint(1, [], [], '0xb43a31fffe2f1f6a', [], {}, [], 1, 1, {multiEndpointSkip: ['state', 'power', 'energy', 'brightness']}),
new Endpoint(2, [], [], '0xb43a31fffe2f1f6a', [], {}, [], 1, 1, {multiEndpointSkip: ['state', 'power', 'energy', 'brightness']}),
new Endpoint(3, [], [], '0xb43a31fffe2f1f6a', [], {}, [], 1, 1, {multiEndpointSkip: ['state', 'power', 'energy', 'brightness']}),
],
true,
'Mains (single phase)',
'VZM31-SN',
false,
undefined,
undefined,
undefined,
CUSTOM_CLUSTERS,
),
};

export const mockController = {
Expand Down
Loading