Advertisement
Guest User

Zigbee groups handling

a guest
Feb 25th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let allGroupMembers = null;
  2. let groupsChecked = [];
  3. let setStateIds = {};
  4. let existingStateIds = {};
  5.  
  6. function getGroupMembers() {
  7.     let groupMembers = {};
  8.     let devsWithGroup = $('zigbee.0.*.groups');
  9.     devsWithGroup.each(function(stateid) {
  10.         if(stateid.indexOf('zigbee.0.info.') === 0) {
  11.             return this;
  12.         }
  13.  
  14.         let deviceGroups = getState(stateid).val;
  15.         if(!deviceGroups) {
  16.             return this;
  17.         }
  18.         deviceGroups = JSON.parse(deviceGroups);
  19.         if(!deviceGroups) {
  20.             return this;
  21.         }
  22.         let deviceId = stateid.substr(0, stateid.length - 7);
  23.         let groupIdx;
  24.         for(let i = 0; i < deviceGroups.length; i++) {
  25.             groupIdx = 'group_' + deviceGroups[i];
  26.             if(!groupMembers[groupIdx]) {
  27.                 groupMembers[groupIdx] = [];
  28.             }
  29.             groupMembers[groupIdx].push(deviceId);
  30.         }
  31.     });
  32.  
  33.     return groupMembers;
  34. }
  35.  
  36. function checkDeviceGroups(deviceId, attribute, value, keepMembers) {
  37.     if(!keepMembers) {
  38.         // (re-)read group members
  39.         allGroupMembers = getGroupMembers();
  40.         groupsChecked = [];
  41.         setStateIds = {};
  42.     }
  43.  
  44.     let groupsToCheck = [];
  45.    
  46.     log('checkDeviceGroups(' + deviceId + ', ' + attribute + (keepMembers ? ', true' : '') + ')', 'debug');
  47.  
  48.     if(deviceId.indexOf('zigbee.0.group_') === 0) {
  49.         // this is a group
  50.         let groupIdx = deviceId.replace(/^zigbee\.0\./, '');
  51.        
  52.         // 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
  53.         for(let m = 0; m < allGroupMembers[groupIdx].length; m++) {
  54.             log('Copy state ' + attribute + ' of ' + groupIdx + ' with value ' + value + ' to ' + allGroupMembers[groupIdx][m] + '.' + attribute, 'debug');
  55.             let stateId = allGroupMembers[groupIdx][m] + '.' + attribute;
  56.             setState(stateId, value, true); // not trigger changes again!
  57.             setStateIds[stateId] = value;
  58.         }
  59.  
  60.         for(let m = 0; m < allGroupMembers[groupIdx].length; m++) {
  61.             checkDeviceGroups(allGroupMembers[groupIdx][m], attribute, value, true);
  62.         }
  63.     } else {
  64.         // this is a device
  65.         let deviceGroups = getState(deviceId + '.groups').val;
  66.         deviceGroups = JSON.parse(deviceGroups);
  67.         for(let i = 0; i < deviceGroups.length; i++) {
  68.             log('Need to check group ' + deviceGroups[i] + ' which I am a member of.', 'debug');
  69.             groupsToCheck.push(deviceGroups[i]);
  70.         }
  71.     }
  72.  
  73.     for(let i = 0; i < groupsToCheck.length; i++) {
  74.         let group = groupsToCheck[i];
  75.         let groupIdx = 'group_' + group;
  76.         if(groupsChecked.indexOf(groupIdx) !== -1) {
  77.             // no need to check this group again
  78.            continue;
  79.         }
  80.         groupsChecked.push(groupIdx);
  81.  
  82.         if(!allGroupMembers[groupIdx]) {
  83.             log('Group ' + groupIdx + ' checked has no members. This should not happen.', 'warn');
  84.         } else {
  85.             let sameStates = true;
  86.             let sameStateValue = null;
  87.             let stateValue;
  88.             let curDevice;
  89.             let stateId;
  90.             for(let m = 0; m < allGroupMembers[groupIdx].length; m++) {
  91.                 curDevice = allGroupMembers[groupIdx][m];
  92.                 stateId = curDevice + '.' + attribute;
  93.                 if(stateId in setStateIds) {
  94.                     stateValue = setStateIds[stateId];
  95.                 } else {
  96.                     if(stateId in existingStateIds || existsState(stateId)) {
  97.                         stateValue = getState(stateId).val;
  98.                         existingStateIds[stateId] = true;
  99.                     }
  100.                 }
  101.                 if(sameStateValue === null || sameStateValue === stateValue) {
  102.                     //log('checkDeviceGroups: StateValue ' + stateValue + ' of ' + curDevice + '.' + attribute + ' matches ' + sameStateValue + ' in ' + groupIdx, 'debug');
  103.                     sameStateValue = stateValue;
  104.                 } else {
  105.                     //log('checkDeviceGroups: StateValue ' + stateValue + ' of ' + curDevice + '.' + attribute + ' differs from ' + sameStateValue + ' in ' + groupIdx, 'debug');
  106.                     sameStates = false;
  107.                     break;
  108.                 }
  109.             }
  110.  
  111.             if(sameStates === true && sameStateValue !== null) {
  112.                 setState('zigbee.0.' + groupIdx + '.' + attribute, sameStateValue, true);
  113.                 log('checkDeviceGroups: All members of ' + groupIdx + ' have same state. Setting group attribute ' + attribute + ' to ' + sameStateValue);
  114.             }
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement