iobroker.zigbee 1.7.3 → 1.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/.eslintignore +1 -1
  2. package/.eslintrc.json +36 -36
  3. package/.github/FUNDING.yml +3 -3
  4. package/.github/stale.yml +13 -13
  5. package/.github/workflows/test-and-release.yml +151 -151
  6. package/.travis/wiki.sh +27 -27
  7. package/LICENSE +21 -21
  8. package/README.md +375 -375
  9. package/admin/adapter-settings.js +244 -244
  10. package/admin/admin.js +2926 -2926
  11. package/admin/img/MLI-404011-MLI-404049.png +0 -0
  12. package/admin/img/philips_hue_lom001.png +0 -0
  13. package/admin/index.html +159 -159
  14. package/admin/index_m.html +1161 -1161
  15. package/admin/moment.min.js +1 -1
  16. package/admin/shuffle.min.js +2 -2
  17. package/admin/tab_m.html +944 -944
  18. package/admin/vis-network.min.css +1 -1
  19. package/admin/vis-network.min.js +27 -27
  20. package/admin/words.js +112 -112
  21. package/docs/de/readme.md +27 -27
  22. package/docs/en/readme.md +30 -30
  23. package/docs/flashing_via_arduino_(en).md +110 -110
  24. package/docs/ru/readme.md +28 -28
  25. package/docs/tutorial/groups-1.png +0 -0
  26. package/docs/tutorial/groups-2.png +0 -0
  27. package/docs/tutorial/tab-dev-1.png +0 -0
  28. package/io-package.json +354 -354
  29. package/lib/backup.js +171 -171
  30. package/lib/binding.js +325 -325
  31. package/lib/colors.js +460 -460
  32. package/lib/commands.js +501 -501
  33. package/lib/developer.js +148 -148
  34. package/lib/devices.js +3145 -3145
  35. package/lib/exclude.js +168 -168
  36. package/lib/exposes.js +804 -804
  37. package/lib/groups.js +342 -342
  38. package/lib/json.js +60 -60
  39. package/lib/networkmap.js +56 -56
  40. package/lib/ota.js +179 -179
  41. package/lib/rgb.js +255 -255
  42. package/lib/seriallist.js +37 -37
  43. package/lib/states.js +6416 -6416
  44. package/lib/statescontroller.js +666 -666
  45. package/lib/tools.js +54 -54
  46. package/lib/utils.js +151 -151
  47. package/lib/zbBaseExtension.js +36 -36
  48. package/lib/zbDelayedAction.js +152 -152
  49. package/lib/zbDeviceAvailability.js +315 -315
  50. package/lib/zbDeviceConfigure.js +152 -152
  51. package/lib/zbDeviceEvent.js +49 -49
  52. package/lib/zigbeecontroller.js +946 -946
  53. package/package.json +1 -1
  54. package/support/docgen.js +93 -93
  55. package/admin/img/zbt_remote.png +0 -0
package/lib/groups.js CHANGED
@@ -1,342 +1,342 @@
1
- 'use strict';
2
-
3
- const statesMapping = require('./devices');
4
-
5
-
6
- class Groups {
7
- constructor(adapter) {
8
- this.adapter = adapter;
9
- this.adapter.on('message', this.onMessage.bind(this));
10
- }
11
-
12
- start(zbController, stController) {
13
- this.zbController = zbController;
14
- this.stController = stController;
15
- this.adapter.getStateAsync('info.groups')
16
- .then((groupsState) => {
17
- const groups = (groupsState && groupsState.val) ? JSON.parse(groupsState.val) : {};
18
- for (const gid in groups) {
19
- stController.storeDeviceName(`group_${gid}`, groups[gid]);
20
- }
21
- // this.Adapter.deleteState('info.groups');
22
- this.syncGroups();
23
- });
24
- }
25
-
26
- stop() {
27
- delete this.zbController;
28
- delete this.stController;
29
- }
30
-
31
- info(msg) {
32
- this.adapter.log.info(msg);
33
- }
34
-
35
- error(msg) {
36
- this.adapter.log.error(msg);
37
- }
38
-
39
- debug(msg) {
40
- this.adapter.log.debug(msg);
41
- }
42
-
43
- warn(msg) {
44
- this.adapter.log.warn(msg);
45
- }
46
-
47
- /**
48
- * @param {ioBroker.Message} obj
49
- */
50
- onMessage(obj) {
51
- if (typeof obj === 'object' && obj.command) {
52
- switch (obj.command) {
53
- case 'getGroups':
54
- this.getGroups(obj);
55
- break;
56
- case 'renameGroup':
57
- // used for renaming AND creating groups
58
- this.renameGroup(obj.from, obj.command, obj.message, obj.callback);
59
- break;
60
- case 'deleteGroup':
61
- this.deleteGroup(obj.from, obj.command, obj.message, obj.callback);
62
- break;
63
- case 'updateGroupMembership':
64
- this.updateGroupMembership(obj.from, obj.command, obj.message, obj.callback);
65
- break;
66
- }
67
- }
68
- }
69
-
70
- async getGroupMembersFromController(id) {
71
- const members = [];
72
- try {
73
- const group = await this.zbController.getGroupByID(id);
74
- if (group) {
75
- const groupmembers = group.members;
76
-
77
- for (const member of groupmembers) {
78
- const nwk = member.deviceNetworkAddress;
79
- const device = this.zbController.getDeviceByNetworkAddress(nwk);
80
- if (device && device.ieeeAddr) members.push( { device:device.ieeeAddr } );
81
- }
82
- }
83
- else {
84
- return undefined;
85
- }
86
-
87
- } catch (error) {
88
- if (error) this.error('getGroupMembersFromController: error is ' + JSON.stringify(error) + ' ' + JSON.stringify(new Error().stack));
89
- else this.error('unidentifed error in getGroupMembersFromController');
90
- }
91
- return members;
92
- }
93
-
94
- async getGroups(obj) {
95
- const response = { groups: {} };
96
- try {
97
- // const groupsState = await this.adapter.getStateAsync('info.groups');
98
- const herdsmanGroups = await this.zbController.getGroups();
99
-
100
- // const groups = (groupsState && groupsState.val) ? JSON.parse(groupsState.val) : {};
101
-
102
- const groups = {};
103
- if (typeof herdsmanGroups === 'object') {
104
- for (const group of herdsmanGroups) {
105
- const gid = group.groupID;
106
- if (gid) {
107
- groups[gid]=this.stController.verifyDeviceName(`group_${gid}`,`Group ${gid}`);
108
- }
109
- }
110
- }
111
- this.debug('getGroups result: ' + JSON.stringify(groups));
112
- response.groups = groups;
113
- }
114
- catch (error) {
115
- response.error = `getGroups: caught error: ${error}`;
116
- this.error(`getGroups: caught error: ${error}`);
117
- }
118
- finally {
119
- if (obj)
120
- this.adapter.sendTo(obj.from, obj.command, response, obj.callback);
121
-
122
- }
123
- return response.groups;
124
- }
125
-
126
- async updateGroupMembership(from, command, message, callback) {
127
- try {
128
- const groups = (message && message.groups ? message.groups : {});
129
- const devId = (message && message.id ? message.id : undefined);
130
- if (devId === undefined) {
131
- this.adapter.sendTo(from, command, {error: 'No device specified'}, callback);
132
- }
133
- const sysid = devId.replace(this.adapter.namespace + '.', '0x');
134
- // Keeping this for reference. State update or state removal needs to be decided upon
135
- //const id = `${devId}.groups`;
136
- // this.adapter.setState(id, JSON.stringify(groups), true);
137
-
138
- //const current = await this.zbController.getGroupMembersFromController(sysid);
139
- const errors = []
140
- for (const epid in groups) {
141
- for (const gpid of groups[epid]) {
142
- const gpidn = parseInt(gpid);
143
- if (gpidn < 0) {
144
- let response = await this.zbController.removeDevFromGroup(sysid,(-gpidn), epid);
145
- if (response && response.error) {
146
- errors.push(response.error);
147
- this.error("remove dev from group Error: "+ JSON.stringify(response.error));
148
- }
149
- }
150
- else if (gpidn > 0){
151
- let response = await this.zbController.addDevToGroup(sysid,(gpidn), epid);
152
- if (response && response.error) {
153
- errors.push(response.error);
154
- this.error("add dev to group Error: "+ JSON.stringify(response.error));
155
- }
156
-
157
- }
158
- else this.warn('illegal group id 0');
159
- }
160
- }
161
- } catch (e) {
162
- this.adapter.sendTo(from, command, {error: e}, callback);
163
- return;
164
- }
165
- this.adapter.sendTo(from, command, { }, callback);
166
- }
167
-
168
-
169
- async queryGroupMemberState(groupID, stateDesc) {
170
- const members = await this.getGroupMembersFromController(groupID);
171
- const result = {
172
- unsupported: [],
173
- unread: []
174
- };
175
- for (const member of members) {
176
- const entity = await this.zbController.resolveEntity(member.device);
177
- if (!entity) return false;
178
- this.debug(`entity: ${JSON.stringify(entity)}`);
179
- const mappedModel = entity.mapped;
180
- this.debug('Mapped Model: ' + JSON.stringify(mappedModel));
181
- const converter = mappedModel.toZigbee.find((c) => c && (c.key.includes(stateDesc.prop) || c.key.includes(stateDesc.setattr) || c.key.includes(stateDesc.id)));
182
- if (!converter) {
183
- result.unsupported.push(member.device);
184
- continue;
185
- }
186
- if (converter.hasOwnProperty('convertGet')) {
187
- try {
188
- await converter.convertGet(entity.device.endpoints[0], stateDesc.id, {});
189
- } catch (error) {
190
- result.unread.push(member.device);
191
- }
192
- }
193
- }
194
- if (result.unsupported.length>0) {
195
- const error = {
196
- code: 134,
197
- message: `unsupported ${stateDesc.id} change for group members ${result.unsupported.join()}`
198
- };
199
- throw error;
200
- }
201
- if (result.unread.length>0) {
202
- this.warn('unread ' + stateDesc.id + ' change for group members ' + JSON.stringify(result.unread));
203
- }
204
- }
205
-
206
- async deleteGroup(from, command, message) {
207
- /*
208
- const members = await this.getGroupMembersFromController(parseInt(message));
209
- if (members && members.length) {
210
- for (const member of members) {
211
- const devName = member.device.substring(2);
212
- const groupEntry = this.adapter.getStateAsync(`${devName}.groups`);
213
- const memberarray = (groupEntry && groupEntry.val) ? JSON.parse(groupEntry.val) : [];
214
- const index = memberarray.indexOf(message.toString());
215
- if (index > -1) {
216
- memberarray.splice(index, 1);
217
- }
218
- if (memberarray.length > 0) {
219
- await this.adapter.setStateAsync(`${devName}.groups`, JSON.stringify(memberarray), true);
220
- }
221
- else {
222
- await this.adapter.setStateAsync(`${devName}.groups`, '', true);
223
- }
224
- }
225
- }
226
- const groupsEntry = await this.adapter.getStateAsync('info.groups');
227
- const objGroups = (groupsEntry && groupsEntry.val ? JSON.parse(groupsEntry.val) : {});
228
- delete objGroups[message.toString()];
229
- await this.adapter.setStateAsync('info.groups', JSON.stringify(objGroups), true);
230
- */
231
- await this.zbController.removeGroupById(message);
232
- await this.stController.deleteDeviceStatesAsync(`group_${parseInt(message)}`);
233
- }
234
-
235
- async renameGroup(from, command, message) {
236
-
237
- // const groupsEntry = await this.adapter.getStateAsync('info.groups');
238
- // const objGroups = (groupsEntry && groupsEntry.val ? JSON.parse(groupsEntry.val) : {});
239
- const name = message.name;
240
- const id = `group_${message.id}`;
241
- this.stController.storeDeviceName(id, name);
242
- try {
243
- await this.zbController.verifyGroupExists(message.id);
244
- }
245
- catch (e)
246
- {
247
- if (e && e.hasOwnProperty('code')) this.warn('renameGroup caught error ' + JSON.stringify(e.code));
248
- }
249
- // objGroups[message.id.toString()] = message.name;
250
- // await this.adapter.setStateAsync('info.groups', JSON.stringify(objGroups), true);
251
-
252
- const group = await this.adapter.getStateAsync(id);
253
- if (!group) {
254
- // assume we have to create the group
255
- this.adapter.setObjectNotExists(id, {
256
- type: 'device',
257
- common: {name: name, type: 'group'},
258
- native: {id: id}
259
- }, () => {
260
- this.adapter.extendObject(id , {common: {name: name, type: 'group'}});
261
- // create writable states for groups from their devices
262
- for (const stateInd in statesMapping.groupStates) {
263
- if (!statesMapping.groupStates.hasOwnProperty(stateInd)) continue;
264
- const statedesc = statesMapping.groupStates[stateInd];
265
- const common = {
266
- name: statedesc.name,
267
- type: statedesc.type,
268
- unit: statedesc.unit,
269
- read: statedesc.read,
270
- write: statedesc.write,
271
- icon: statedesc.icon,
272
- role: statedesc.role,
273
- min: statedesc.min,
274
- max: statedesc.max,
275
- };
276
- this.stController.updateState(id, statedesc.id, undefined, common);
277
- }
278
- });
279
- }
280
- }
281
-
282
-
283
- async syncGroups() {
284
- const groups = await this.getGroups();
285
- const chain = [];
286
- const usedGroupsIds = [];
287
- for (const j in groups) {
288
- if (groups.hasOwnProperty(j)) {
289
- const id = `group_${j}`,
290
- name = groups[j];
291
- chain.push(new Promise((resolve) => {
292
- this.adapter.setObjectNotExists(id, {
293
- type: 'device',
294
- common: {name: name, type: 'group'},
295
- native: {id: j}
296
- }, () => {
297
- this.adapter.extendObject(id, {common: {type: 'group'}});
298
- // create writable states for groups from their devices
299
- for (const stateInd in statesMapping.groupStates) {
300
- if (!statesMapping.groupStates.hasOwnProperty(stateInd)) continue;
301
- const statedesc = statesMapping.groupStates[stateInd];
302
- const common = {
303
- name: statedesc.name,
304
- type: statedesc.type,
305
- unit: statedesc.unit,
306
- read: statedesc.read,
307
- write: statedesc.write,
308
- icon: statedesc.icon,
309
- role: statedesc.role,
310
- min: statedesc.min,
311
- max: statedesc.max,
312
- };
313
- this.stController.updateState(id, statedesc.id, undefined, common);
314
- }
315
- resolve();
316
- });
317
- }));
318
- usedGroupsIds.push(parseInt(j));
319
- }
320
- }
321
- chain.push(new Promise((resolve) => {
322
- // remove unused adpter groups
323
- this.adapter.getDevices((err, devices) => {
324
- if (!err) {
325
- devices.forEach((dev) => {
326
- if (dev.common.type === 'group') {
327
- const groupid = parseInt(dev.native.id);
328
- if (!usedGroupsIds.includes(groupid)) {
329
- this.stController.deleteDeviceStates(`group_${groupid}`);
330
- }
331
- }
332
- });
333
- }
334
- resolve();
335
- });
336
- }));
337
- Promise.all(chain);
338
- }
339
-
340
- }
341
-
342
- module.exports = Groups;
1
+ 'use strict';
2
+
3
+ const statesMapping = require('./devices');
4
+
5
+
6
+ class Groups {
7
+ constructor(adapter) {
8
+ this.adapter = adapter;
9
+ this.adapter.on('message', this.onMessage.bind(this));
10
+ }
11
+
12
+ start(zbController, stController) {
13
+ this.zbController = zbController;
14
+ this.stController = stController;
15
+ this.adapter.getStateAsync('info.groups')
16
+ .then((groupsState) => {
17
+ const groups = (groupsState && groupsState.val) ? JSON.parse(groupsState.val) : {};
18
+ for (const gid in groups) {
19
+ stController.storeDeviceName(`group_${gid}`, groups[gid]);
20
+ }
21
+ // this.Adapter.deleteState('info.groups');
22
+ this.syncGroups();
23
+ });
24
+ }
25
+
26
+ stop() {
27
+ delete this.zbController;
28
+ delete this.stController;
29
+ }
30
+
31
+ info(msg) {
32
+ this.adapter.log.info(msg);
33
+ }
34
+
35
+ error(msg) {
36
+ this.adapter.log.error(msg);
37
+ }
38
+
39
+ debug(msg) {
40
+ this.adapter.log.debug(msg);
41
+ }
42
+
43
+ warn(msg) {
44
+ this.adapter.log.warn(msg);
45
+ }
46
+
47
+ /**
48
+ * @param {ioBroker.Message} obj
49
+ */
50
+ onMessage(obj) {
51
+ if (typeof obj === 'object' && obj.command) {
52
+ switch (obj.command) {
53
+ case 'getGroups':
54
+ this.getGroups(obj);
55
+ break;
56
+ case 'renameGroup':
57
+ // used for renaming AND creating groups
58
+ this.renameGroup(obj.from, obj.command, obj.message, obj.callback);
59
+ break;
60
+ case 'deleteGroup':
61
+ this.deleteGroup(obj.from, obj.command, obj.message, obj.callback);
62
+ break;
63
+ case 'updateGroupMembership':
64
+ this.updateGroupMembership(obj.from, obj.command, obj.message, obj.callback);
65
+ break;
66
+ }
67
+ }
68
+ }
69
+
70
+ async getGroupMembersFromController(id) {
71
+ const members = [];
72
+ try {
73
+ const group = await this.zbController.getGroupByID(id);
74
+ if (group) {
75
+ const groupmembers = group.members;
76
+
77
+ for (const member of groupmembers) {
78
+ const nwk = member.deviceNetworkAddress;
79
+ const device = this.zbController.getDeviceByNetworkAddress(nwk);
80
+ if (device && device.ieeeAddr) members.push( { device:device.ieeeAddr } );
81
+ }
82
+ }
83
+ else {
84
+ return undefined;
85
+ }
86
+
87
+ } catch (error) {
88
+ if (error) this.error('getGroupMembersFromController: error is ' + JSON.stringify(error) + ' ' + JSON.stringify(new Error().stack));
89
+ else this.error('unidentifed error in getGroupMembersFromController');
90
+ }
91
+ return members;
92
+ }
93
+
94
+ async getGroups(obj) {
95
+ const response = { groups: {} };
96
+ try {
97
+ // const groupsState = await this.adapter.getStateAsync('info.groups');
98
+ const herdsmanGroups = await this.zbController.getGroups();
99
+
100
+ // const groups = (groupsState && groupsState.val) ? JSON.parse(groupsState.val) : {};
101
+
102
+ const groups = {};
103
+ if (typeof herdsmanGroups === 'object') {
104
+ for (const group of herdsmanGroups) {
105
+ const gid = group.groupID;
106
+ if (gid) {
107
+ groups[gid]=this.stController.verifyDeviceName(`group_${gid}`,`Group ${gid}`);
108
+ }
109
+ }
110
+ }
111
+ this.debug('getGroups result: ' + JSON.stringify(groups));
112
+ response.groups = groups;
113
+ }
114
+ catch (error) {
115
+ response.error = `getGroups: caught error: ${error}`;
116
+ this.error(`getGroups: caught error: ${error}`);
117
+ }
118
+ finally {
119
+ if (obj)
120
+ this.adapter.sendTo(obj.from, obj.command, response, obj.callback);
121
+
122
+ }
123
+ return response.groups;
124
+ }
125
+
126
+ async updateGroupMembership(from, command, message, callback) {
127
+ try {
128
+ const groups = (message && message.groups ? message.groups : {});
129
+ const devId = (message && message.id ? message.id : undefined);
130
+ if (devId === undefined) {
131
+ this.adapter.sendTo(from, command, {error: 'No device specified'}, callback);
132
+ }
133
+ const sysid = devId.replace(this.adapter.namespace + '.', '0x');
134
+ // Keeping this for reference. State update or state removal needs to be decided upon
135
+ //const id = `${devId}.groups`;
136
+ // this.adapter.setState(id, JSON.stringify(groups), true);
137
+
138
+ //const current = await this.zbController.getGroupMembersFromController(sysid);
139
+ const errors = []
140
+ for (const epid in groups) {
141
+ for (const gpid of groups[epid]) {
142
+ const gpidn = parseInt(gpid);
143
+ if (gpidn < 0) {
144
+ let response = await this.zbController.removeDevFromGroup(sysid,(-gpidn), epid);
145
+ if (response && response.error) {
146
+ errors.push(response.error);
147
+ this.error("remove dev from group Error: "+ JSON.stringify(response.error));
148
+ }
149
+ }
150
+ else if (gpidn > 0){
151
+ let response = await this.zbController.addDevToGroup(sysid,(gpidn), epid);
152
+ if (response && response.error) {
153
+ errors.push(response.error);
154
+ this.error("add dev to group Error: "+ JSON.stringify(response.error));
155
+ }
156
+
157
+ }
158
+ else this.warn('illegal group id 0');
159
+ }
160
+ }
161
+ } catch (e) {
162
+ this.adapter.sendTo(from, command, {error: e}, callback);
163
+ return;
164
+ }
165
+ this.adapter.sendTo(from, command, { }, callback);
166
+ }
167
+
168
+
169
+ async queryGroupMemberState(groupID, stateDesc) {
170
+ const members = await this.getGroupMembersFromController(groupID);
171
+ const result = {
172
+ unsupported: [],
173
+ unread: []
174
+ };
175
+ for (const member of members) {
176
+ const entity = await this.zbController.resolveEntity(member.device);
177
+ if (!entity) return false;
178
+ this.debug(`entity: ${JSON.stringify(entity)}`);
179
+ const mappedModel = entity.mapped;
180
+ this.debug('Mapped Model: ' + JSON.stringify(mappedModel));
181
+ const converter = mappedModel.toZigbee.find((c) => c && (c.key.includes(stateDesc.prop) || c.key.includes(stateDesc.setattr) || c.key.includes(stateDesc.id)));
182
+ if (!converter) {
183
+ result.unsupported.push(member.device);
184
+ continue;
185
+ }
186
+ if (converter.hasOwnProperty('convertGet')) {
187
+ try {
188
+ await converter.convertGet(entity.device.endpoints[0], stateDesc.id, {});
189
+ } catch (error) {
190
+ result.unread.push(member.device);
191
+ }
192
+ }
193
+ }
194
+ if (result.unsupported.length>0) {
195
+ const error = {
196
+ code: 134,
197
+ message: `unsupported ${stateDesc.id} change for group members ${result.unsupported.join()}`
198
+ };
199
+ throw error;
200
+ }
201
+ if (result.unread.length>0) {
202
+ this.warn('unread ' + stateDesc.id + ' change for group members ' + JSON.stringify(result.unread));
203
+ }
204
+ }
205
+
206
+ async deleteGroup(from, command, message) {
207
+ /*
208
+ const members = await this.getGroupMembersFromController(parseInt(message));
209
+ if (members && members.length) {
210
+ for (const member of members) {
211
+ const devName = member.device.substring(2);
212
+ const groupEntry = this.adapter.getStateAsync(`${devName}.groups`);
213
+ const memberarray = (groupEntry && groupEntry.val) ? JSON.parse(groupEntry.val) : [];
214
+ const index = memberarray.indexOf(message.toString());
215
+ if (index > -1) {
216
+ memberarray.splice(index, 1);
217
+ }
218
+ if (memberarray.length > 0) {
219
+ await this.adapter.setStateAsync(`${devName}.groups`, JSON.stringify(memberarray), true);
220
+ }
221
+ else {
222
+ await this.adapter.setStateAsync(`${devName}.groups`, '', true);
223
+ }
224
+ }
225
+ }
226
+ const groupsEntry = await this.adapter.getStateAsync('info.groups');
227
+ const objGroups = (groupsEntry && groupsEntry.val ? JSON.parse(groupsEntry.val) : {});
228
+ delete objGroups[message.toString()];
229
+ await this.adapter.setStateAsync('info.groups', JSON.stringify(objGroups), true);
230
+ */
231
+ await this.zbController.removeGroupById(message);
232
+ await this.stController.deleteDeviceStatesAsync(`group_${parseInt(message)}`);
233
+ }
234
+
235
+ async renameGroup(from, command, message) {
236
+
237
+ // const groupsEntry = await this.adapter.getStateAsync('info.groups');
238
+ // const objGroups = (groupsEntry && groupsEntry.val ? JSON.parse(groupsEntry.val) : {});
239
+ const name = message.name;
240
+ const id = `group_${message.id}`;
241
+ this.stController.storeDeviceName(id, name);
242
+ try {
243
+ await this.zbController.verifyGroupExists(message.id);
244
+ }
245
+ catch (e)
246
+ {
247
+ if (e && e.hasOwnProperty('code')) this.warn('renameGroup caught error ' + JSON.stringify(e.code));
248
+ }
249
+ // objGroups[message.id.toString()] = message.name;
250
+ // await this.adapter.setStateAsync('info.groups', JSON.stringify(objGroups), true);
251
+
252
+ const group = await this.adapter.getStateAsync(id);
253
+ if (!group) {
254
+ // assume we have to create the group
255
+ this.adapter.setObjectNotExists(id, {
256
+ type: 'device',
257
+ common: {name: name, type: 'group'},
258
+ native: {id: id}
259
+ }, () => {
260
+ this.adapter.extendObject(id , {common: {name: name, type: 'group'}});
261
+ // create writable states for groups from their devices
262
+ for (const stateInd in statesMapping.groupStates) {
263
+ if (!statesMapping.groupStates.hasOwnProperty(stateInd)) continue;
264
+ const statedesc = statesMapping.groupStates[stateInd];
265
+ const common = {
266
+ name: statedesc.name,
267
+ type: statedesc.type,
268
+ unit: statedesc.unit,
269
+ read: statedesc.read,
270
+ write: statedesc.write,
271
+ icon: statedesc.icon,
272
+ role: statedesc.role,
273
+ min: statedesc.min,
274
+ max: statedesc.max,
275
+ };
276
+ this.stController.updateState(id, statedesc.id, undefined, common);
277
+ }
278
+ });
279
+ }
280
+ }
281
+
282
+
283
+ async syncGroups() {
284
+ const groups = await this.getGroups();
285
+ const chain = [];
286
+ const usedGroupsIds = [];
287
+ for (const j in groups) {
288
+ if (groups.hasOwnProperty(j)) {
289
+ const id = `group_${j}`,
290
+ name = groups[j];
291
+ chain.push(new Promise((resolve) => {
292
+ this.adapter.setObjectNotExists(id, {
293
+ type: 'device',
294
+ common: {name: name, type: 'group'},
295
+ native: {id: j}
296
+ }, () => {
297
+ this.adapter.extendObject(id, {common: {type: 'group'}});
298
+ // create writable states for groups from their devices
299
+ for (const stateInd in statesMapping.groupStates) {
300
+ if (!statesMapping.groupStates.hasOwnProperty(stateInd)) continue;
301
+ const statedesc = statesMapping.groupStates[stateInd];
302
+ const common = {
303
+ name: statedesc.name,
304
+ type: statedesc.type,
305
+ unit: statedesc.unit,
306
+ read: statedesc.read,
307
+ write: statedesc.write,
308
+ icon: statedesc.icon,
309
+ role: statedesc.role,
310
+ min: statedesc.min,
311
+ max: statedesc.max,
312
+ };
313
+ this.stController.updateState(id, statedesc.id, undefined, common);
314
+ }
315
+ resolve();
316
+ });
317
+ }));
318
+ usedGroupsIds.push(parseInt(j));
319
+ }
320
+ }
321
+ chain.push(new Promise((resolve) => {
322
+ // remove unused adpter groups
323
+ this.adapter.getDevices((err, devices) => {
324
+ if (!err) {
325
+ devices.forEach((dev) => {
326
+ if (dev.common.type === 'group') {
327
+ const groupid = parseInt(dev.native.id);
328
+ if (!usedGroupsIds.includes(groupid)) {
329
+ this.stController.deleteDeviceStates(`group_${groupid}`);
330
+ }
331
+ }
332
+ });
333
+ }
334
+ resolve();
335
+ });
336
+ }));
337
+ Promise.all(chain);
338
+ }
339
+
340
+ }
341
+
342
+ module.exports = Groups;