mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-07-11 06:11:46 +00:00
Implement group optimistic ‘group_devices’ mode. https://github.com/Koenkk/zigbee2mqtt/issues/764#issuecomment-534146878
This commit is contained in:
+14
-2
@@ -84,8 +84,20 @@ class Groups extends BaseExtension {
|
||||
const zigbeeGroups = this.zigbee.getGroups();
|
||||
for (const zigbeeGroup of zigbeeGroups) {
|
||||
const settingsGroup = settings.getGroup(zigbeeGroup.groupID);
|
||||
if (settingsGroup && settingsGroup.optimistic && zigbeeGroup.hasMember(entity.endpoint)) {
|
||||
await this.publishEntityState(zigbeeGroup.groupID, payload);
|
||||
if (settingsGroup && zigbeeGroup.hasMember(entity.endpoint)) {
|
||||
if (settingsGroup.optimistic === 'group' || settingsGroup.optimistic === 'group_devices') {
|
||||
if (!this.state.is(zigbeeGroup.groupID, payload)) {
|
||||
await this.publishEntityState(zigbeeGroup.groupID, payload);
|
||||
}
|
||||
|
||||
if (settingsGroup.optimistic === 'group_devices') {
|
||||
for (const endpoint of zigbeeGroup.getMembers()) {
|
||||
if (!this.state.is(endpoint.deviceIeeeAddress, payload)) {
|
||||
await this.publishEntityState(endpoint.deviceIeeeAddress, payload);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ const data = require('./util/data');
|
||||
const fs = require('fs');
|
||||
const objectAssignDeep = require('object-assign-deep');
|
||||
const events = require('events');
|
||||
const equals = require('fast-deep-equal');
|
||||
|
||||
const saveInterval = 1000 * 60 * 5; // 5 minutes
|
||||
|
||||
@@ -68,6 +69,17 @@ class State extends events.EventEmitter {
|
||||
return this.state[ID];
|
||||
}
|
||||
|
||||
is(ID, expected) {
|
||||
const current = this.get(ID);
|
||||
if (!current) return false;
|
||||
|
||||
for (const [key, value] of Object.entries(expected)) {
|
||||
if (!equals(current[key], value)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
set(ID, state) {
|
||||
const toState = objectAssignDeep.noMutate(state);
|
||||
dontCacheProperties.forEach((property) => {
|
||||
|
||||
@@ -222,7 +222,7 @@ const schema = {
|
||||
friendly_name: {type: 'string'},
|
||||
retain: {type: 'boolean'},
|
||||
devices: {type: 'array', items: {type: 'string'}},
|
||||
optimistic: {type: 'boolean'},
|
||||
optimistic: {type: 'string', enum: ['disable', 'group', 'group_devices']},
|
||||
qos: {type: 'number'},
|
||||
},
|
||||
required: ['friendly_name', 'retain'],
|
||||
@@ -329,12 +329,12 @@ function getGroup(IDorName) {
|
||||
const settings = getWithDefaults();
|
||||
const byID = settings.groups[IDorName];
|
||||
if (byID) {
|
||||
return {optimistic: true, devices: [], ...byID, ID: Number(IDorName), friendlyName: byID.friendly_name};
|
||||
return {optimistic: 'group', devices: [], ...byID, ID: Number(IDorName), friendlyName: byID.friendly_name};
|
||||
}
|
||||
|
||||
for (const [ID, group] of Object.entries(settings.groups)) {
|
||||
if (group.friendly_name === IDorName) {
|
||||
return {optimistic: true, devices: [], ...group, ID: Number(ID), friendlyName: group.friendly_name};
|
||||
return {optimistic: 'group', devices: [], ...group, ID: Number(ID), friendlyName: group.friendly_name};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -344,7 +344,7 @@ function getGroup(IDorName) {
|
||||
function getGroups() {
|
||||
const settings = getWithDefaults();
|
||||
return Object.entries(settings.groups).map(([ID, group]) => {
|
||||
return {optimistic: true, devices: [], ...group, ID: Number(ID), friendlyName: group.friendly_name};
|
||||
return {optimistic: 'group', devices: [], ...group, ID: Number(ID), friendlyName: group.friendly_name};
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
"dependencies": {
|
||||
"ajv": "*",
|
||||
"debounce": "*",
|
||||
"fast-deep-equal": "*",
|
||||
"git-last-commit": "*",
|
||||
"js-yaml": "*",
|
||||
"mkdir-recursive": "*",
|
||||
|
||||
@@ -168,7 +168,7 @@ describe('Bridge config', () => {
|
||||
await flushPromises();
|
||||
expect(MQTT.publish.mock.calls[0][0]).toStrictEqual('zigbee2mqtt/bridge/log');
|
||||
const payload = JSON.parse(MQTT.publish.mock.calls[0][1]);
|
||||
expect(payload).toStrictEqual({"message": [{"ID": 1, "friendly_name": "group_1", "retain": false, 'devices': [], optimistic: true}, {"ID": 2, "friendly_name": "group_2", "retain": false, "devices": [], optimistic: true}], "type": "groups"});
|
||||
expect(payload).toStrictEqual({"message": [{"ID": 1, "friendly_name": "group_1", "retain": false, 'devices': [], optimistic: "group"}, {"ID": 2, "friendly_name": "group_2", "retain": false, "devices": [], optimistic: "group"}], "type": "groups"});
|
||||
});
|
||||
|
||||
it('Should allow rename devices', async () => {
|
||||
@@ -203,7 +203,7 @@ describe('Bridge config', () => {
|
||||
zigbeeHerdsman.createGroup.mockClear();
|
||||
MQTT.events.message('zigbee2mqtt/bridge/config/add_group', 'new_group');
|
||||
await flushPromises();
|
||||
expect(settings.getGroup('new_group')).toStrictEqual({"ID": 3, "friendlyName": "new_group", "friendly_name": "new_group", devices: [], optimistic: true});
|
||||
expect(settings.getGroup('new_group')).toStrictEqual({"ID": 3, "friendlyName": "new_group", "friendly_name": "new_group", devices: [], optimistic: "group"});
|
||||
expect(zigbeeHerdsman.createGroup).toHaveBeenCalledTimes(1);
|
||||
expect(zigbeeHerdsman.createGroup).toHaveBeenCalledWith(3)
|
||||
});
|
||||
|
||||
+30
-4
@@ -260,16 +260,42 @@ describe('Groups', () => {
|
||||
const endpoint = device.getEndpoint(1);
|
||||
const group = zigbeeHerdsman.groups.group_1;
|
||||
group.members.push(endpoint);
|
||||
settings.set(['groups'], {'1': {friendly_name: 'group_1', devices: [device.ieeeAddr], optimistic: false, retain: false}});
|
||||
settings.set(['groups'], {'1': {friendly_name: 'group_1', devices: [device.ieeeAddr], optimistic: 'disable', retain: false}});
|
||||
await controller.start();
|
||||
await flushPromises();
|
||||
|
||||
MQTT.publish.mockClear();
|
||||
controller.state.state = {};
|
||||
const payload = {data: {onOff: 1}, cluster: 'genOnOff', device, endpoint, type: 'attributeReport', linkquality: 10};
|
||||
await zigbeeHerdsman.events.message(payload);
|
||||
await flushPromises();
|
||||
|
||||
for (const call in MQTT.publish.mock.calls) {
|
||||
expect(call[0]).not.toBe("zigbee2mqtt/group_1")
|
||||
}
|
||||
expect(MQTT.publish).toHaveBeenCalledTimes(1);
|
||||
expect(MQTT.publish).toHaveBeenCalledWith("zigbee2mqtt/bulb_color", '{"state":"ON","linkquality":10}', {"retain": false, qos: 0}, expect.any(Function));
|
||||
});
|
||||
|
||||
it('onlythis Should publish group and members state change when a group is in optimistic group_devices', async () => {
|
||||
const device1 = zigbeeHerdsman.devices.bulb_color;
|
||||
const device2 = zigbeeHerdsman.devices.bulb;
|
||||
const group = zigbeeHerdsman.groups.group_1;
|
||||
group.members.push(device1.getEndpoint(1));
|
||||
group.members.push(device2.getEndpoint(1));
|
||||
|
||||
settings.set(['groups'], {'1': {friendly_name: 'group_1', devices: [device1.ieeeAddr, device2.ieeeAddr], optimistic: 'group_devices', retain: false}});
|
||||
await controller.start();
|
||||
await flushPromises();
|
||||
|
||||
MQTT.publish.mockClear();
|
||||
controller.state.state = {};
|
||||
controller.state.state[device2.ieeeAddr] = {state: 'OFF'};
|
||||
const payload = {data: {onOff: 1}, cluster: 'genOnOff', device: device1, endpoint: device1.getEndpoint(1), type: 'attributeReport', linkquality: 10};
|
||||
await zigbeeHerdsman.events.message(payload);
|
||||
await flushPromises();
|
||||
|
||||
expect(MQTT.publish).toHaveBeenCalledTimes(3);
|
||||
|
||||
expect(MQTT.publish).toHaveBeenCalledWith("zigbee2mqtt/group_1", '{"state":"ON"}', {"retain": false, qos: 0}, expect.any(Function));
|
||||
expect(MQTT.publish).toHaveBeenCalledWith("zigbee2mqtt/bulb_color", '{"state":"ON","linkquality":10}', {"retain": false, qos: 0}, expect.any(Function));
|
||||
expect(MQTT.publish).toHaveBeenCalledWith("zigbee2mqtt/bulb", '{"state":"ON"}', {"retain": true, qos: 0}, expect.any(Function));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -203,7 +203,7 @@ describe('Settings', () => {
|
||||
friendlyName: '123',
|
||||
friendly_name: '123',
|
||||
devices: [],
|
||||
optimistic: true,
|
||||
optimistic: 'group',
|
||||
};
|
||||
|
||||
expect(group).toStrictEqual(expected);
|
||||
@@ -229,7 +229,7 @@ describe('Settings', () => {
|
||||
friendlyName: '123',
|
||||
friendly_name: '123',
|
||||
devices: [],
|
||||
optimistic: true,
|
||||
optimistic: 'group',
|
||||
};
|
||||
|
||||
expect(group).toStrictEqual(expected);
|
||||
@@ -272,7 +272,7 @@ describe('Settings', () => {
|
||||
|
||||
const group = settings.getGroup('1');
|
||||
const expectedGroup = {
|
||||
optimistic: true,
|
||||
optimistic: 'group',
|
||||
ID: 1,
|
||||
friendlyName: '123',
|
||||
friendly_name: '123',
|
||||
|
||||
@@ -91,7 +91,7 @@ const returnDevices = [];
|
||||
|
||||
const devices = {
|
||||
'coordinator': new Device('Coordinator', '0x00124b00120144ae', 0, 0, [new Endpoint(1, [], [])], false),
|
||||
'bulb': new Device('Router', '0x000b57fffec6a5b2', 40369, 4476, [new Endpoint(1, [0,3,4,5,6,8,768,2821,4096], [5,25,32,4096])], true, "Mains (single phase)", "TRADFRI bulb E27 WS opal 980lm"),
|
||||
'bulb': new Device('Router', '0x000b57fffec6a5b2', 40369, 4476, [new Endpoint(1, [0,3,4,5,6,8,768,2821,4096], [5,25,32,4096], '0x000b57fffec6a5b2')], true, "Mains (single phase)", "TRADFRI bulb E27 WS opal 980lm"),
|
||||
'bulb_color': new Device('Router', '0x000b57fffec6a5b3', 40399, 6535, [new Endpoint(1, [0,3,4,5,6,8,768,2821,4096], [5,25,32,4096], '0x000b57fffec6a5b3')], true, "Mains (single phase)", "LLC020"),
|
||||
'remote': new Device('EndDevice', '0x0017880104e45517', 6535, 4107, [new Endpoint(1, [0], [0,3,4,6,8,5]), new Endpoint(2, [0,1,3,15,64512], [25, 6])], true, "Battery", "RWL021"),
|
||||
'unsupported': new Device('EndDevice', '0x0017880104e45518', 6536, 0, [new Endpoint(1, [0], [0,3,4,6,8,5])], true, "Battery", "notSupportedModelID"),
|
||||
|
||||
Reference in New Issue
Block a user