Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let allGroupMembers = null;
- let groupsChecked = [];
- let setStateIds = {};
- let existingStateIds = {};
- function getGroupMembers() {
- let groupMembers = {};
- let devsWithGroup = $('zigbee.0.*.groups');
- devsWithGroup.each(function(stateid) {
- if(stateid.indexOf('zigbee.0.info.') === 0) {
- return this;
- }
- let deviceGroups = getState(stateid).val;
- if(!deviceGroups) {
- return this;
- }
- deviceGroups = JSON.parse(deviceGroups);
- if(!deviceGroups) {
- return this;
- }
- let deviceId = stateid.substr(0, stateid.length - 7);
- let groupIdx;
- for(let i = 0; i < deviceGroups.length; i++) {
- groupIdx = 'group_' + deviceGroups[i];
- if(!groupMembers[groupIdx]) {
- groupMembers[groupIdx] = [];
- }
- groupMembers[groupIdx].push(deviceId);
- }
- });
- return groupMembers;
- }
- function checkDeviceGroups(deviceId, attribute, value, keepMembers) {
- if(!keepMembers) {
- // (re-)read group members
- allGroupMembers = getGroupMembers();
- groupsChecked = [];
- setStateIds = {};
- }
- let groupsToCheck = [];
- log('checkDeviceGroups(' + deviceId + ', ' + attribute + (keepMembers ? ', true' : '') + ')', 'debug');
- if(deviceId.indexOf('zigbee.0.group_') === 0) {
- // this is a group
- let groupIdx = deviceId.replace(/^zigbee\.0\./, '');
- // set value of devices to that of group. We need to do that before we dive into checkDeviceGroups for each device due to group checks
- for(let m = 0; m < allGroupMembers[groupIdx].length; m++) {
- log('Copy state ' + attribute + ' of ' + groupIdx + ' with value ' + value + ' to ' + allGroupMembers[groupIdx][m] + '.' + attribute, 'debug');
- let stateId = allGroupMembers[groupIdx][m] + '.' + attribute;
- setState(stateId, value, true); // not trigger changes again!
- setStateIds[stateId] = value;
- }
- for(let m = 0; m < allGroupMembers[groupIdx].length; m++) {
- checkDeviceGroups(allGroupMembers[groupIdx][m], attribute, value, true);
- }
- } else {
- // this is a device
- let deviceGroups = getState(deviceId + '.groups').val;
- deviceGroups = JSON.parse(deviceGroups);
- for(let i = 0; i < deviceGroups.length; i++) {
- log('Need to check group ' + deviceGroups[i] + ' which I am a member of.', 'debug');
- groupsToCheck.push(deviceGroups[i]);
- }
- }
- for(let i = 0; i < groupsToCheck.length; i++) {
- let group = groupsToCheck[i];
- let groupIdx = 'group_' + group;
- if(groupsChecked.indexOf(groupIdx) !== -1) {
- // no need to check this group again
- continue;
- }
- groupsChecked.push(groupIdx);
- if(!allGroupMembers[groupIdx]) {
- log('Group ' + groupIdx + ' checked has no members. This should not happen.', 'warn');
- } else {
- let sameStates = true;
- let sameStateValue = null;
- let stateValue;
- let curDevice;
- let stateId;
- for(let m = 0; m < allGroupMembers[groupIdx].length; m++) {
- curDevice = allGroupMembers[groupIdx][m];
- stateId = curDevice + '.' + attribute;
- if(stateId in setStateIds) {
- stateValue = setStateIds[stateId];
- } else {
- if(stateId in existingStateIds || existsState(stateId)) {
- stateValue = getState(stateId).val;
- existingStateIds[stateId] = true;
- }
- }
- if(sameStateValue === null || sameStateValue === stateValue) {
- //log('checkDeviceGroups: StateValue ' + stateValue + ' of ' + curDevice + '.' + attribute + ' matches ' + sameStateValue + ' in ' + groupIdx, 'debug');
- sameStateValue = stateValue;
- } else {
- //log('checkDeviceGroups: StateValue ' + stateValue + ' of ' + curDevice + '.' + attribute + ' differs from ' + sameStateValue + ' in ' + groupIdx, 'debug');
- sameStates = false;
- break;
- }
- }
- if(sameStates === true && sameStateValue !== null) {
- setState('zigbee.0.' + groupIdx + '.' + attribute, sameStateValue, true);
- log('checkDeviceGroups: All members of ' + groupIdx + ' have same state. Setting group attribute ' + attribute + ' to ' + sameStateValue);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement