Skip to content

Commit 21daf77

Browse files
- Fixes #45
- Added function to separate group lights into models - Improved tests to cover issues #45 and #44
1 parent e2aef52 commit 21daf77

5 files changed

Lines changed: 118 additions & 3 deletions

File tree

Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Change Log
22

3+
## 1.0.4
4+
- Fixes issue #45 creating a scene resulted in a NaN id for the scene created
5+
36
## 1.0.3
47
- Fixes issue #44 generating an incorrect error when the id for `setLightState()` is not valid
58

hue-api/index.js

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ HueApi.prototype.setLightState = function (id, stateValues, cb) {
337337
*/
338338
HueApi.prototype.setGroupLightState = function (id, stateValues, cb) {
339339
var promise = this._getGroupLightStateOptions(id, stateValues)
340-
.then(function(options) {
340+
.then(function (options) {
341341
return http.invoke(groupsApi.setGroupState, options);
342342
});
343343
return utils.promiseOrCallback(promise, cb);
@@ -859,7 +859,7 @@ HueApi.prototype._getConfig = function () {
859859
};
860860

861861
HueApi.prototype._getScenePrefix = function () {
862-
return this._getConfig().scene_prefix;
862+
return this._getConfig().scenePrefix;
863863
};
864864

865865
/**
@@ -928,10 +928,53 @@ HueApi.prototype._getNextSceneId = function () {
928928
});
929929
}
930930

931-
return scenePrefix + (maxId + 1);
931+
return "" + scenePrefix + (maxId + 1);
932932
});
933933
};
934934

935+
/**
936+
* Obtains the lights in a group and separates them into sub groups based on the model.
937+
* @param groupId The id of the group.
938+
* @returns {Object} A map of modelid to and array of lights that have that model.
939+
* @private
940+
*/
941+
HueApi.prototype._getGroupLightsByType = function (groupId) {
942+
var self = this;
943+
944+
return Q.all(
945+
[
946+
self.getGroup(groupId),
947+
self.getLights()
948+
])
949+
.spread(function (group, allLights) {
950+
var map = {}
951+
, lightMap = getLightsModelMap(allLights)
952+
;
953+
954+
if (group && group.lights) {
955+
group.lights.forEach(function(lightId) {
956+
var modelid = lightMap[lightId];
957+
958+
if (map[modelid]) {
959+
map[modelid].push(lightId);
960+
} else {
961+
map[modelid] = [lightId];
962+
}
963+
});
964+
}
965+
966+
return map;
967+
});
968+
};
969+
970+
/**
971+
* Generates the light state options for a group
972+
* @param groupId The group to apply the state values to
973+
* @param stateValues The state of the lights to apply
974+
* @returns {Q.promise} That will resolve to a set of options for the group or an array of options to apply subsets of
975+
* lights in the group.
976+
* @private
977+
*/
935978
HueApi.prototype._getGroupLightStateOptions = function (groupId, stateValues) {
936979
var self = this
937980
, options = self._defaultOptions()
@@ -961,6 +1004,30 @@ HueApi.prototype._getGroupLightStateOptions = function (groupId, stateValues) {
9611004
deferred = Q.defer();
9621005
deferred.reject(new ApiError("RGB state is not supported for groups yet"));
9631006
promise = deferred.promise;
1007+
1008+
//// Separate the lights into models and apply the state to each type
1009+
//promise = self._getGroupLightsByType(groupId)
1010+
// .then(function(groupLightsMap) {
1011+
// var models = Object.keys(groupLightsMap)
1012+
// , result = []
1013+
// ;
1014+
//
1015+
// models.forEach(function(model) {
1016+
// var newState = state.copy();
1017+
// newState.applyRGB(model);
1018+
//
1019+
// result.push({
1020+
// modelid: model,
1021+
// lights: groupLightsMap[model],
1022+
// state: newState
1023+
// });
1024+
// });
1025+
//
1026+
// return result;
1027+
// })
1028+
// .then(function(subgroupsWithState) {
1029+
// //TODO need to create options
1030+
// });
9641031
} else {
9651032
options.values = state.payload();
9661033

@@ -1121,6 +1188,18 @@ function _setConfigurationOptions(options, values) {
11211188
return errorPromise;
11221189
}
11231190

1191+
function getLightsModelMap(lightsArray) {
1192+
var map = {};
1193+
1194+
if (Array.isArray(lightsArray)) {
1195+
lightsArray.forEach(function(light) {
1196+
map[light.id] = light.modelid;
1197+
});
1198+
}
1199+
1200+
return map;
1201+
}
1202+
11241203
/**
11251204
* Validates and then injects the 'id' into the options for a light in the bridge.
11261205
*

test/groups-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,20 @@ describe("Hue API", function () {
476476
.done();
477477
});
478478
});
479+
480+
describe("#_getGroupLightsByType", function() {
481+
482+
it("should work for group 0", function(done) {
483+
hue._getGroupLightsByType(0)
484+
.then(function(map) {
485+
486+
console.log(JSON.stringify(map, null, 2));
487+
488+
done();
489+
})
490+
.done();
491+
});
492+
});
479493
});
480494
});
481495

test/scene-tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ describe("Hue API", function () {
141141
return function (result) {
142142
expect(result).to.be.defined;
143143
expect(result).to.have.property("id");
144+
expect(result.id).to.contain("node-hue-api");
144145
expect(result).to.have.property("name", name);
145146

146147
expect(result).to.have.property("lights");

test/setLightState-tests.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,23 @@ describe("Hue API", function () {
159159
// .done();
160160
// });
161161
//});
162+
163+
//TODO complete the error checking
164+
describe("using an invalid light id", function() {
165+
166+
it("should fail with appropriate message", function(done) {
167+
var state = lightState.create().on().rgb(100, 100, 100);
168+
169+
hue.setLightState(0, state)
170+
.then(function() {
171+
throw new Error("should not be called");
172+
}, function(err) {
173+
expect(err.message).to.contain("light id");
174+
expect(err.message).to.contain("is not valid");
175+
done();
176+
})
177+
.done();
178+
});
179+
});
162180
});
163181
});

0 commit comments

Comments
 (0)