diff --git a/addons/io/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/HueEmulationServlet.java b/addons/io/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/HueEmulationServlet.java index a72875f908ec8..e77c5722baf53 100644 --- a/addons/io/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/HueEmulationServlet.java +++ b/addons/io/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/HueEmulationServlet.java @@ -41,7 +41,9 @@ import org.eclipse.smarthome.core.items.ItemRegistry; import org.eclipse.smarthome.core.items.events.ItemEventFactory; import org.eclipse.smarthome.core.library.types.DecimalType; +import org.eclipse.smarthome.core.library.types.HSBType; import org.eclipse.smarthome.core.library.types.OnOffType; +import org.eclipse.smarthome.core.library.types.PercentType; import org.eclipse.smarthome.core.types.Command; import org.eclipse.smarthome.core.types.State; import org.eclipse.smarthome.core.types.TypeParser; @@ -258,15 +260,22 @@ private void apiState(String id, HttpServletRequest req, HttpServletResponse res // will throw exception if not found Item item = itemRegistry.getItem(id); HueState state = gson.fromJson(req.getReader(), HueState.class); + logger.debug("State " + state); + String value; - if (state.bri > -1) { + if (item.getAcceptedCommandTypes().contains(HSBType.class)) { + value = String.format("%d,%d,%d", state.hue, state.sat, state.bri); + } else if (state.bri > -1 && (item.getAcceptedCommandTypes().contains(DecimalType.class) + || item.getAcceptedCommandTypes().contains(PercentType.class))) { value = String.valueOf(Math.round(state.bri / 255.0 * 100)); } else { value = state.on ? "ON" : "OFF"; } + Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), value); eventPublisher.post(ItemEventFactory.createCommandEvent(id, command)); + PrintWriter out = resp.getWriter(); out.write(String.format(STATE_RESP, id, String.valueOf(state.on))); out.close(); @@ -434,13 +443,21 @@ public Map getHueDeviceNames() { */ private HueDevice itemToDevice(Item item) { State itemState = item.getState(); - short bri = 0; - if (itemState instanceof DecimalType) { - bri = (short) ((((DecimalType) itemState).intValue() * 255) / 100); + HueState hueState; + if (itemState instanceof HSBType) { + HSBType color = (HSBType) itemState; + hueState = new HueState(color.getHue().intValue(), color.getSaturation().shortValue(), + color.getBrightness().shortValue()); + } else if (itemState instanceof DecimalType) { + short bri = (short) ((((DecimalType) itemState).intValue() * 255) / 100); + hueState = new HueState(bri > 0, bri); } else if (itemState instanceof OnOffType) { - bri = (short) (((OnOffType) itemState) == OnOffType.ON ? 255 : 0); + short bri = (short) (((OnOffType) itemState) == OnOffType.ON ? 255 : 0); + hueState = new HueState(bri > 0, bri); + } else { + hueState = new HueState(false, (short) 0); } - HueState hueState = new HueState(bri > 0, bri); + HueDevice d = new HueDevice(hueState, item.getLabel(), item.getName()); return d; } diff --git a/addons/io/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/api/HueState.java b/addons/io/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/api/HueState.java index a9257d97515b2..a389dc85dc7bc 100644 --- a/addons/io/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/api/HueState.java +++ b/addons/io/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/api/HueState.java @@ -36,6 +36,14 @@ public HueState(boolean on, short bri) { this.bri = bri; } + public HueState(int h, short s, short b) { + super(); + this.on = b > 0; + this.hue = h; + this.sat = s; + this.bri = b; + } + @Override public String toString() { String xyString = "{";