-
Notifications
You must be signed in to change notification settings - Fork 145
/
Lights.js
172 lines (151 loc) · 4.93 KB
/
Lights.js
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
'use strict';
const Bottleneck = require('bottleneck')
, lightsApi = require('./http/endpoints/lights')
, ApiDefinition = require('./http/ApiDefinition.js')
, ApiError = require('../ApiError')
, util = require('../util')
, model = require('../model')
;
/**
* @type {Lights}
*/
module.exports = class Lights extends ApiDefinition {
constructor(hueApi) {
super(hueApi);
// As per Bridge documentation guidance, limit the number of calls to the light state changes to 10 per second max
this._lightStateLimiter = new Bottleneck({maxConcurrent: 1, minTime: 60});
}
/**
* Gets all the Lights from the Bridge
* @returns {Promise<Array<import('../model/Light')>>}
*/
getAll() {
return this.execute(lightsApi.getAllLights);
}
/**
* Get a specific Light from the Bridge.
* @param id {number | Light} The id or Light instance to get from the Bridge.
* @returns {Promise<Light>}
*/
getLight(id) {
const lightId = id.id || id;
return this.getAll().then(lights => {
const found = lights.filter(light => light.id === lightId);
if (found.length === 0) {
throw new ApiError(`Light ${lightId} not found`);
}
return found[0]
});
}
/**
* @deprecated since 4.0. Use getLight(id) instead.
* @param id {number} The ide of the light to get.
* @returns {Promise<Light>}
*/
getLightById(id) {
util.deprecatedFunction('5.x', 'lights.getLightById(id)', 'Use lights.getLight(id) instead.');
return this.getLight(id);
}
/**
* Retrieves a Light from the Bridge by name.
* @param name {string} The name of the light to get.
* @returns {Promise<Array<Light>>}
*/
getLightByName(name) {
return this.getAll().then(lights => {
return lights.find(light => {
return light.name === name;
});
});
}
/**
* Discovers the "new" lights detected by the Bridge.
* @returns {Promise<Array<Light>>}
*/
getNew() {
return this.execute(lightsApi.getNewLights);
}
/**
* Starts a search for "new"/undiscovered Lights by the bridge. This can take up to 30 seconds to complete.
* @returns {Promise<boolean>}
*/
searchForNew() {
return this.execute(lightsApi.searchForNewLights);
}
/**
* Obtains the current Attributes and State settings for the specified Light.
* @param id {number | Light} The id or Light instance to get the attributes and state for.
* @returns {Promise<Object>}
*/
getLightAttributesAndState(id) {
return this.execute(lightsApi.getLightAttributesAndState, {id: id});
}
/**
* Obtains the current State settings for the specified Light.
* @param id {number | Light} The id or Light instance to get the current state for.
* @returns {Promise<Object>}
*/
getLightState(id) {
return this.getLightAttributesAndState(id).then(result => { return result.state; });
}
/**
* Sets the current state for the Light to desired settings.
* @param id {number | Light} The id or Light instance to set the state on.
* @param state {Object | LightState} The LightState to set on the light.
* @returns {PromiseLike<any> | Promise<any>}
*/
setLightState(id, state) {
let lightId = id;
if (model.isLightInstance(id)) {
lightId = id.id;
}
return this.hueApi.getLightDefinition(lightId)
.then(device => {
if (!device) {
throw new ApiError(`Light with id:${lightId} was not found on this bridge`);
}
return this._setLightState(id, state, device);
});
}
/**
* Renames a Light on the Bridge to the specified name in the Light instance.
* @param light {Light} The Light to rename with the new name set.
* @returns {Promise<Boolean>}
*/
renameLight(light) {
if (model.isLightInstance(light)) {
return this.execute(lightsApi.setLightAttributes, {id: light, light: light});
} else {
throw new ApiError('Light parameter is not an instance of a light');
}
}
/**
* @deprecated since 4.x, use renameLight(light) instead
* @param id {int} The Light to rename.
* @param name {string} The new name.
* @returns {Promise}
*/
rename(id, name) {
if (arguments.length === 1) {
util.deprecatedFunction('5.x', 'lights.rename(id, name)', 'Use lights.renameLight(light) instead.');
return this.renameLight(id);
} else {
util.deprecatedFunction('5.x', 'lights.rename(id, name)', 'Use lights.renameLight(light) instead.');
return this.execute(lightsApi.setLightAttributes, {id: id, name: name});
}
}
/**
* Deletes a Light from the Hue Bridge
* @param id { number | Light} The id or Light instance to be deleted
* @returns {Promise<boolean>}
*/
deleteLight(id) {
return this.execute(lightsApi.deleteLight, {id: id});
}
_setLightState(id, state, device) {
const self = this;
return this._lightStateLimiter.schedule(() => {
return self.execute(lightsApi.setLightState, {id: id, state: state, device: device});
});
}
};