Skip to content

Commit

Permalink
- Fixes #45
Browse files Browse the repository at this point in the history
- Added function to separate group lights into models
- Improved tests to cover issues #45 and #44
  • Loading branch information
osirisoft-pmurray committed Feb 8, 2015
1 parent e2aef52 commit 21daf77
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## 1.0.4
- Fixes issue #45 creating a scene resulted in a NaN id for the scene created

## 1.0.3
- Fixes issue #44 generating an incorrect error when the id for `setLightState()` is not valid

Expand Down
85 changes: 82 additions & 3 deletions hue-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ HueApi.prototype.setLightState = function (id, stateValues, cb) {
*/
HueApi.prototype.setGroupLightState = function (id, stateValues, cb) {
var promise = this._getGroupLightStateOptions(id, stateValues)
.then(function(options) {
.then(function (options) {
return http.invoke(groupsApi.setGroupState, options);
});
return utils.promiseOrCallback(promise, cb);
Expand Down Expand Up @@ -859,7 +859,7 @@ HueApi.prototype._getConfig = function () {
};

HueApi.prototype._getScenePrefix = function () {
return this._getConfig().scene_prefix;
return this._getConfig().scenePrefix;
};

/**
Expand Down Expand Up @@ -928,10 +928,53 @@ HueApi.prototype._getNextSceneId = function () {
});
}

return scenePrefix + (maxId + 1);
return "" + scenePrefix + (maxId + 1);
});
};

/**
* Obtains the lights in a group and separates them into sub groups based on the model.
* @param groupId The id of the group.
* @returns {Object} A map of modelid to and array of lights that have that model.
* @private
*/
HueApi.prototype._getGroupLightsByType = function (groupId) {
var self = this;

return Q.all(
[
self.getGroup(groupId),
self.getLights()
])
.spread(function (group, allLights) {
var map = {}
, lightMap = getLightsModelMap(allLights)
;

if (group && group.lights) {
group.lights.forEach(function(lightId) {
var modelid = lightMap[lightId];

if (map[modelid]) {
map[modelid].push(lightId);
} else {
map[modelid] = [lightId];
}
});
}

return map;
});
};

/**
* Generates the light state options for a group
* @param groupId The group to apply the state values to
* @param stateValues The state of the lights to apply
* @returns {Q.promise} That will resolve to a set of options for the group or an array of options to apply subsets of
* lights in the group.
* @private
*/
HueApi.prototype._getGroupLightStateOptions = function (groupId, stateValues) {
var self = this
, options = self._defaultOptions()
Expand Down Expand Up @@ -961,6 +1004,30 @@ HueApi.prototype._getGroupLightStateOptions = function (groupId, stateValues) {
deferred = Q.defer();
deferred.reject(new ApiError("RGB state is not supported for groups yet"));
promise = deferred.promise;

//// Separate the lights into models and apply the state to each type
//promise = self._getGroupLightsByType(groupId)
// .then(function(groupLightsMap) {
// var models = Object.keys(groupLightsMap)
// , result = []
// ;
//
// models.forEach(function(model) {
// var newState = state.copy();
// newState.applyRGB(model);
//
// result.push({
// modelid: model,
// lights: groupLightsMap[model],
// state: newState
// });
// });
//
// return result;
// })
// .then(function(subgroupsWithState) {
// //TODO need to create options
// });
} else {
options.values = state.payload();

Expand Down Expand Up @@ -1121,6 +1188,18 @@ function _setConfigurationOptions(options, values) {
return errorPromise;
}

function getLightsModelMap(lightsArray) {
var map = {};

if (Array.isArray(lightsArray)) {
lightsArray.forEach(function(light) {
map[light.id] = light.modelid;
});
}

return map;
}

/**
* Validates and then injects the 'id' into the options for a light in the bridge.
*
Expand Down
14 changes: 14 additions & 0 deletions test/groups-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,20 @@ describe("Hue API", function () {
.done();
});
});

describe("#_getGroupLightsByType", function() {

it("should work for group 0", function(done) {
hue._getGroupLightsByType(0)
.then(function(map) {

console.log(JSON.stringify(map, null, 2));

done();
})
.done();
});
});
});
});

Expand Down
1 change: 1 addition & 0 deletions test/scene-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ describe("Hue API", function () {
return function (result) {
expect(result).to.be.defined;
expect(result).to.have.property("id");
expect(result.id).to.contain("node-hue-api");
expect(result).to.have.property("name", name);

expect(result).to.have.property("lights");
Expand Down
18 changes: 18 additions & 0 deletions test/setLightState-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,23 @@ describe("Hue API", function () {
// .done();
// });
//});

//TODO complete the error checking
describe("using an invalid light id", function() {

it("should fail with appropriate message", function(done) {
var state = lightState.create().on().rgb(100, 100, 100);

hue.setLightState(0, state)
.then(function() {
throw new Error("should not be called");
}, function(err) {
expect(err.message).to.contain("light id");
expect(err.message).to.contain("is not valid");
done();
})
.done();
});
});
});
});

0 comments on commit 21daf77

Please sign in to comment.