Advertisement
Guest User

Zigbee groups handling

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