Skip to content

Commit

Permalink
[hue] Add support for groups/rooms
Browse files Browse the repository at this point in the history
Fix #7419

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo committed Apr 27, 2020
1 parent 0671c4f commit c924ec6
Show file tree
Hide file tree
Showing 16 changed files with 871 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,26 @@
*/
package org.openhab.binding.hue.internal;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import com.google.gson.reflect.TypeToken;

/**
* Detailed group information.
*
* @author Q42 - Initial contribution
* @author Denis Dudnik - moved Jue library source code inside the smarthome Hue binding
* @author Laurent Garnier - field state added
*/
public class FullGroup extends Group {
public static final Type GSON_TYPE = new TypeToken<Map<String, FullGroup>>() {
}.getType();

private State action;
private List<String> lights;
private State state; // Will not be set by hue API

FullGroup() {
}
Expand All @@ -42,7 +51,16 @@ public State getAction() {
*
* @return lights in the group
*/
public List<HueObject> getLights() {
return Util.idsToLights(lights);
public List<String> getLights() {
return lights;
}

/**
* Returns the current state of the group.
*
* @return current state
*/
public State getState() {
return state;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,22 @@
*/
package org.openhab.binding.hue.internal;

import java.lang.reflect.Type;
import java.util.Map;

import com.google.gson.reflect.TypeToken;

/**
* Basic group information.
*
* @author Q42 - Initial contribution
* @author Denis Dudnik - moved Jue library source code inside the smarthome Hue binding
* @author Laurent Garnier - field type added
*/
public class Group {
public static final Type GSON_TYPE = new TypeToken<Map<String, Group>>() {
}.getType();

private String id;
private String name;
private String type;

Group() {
this.id = "0";
this.name = "Lightset 0";
this.type = "LightGroup";
}

void setName(String name) {
Expand All @@ -43,6 +38,10 @@ void setId(String id) {
this.id = id;
}

void setType(String type) {
this.type = type;
}

/**
* Returns if the group can be modified.
* Currently only returns false for the all lights pseudo group.
Expand Down Expand Up @@ -70,4 +69,13 @@ public String getId() {
public String getName() {
return name;
}

/**
* Returns the tyoe of the group.
*
* @return type
*/
public String getType() {
return type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.binding.hue.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.thing.ThingTypeUID;

/**
Expand All @@ -25,6 +26,7 @@
* @author Samuel Leisering - Added support for sensor API
* @author Christoph Weitkamp - Added support for sensor API
*/
@NonNullByDefault
public class HueBindingConstants {

public static final String BINDING_ID = "hue";
Expand All @@ -51,6 +53,8 @@ public class HueBindingConstants {
public static final ThingTypeUID THING_TYPE_TEMPERATURE_SENSOR = new ThingTypeUID(BINDING_ID, "0302");
public static final ThingTypeUID THING_TYPE_LIGHT_LEVEL_SENSOR = new ThingTypeUID(BINDING_ID, "0106");

public static final ThingTypeUID THING_TYPE_GROUP = new ThingTypeUID(BINDING_ID, "group");

// List all channels
public static final String CHANNEL_COLORTEMPERATURE = "color_temperature";
public static final String CHANNEL_COLOR = "color";
Expand Down Expand Up @@ -88,6 +92,7 @@ public class HueBindingConstants {
public static final String PRODUCT_NAME = "productName";
public static final String UNIQUE_ID = "uniqueId";
public static final String FADETIME = "fadetime";
public static final String GROUP_ID = "groupId";

public static final String NORMALIZE_ID_REGEX = "[^a-zA-Z0-9_]";
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
* @author Andre Fuechsel - search for lights with given serial number added
* @author Denis Dudnik - moved Jue library source code inside the smarthome Hue binding, minor code cleanup
* @author Samuel Leisering - added cached config and API-Version
* @author Laurent Garnier - change the return type of getGroups
*/
@NonNullByDefault
public class HueBridge {
Expand Down Expand Up @@ -415,20 +416,23 @@ public Group getAllGroup() {
* @return list of groups
* @throws UnauthorizedException thrown if the user no longer exists
*/
public List<Group> getGroups() throws IOException, ApiException {
public List<FullGroup> getGroups() throws IOException, ApiException {
requireAuthentication();

Result result = http.get(getRelativeURL("groups"));

handleErrors(result);

Map<String, Group> groupMap = safeFromJson(result.getBody(), Group.GSON_TYPE);
ArrayList<Group> groupList = new ArrayList<>();
Map<String, FullGroup> groupMap = safeFromJson(result.getBody(), FullGroup.GSON_TYPE);
ArrayList<FullGroup> groupList = new ArrayList<>();

groupList.add(new Group());
if (groupMap.get("0") == null) {
// Group 0 is not returned, we create it as in fact it exists
groupList.add(getGroup(new Group()));
}

for (String id : groupMap.keySet()) {
Group group = groupMap.get(id);
FullGroup group = groupMap.get(id);
group.setId(id);
groupList.add(group);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory;
import org.openhab.binding.hue.internal.discovery.HueLightDiscoveryService;
import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
import org.openhab.binding.hue.internal.handler.HueGroupHandler;
import org.openhab.binding.hue.internal.handler.HueLightHandler;
import org.openhab.binding.hue.internal.handler.sensors.ClipHandler;
import org.openhab.binding.hue.internal.handler.sensors.DimmerSwitchHandler;
Expand All @@ -53,16 +54,17 @@
* @author Andre Fuechsel - implemented to use one discovery service per bridge
* @author Samuel Leisering - Added support for sensor API
* @author Christoph Weitkamp - Added support for sensor API
* @author Laurent Garnier - Added support for groups
*/
@NonNullByDefault
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.hue")
public class HueThingHandlerFactory extends BaseThingHandlerFactory {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.unmodifiableSet(Stream
.of(HueBridgeHandler.SUPPORTED_THING_TYPES.stream(), HueLightHandler.SUPPORTED_THING_TYPES.stream(),
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.unmodifiableSet(
Stream.of(HueBridgeHandler.SUPPORTED_THING_TYPES.stream(), HueLightHandler.SUPPORTED_THING_TYPES.stream(),
DimmerSwitchHandler.SUPPORTED_THING_TYPES.stream(), TapSwitchHandler.SUPPORTED_THING_TYPES.stream(),
PresenceHandler.SUPPORTED_THING_TYPES.stream(), TemperatureHandler.SUPPORTED_THING_TYPES.stream(),
LightLevelHandler.SUPPORTED_THING_TYPES.stream(), ClipHandler.SUPPORTED_THING_TYPES.stream())
.flatMap(i -> i).collect(Collectors.toSet()));
LightLevelHandler.SUPPORTED_THING_TYPES.stream(), ClipHandler.SUPPORTED_THING_TYPES.stream(),
HueGroupHandler.SUPPORTED_THING_TYPES.stream()).flatMap(i -> i).collect(Collectors.toSet()));

private final Map<ThingUID, @Nullable ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();

Expand All @@ -82,6 +84,9 @@ public class HueThingHandlerFactory extends BaseThingHandlerFactory {
|| ClipHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
ThingUID hueSensorUID = getSensorUID(thingTypeUID, thingUID, configuration, bridgeUID);
return super.createThing(thingTypeUID, configuration, hueSensorUID, bridgeUID);
} else if (HueGroupHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
ThingUID hueGroupUID = getGroupUID(thingTypeUID, thingUID, configuration, bridgeUID);
return super.createThing(thingTypeUID, configuration, hueGroupUID, bridgeUID);
}

throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the hue binding.");
Expand Down Expand Up @@ -110,6 +115,15 @@ private ThingUID getSensorUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thin
}
}

private ThingUID getGroupUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
@Nullable ThingUID bridgeUID) {
if (thingUID != null) {
return thingUID;
} else {
return getThingUID(thingTypeUID, configuration.get(GROUP_ID).toString(), bridgeUID);
}
}

private ThingUID getThingUID(ThingTypeUID thingTypeUID, String id, @Nullable ThingUID bridgeUID) {
if (bridgeUID != null) {
return new ThingUID(thingTypeUID, id, bridgeUID.getId());
Expand Down Expand Up @@ -138,6 +152,8 @@ private ThingUID getThingUID(ThingTypeUID thingTypeUID, String id, @Nullable Thi
return new LightLevelHandler(thing);
} else if (ClipHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
return new ClipHandler(thing);
} else if (HueGroupHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
return new HueGroupHandler(thing);
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*
* @author Q42 - Initial contribution
* @author Denis Dudnik - moved Jue library source code inside the smarthome Hue binding
* @author Laurent Garnier - add few methods to update the object
*/
public class State {
private boolean on;
Expand Down Expand Up @@ -99,6 +100,10 @@ public boolean isOn() {
return on;
}

public void setOn(boolean on) {
this.on = on;
}

/**
* Returns the brightness.
*
Expand All @@ -108,6 +113,10 @@ public int getBrightness() {
return bri;
}

public void setBri(int bri) {
this.bri = bri;
}

/**
* Returns the hue.
*
Expand All @@ -117,6 +126,10 @@ public int getHue() {
return hue;
}

public void setHue(int hue) {
this.hue = hue;
}

/**
* Returns the saturation.
*
Expand All @@ -126,6 +139,10 @@ public int getSaturation() {
return sat;
}

public void setSaturation(int sat) {
this.sat = sat;
}

/**
* Returns the coordinates in CIE color space.
*
Expand All @@ -135,6 +152,10 @@ public float[] getXY() {
return xy;
}

public void setXY(float[] xy) {
this.xy = xy;
}

/**
* Returns the color temperature.
*
Expand All @@ -144,6 +165,10 @@ public int getColorTemperature() {
return ct;
}

public void setColorTemperature(int ct) {
this.ct = ct;
}

/**
* Returns the last alert mode set.
* Future firmware updates may change this to actually report the current alert mode.
Expand All @@ -169,6 +194,10 @@ public ColorMode getColorMode() {
return ColorMode.valueOf(colormode.toUpperCase());
}

public void setColormode(ColorMode colormode) {
this.colormode = colormode.name();
}

/**
* Returns the current active effect.
*
Expand Down
Loading

0 comments on commit c924ec6

Please sign in to comment.