Groups.js
6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
'use strict';
const Bottleneck = require('bottleneck')
, groupsApi = require('./http/endpoints/groups')
, ApiDefinition = require('./http/ApiDefinition.js')
, model = require('@peter-murray/hue-bridge-model').model
, Room = model.Room
, LightGroup = model.LightGroup
, Zone = model.Zone
, util = require('../util')
;
/**
* @typedef {import('@peter-murray/hue-bridge-model').model.Entertainment} Entertainment
* @typedef {import('@peter-murray/hue-bridge-model').model.Group} Group
* @typedef {import('@peter-murray/hue-bridge-model').model.LightGroup} LightGroup
* @typedef {import('@peter-murray/hue-bridge-model').model.Lightsource} Lightsource
* @typedef {import('@peter-murray/hue-bridge-model').model.Luminaire} Luminaire
* @typedef {import('@peter-murray/hue-bridge-model').model.Room} Room
* @typedef {import('@peter-murray/hue-bridge-model').model.Zone} Zone
*/
module.exports = class Groups extends ApiDefinition {
constructor(hueApi) {
super(hueApi);
// Set up a limiter on the group state changes from the library to once per second as per guidance documentation
this._groupStateLimiter = new Bottleneck({maxConcurrent: 1, minTime: 1000});
}
/**
* Gets all the groups from the bridge.
* @returns {Promise<Array<Entertainment|LightGroup|Lightsource|Luminaire|Room|Zone>>}
*/
getAll() {
// Lightset 0 (all lights) is a special case, so retrieve the bridge's definition of that and prepend to the
// existing group definitions to provide the complete list of groups.
return Promise.all([
this.execute(groupsApi.getGroupAttributes, {id: 0}),
this.execute(groupsApi.getAllGroups)
]).then(results => {
results[1].unshift(results[0]);
return results[1];
});
}
/**
* @param id {int | Group}
* @returns {Promise<Entertainment|LightGroup|Lightsource|Luminaire|Room|Zone>}
*/
getGroup(id) {
return this.execute(groupsApi.getGroupAttributes, {id: id});
}
/**
* @param id {int | Group}
* @returns {Promise<Array<Entertainment|LightGroup|Lightsource|Luminaire|Room|Zone>>}
*/
get(id) {
util.deprecatedFunction('5.x', 'groups.get(id)', 'Use groups.getGroup(id) instead.');
return this.getGroup(id);
}
/**
* @deprecated Use getGroupByName(name) instead.
* @param name {string}
* @returns {Promise<Entertainment|LightGroup|Lightsource|Luminaire|Room|Zone>}
*/
getByName(name) {
util.deprecatedFunction('5.x', 'groups.getByName(name)', 'Use groups.getGroupByName(name) instead.');
return this.getGroupByName(name);
}
/**
* @param name {string}
* @returns {Promise<Array<Entertainment|LightGroup|Lightsource|Luminaire|Room|Zone>>}
*/
getGroupByName(name) {
return this.getAll()
.then(allGroups => {
return allGroups.filter(group => group.name === name);
});
}
/**
* Creates a group
* @param group {Entertainment | LightGroup | Room | Zone | Group}
* @returns {Promise<Group>} The Group that was created on the bridge.
*/
createGroup(group) {
const self = this;
if (arguments.length === 1 && group instanceof model.Group) {
return this.execute(groupsApi.createGroup, {group: group})
.then(result => {
return self.getGroup(result.id);
});
}
util.deprecatedFunction('5.x', 'groups.createGroup(name, lights)', 'Use groups.createGroup(group) instead.');
const newGroup = new LightGroup();
newGroup.name = arguments[0];
newGroup.lights = arguments[1];
return self.createGroup(newGroup);
}
/**
* @deprecated use createGroup(group) instead
*/
createRoom(name, lights, roomClass) {
util.deprecatedFunction('5.x', 'groups.createRoom(name, lights, roomClass)', 'Use groups.createGroup(group) instead.');
const group = new Room();
group.name = name;
group.lights = lights;
group.class = roomClass;
return this.createGroup(group);
}
/**
* @deprecated use createGroup(group) instead
*/
createZone(name, lights, roomClass) {
util.deprecatedFunction('5.x', 'groups.createZone(name, lights, roomClass)', 'Use groups.createGroup(group) instead.');
const group = new Zone();
group.name = name;
group.lights = lights;
group.class = roomClass;
return this.createGroup(group);
}
/**
* Update the Group attributes on the bridge for the specified Group object.
* @param group {Group} The group with updates to be updated on the bridge.
* @returns {Promise<boolean>}
*/
updateGroupAttributes(group) {
return this.execute(groupsApi.setGroupAttributes, {id: group.id, group: group});
}
/**
* Update the Group attributes on the bridge for the specified Group object.
* @param group {Group} The group with updates to be updated on the bridge.
* @returns {Promise<boolean>}
*/
updateAttributes(id, data) {
util.deprecatedFunction('5.x', 'groups.updateAttributes(id, data)', 'Use groups.updateGroupAttributes(group) instead.');
return this.execute(groupsApi.setGroupAttributes, {id: id, group: data});
}
/**
*
* @param id {number | Group | LightGroup | Zone | Room | Entertainment} The id or Group instance to delete
* @returns {Promise<boolean>}
*/
deleteGroup(id) {
return this.execute(groupsApi.deleteGroup, {id: id});
}
/**
* @param id {int | Group}
* @returns {Promise<Object>}
*/
getGroupState(id) {
return this.get(id).then(group => {
return group.state;
});
}
/**
*
* @param id {int | Group}
* @param state {GroupLightState | Object}
* @returns {Promise<*>>}
*/
setGroupState(id, state) {
const self = this;
return self._groupStateLimiter.schedule(() => {
return self.execute(groupsApi.setGroupState, {id: id, state: state});
});
}
/**
* @returns {Promise<LightGroup[]>}
*/
getLightGroups() {
return this._getByType('LightGroup');
}
/**
* @returns {Promise<Luminaire[]>}
*/
getLuminaries() {
return this._getByType('Luminaire');
}
/**
* @returns {Promise<Lightsource[]>}
*/
getLightSources() {
return this._getByType('Lightsource');
}
/**
* @returns {Promise<Room[]>}
*/
getRooms() {
return this._getByType('Room');
}
/**
* @returns {Promise<Zone[]>}
*/
getZones() {
return this._getByType('Zone');
}
/**
* @returns {Promise<Entertainment[]>}
*/
getEntertainment() {
return this._getByType('Entertainment');
}
/**
* Enables the streaming functionality on an Entertainment Group
* @param id {int | Entertainment}
* @returns {Promise<boolean>}
*/
enableStreaming(id) {
return this.execute(groupsApi.setStreaming, {id: id, active: true});
}
/**
* Disabled the streaming functionality on an Entertainment Group
* @param id {int | Entertainment}
* @returns {Promise<boolean>}
*/
disableStreaming(id) {
return this.execute(groupsApi.setStreaming, {id: id, active: false});
}
_getByType(type) {
return this.getAll()
.then(groups => {
return groups.filter(group => group.type === type);
});
}
};