Scenes.js
3.52 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
'use strict';
const scenesApi = require('./http/endpoints/scenes')
, ApiDefinition = require('./http/ApiDefinition')
, model = require('@peter-murray/hue-bridge-model').model
, instanceChecks = model.instanceChecks
, GroupState = model.lightStates.GroupLightState
, util = require('../util')
;
/**
* @typedef {import('@peter-murray/hue-bridge-model').model.LightScene} LightScene
* @typedef {import('@peter-murray/hue-bridge-model').model.GroupScene} GroupScene
*
* @type {Scenes}
*/
module.exports = class Scenes extends ApiDefinition {
constructor(hueApi) {
super(hueApi);
}
/**
* @returns {Promise<(LightScene | GroupScene)[]>}
*/
getAll() {
return this.execute(scenesApi.getAll);
}
/**
* @deprecated since 4.x use getScene(id) instead.
*/
get(id) {
util.deprecatedFunction('5.x', 'scenes.get(id)', 'Use scenes.getScene(id) instead.');
return this.getScene(id);
}
/**
* @param id {string | LightScene | GroupScene}
* @returns {Promise<LightScene | GroupScene>}
*/
getScene(id) {
return this.execute(scenesApi.getScene, {id: id});
}
/**
* @deprecated since 4.x use getSceneByName(name) instead.
*/
getByName(name) {
util.deprecatedFunction('5.x', 'scenes.getByName(name)', 'Use scenes.getSceneByName(name) instead.');
return this.getSceneByName(name);
}
/**
* Obtains the scenes that have the specified name from the bridge.
* @param name {string}
* @returns {Promise<(LightScene | GroupScene)[]>}
*/
getSceneByName(name) {
return this.getAll().then(allScenes => {
return allScenes.filter(scene => scene.name === name);
});
}
/**
* @param scene {LightScene | GroupScene}
* @returns {Promise<(LightScene | GroupScene)>}
*/
createScene(scene) {
const self = this;
return this.execute(scenesApi.createScene, {scene: scene})
.then(data => {
return self.getScene(data.id);
});
}
/**
* @deprecated since 4.x use updateScene(scene) instead.
*/
update(id, scene) {
util.deprecatedFunction('5.x', 'scenes.update(id, scene)', 'Use scenes.updateScene(scene) instead.');
return this.execute(scenesApi.updateScene, {id: id, scene: scene});
}
/**
* @param scene {LightScene | GroupScene}
* @returns {Promise<Object>}
*/
updateScene(scene) {
return this.execute(scenesApi.updateScene, {id: scene, scene: scene});
}
/**
* Updates the light state for a specific light in the scene
* @param id {string | LightScene | GroupScene}
* @param lightId {int | Light}
* @param sceneLightState {SceneLightState}
* @returns {Promise<object>}
*/
updateLightState(id, lightId, sceneLightState) {
return this.execute(scenesApi.updateSceneLightState, {id: id, lightStateId: lightId, lightState: sceneLightState});
}
/**
* @param id {string | Scene}
* @returns {Promise<boolean>}
*/
deleteScene(id) {
return this.execute(scenesApi.deleteScene, {id: id});
}
/**
* @param id {string | LightScene | GroupScene}
* @returns {Promise<boolean>}
*/
activateScene(id) {
// Scene activation is done as an intersection of setting a group light state to a scene id, the intersection of the
// scene light ids and that of the group is the lights that are targeted to change.
let sceneId = id;
if (instanceChecks.isSceneInstance(id)) {
sceneId = id.id;
}
// We target the all lights group here, so that all the lights in the scene are targeted.
return this.hueApi.groups.setGroupState(0, new GroupState().scene(sceneId));
}
};