@@ -337,7 +337,7 @@ HueApi.prototype.setLightState = function (id, stateValues, cb) {
337337 */
338338HueApi . 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
861861HueApi . 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+ */
935978HueApi . 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 *
0 commit comments