Skip to content

Commit

Permalink
Add color temprature control for groups
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo committed Apr 27, 2020
1 parent 36a4c5c commit 646e843
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ protected void doConnectedRun() throws IOException, ApiException {
groupState.setColormode(colorRef.getColorMode());
groupState.setHue(colorRef.getHue());
groupState.setSaturation(colorRef.getSaturation());
groupState.setColorTemperature(colorRef.getColorTemperature());
groupState.setXY(colorRef.getXY());
}
logger.trace("Group {} ({}): on {} bri {} hue {} sat {} temp {} mode {} XY {}", fullGroup.getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.UnDefType;
import org.openhab.binding.hue.internal.FullGroup;
import org.openhab.binding.hue.internal.HueBridge;
import org.openhab.binding.hue.internal.State;
import org.openhab.binding.hue.internal.State.ColorMode;
import org.openhab.binding.hue.internal.StateUpdate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -54,6 +56,7 @@ public class HueGroupHandler extends BaseThingHandler implements GroupStatusList

private @NonNullByDefault({}) String groupId;

private @Nullable Integer lastSentColorTemp;
private @Nullable Integer lastSentBrightness;

private long defaultFadeTime = 400;
Expand Down Expand Up @@ -182,6 +185,21 @@ public void handleCommand(String channel, Command command, long fadeTime) {
}
}
break;
case CHANNEL_COLORTEMPERATURE:
if (command instanceof PercentType) {
groupState = LightStateConverter.toColorTemperatureLightState((PercentType) command);
if (groupState != null) {
groupState.setTransitionTime(fadeTime);
}
} else if (command instanceof OnOffType) {
groupState = LightStateConverter.toOnOffLightState((OnOffType) command);
} else if (command instanceof IncreaseDecreaseType) {
groupState = convertColorTempChangeToStateUpdate((IncreaseDecreaseType) command, group);
if (groupState != null) {
groupState.setTransitionTime(fadeTime);
}
}
break;
case CHANNEL_BRIGHTNESS:
if (command instanceof PercentType) {
groupState = LightStateConverter.toBrightnessLightState((PercentType) command);
Expand All @@ -196,11 +214,23 @@ public void handleCommand(String channel, Command command, long fadeTime) {
groupState.setTransitionTime(fadeTime);
}
}
if (groupState != null && lastSentColorTemp != null) {
// make sure that the light also has the latest color temp
// this might not have been yet set in the light, if it was off
groupState.setColorTemperature(lastSentColorTemp);
groupState.setTransitionTime(fadeTime);
}
break;
case CHANNEL_SWITCH:
if (command instanceof OnOffType) {
groupState = LightStateConverter.toOnOffLightState((OnOffType) command);
}
if (groupState != null && lastSentColorTemp != null) {
// make sure that the light also has the latest color temp
// this might not have been yet set in the light, if it was off
groupState.setColorTemperature(lastSentColorTemp);
groupState.setTransitionTime(fadeTime);
}
break;
default:
break;
Expand All @@ -211,12 +241,34 @@ public void handleCommand(String channel, Command command, long fadeTime) {
if (tmpBrightness != null) {
lastSentBrightness = tmpBrightness;
}
Integer tmpColorTemp = groupState.getColorTemperature();
if (tmpColorTemp != null) {
lastSentColorTemp = tmpColorTemp;
}
hueBridge.updateGroupState(group, groupState);
} else {
logger.warn("Command sent to an unknown channel id: {}:{}", getThing().getUID(), channel);
}
}

private @Nullable StateUpdate convertColorTempChangeToStateUpdate(IncreaseDecreaseType command, FullGroup group) {
StateUpdate stateUpdate = null;
Integer currentColorTemp = getCurrentColorTemp(group.getState());
if (currentColorTemp != null) {
int newColorTemp = LightStateConverter.toAdjustedColorTemp(command, currentColorTemp);
stateUpdate = new StateUpdate().setColorTemperature(newColorTemp);
}
return stateUpdate;
}

private @Nullable Integer getCurrentColorTemp(@Nullable State groupState) {
Integer colorTemp = lastSentColorTemp;
if (colorTemp == null && groupState != null) {
colorTemp = groupState.getColorTemperature();
}
return colorTemp;
}

private @Nullable StateUpdate convertBrightnessChangeToStateUpdate(IncreaseDecreaseType command, FullGroup group) {
StateUpdate stateUpdate = null;
Integer currentBrightness = getCurrentBrightness(group.getState());
Expand Down Expand Up @@ -272,6 +324,7 @@ public void onGroupStateChanged(@Nullable HueBridge bridge, FullGroup group) {
return;
}

lastSentColorTemp = null;
lastSentBrightness = null;

updateStatus(ThingStatus.ONLINE);
Expand All @@ -288,6 +341,14 @@ public void onGroupStateChanged(@Nullable HueBridge bridge, FullGroup group) {
}
updateState(CHANNEL_COLOR, hsbType);

ColorMode colorMode = state.getColorMode();
if (ColorMode.CT.equals(colorMode)) {
PercentType colorTempPercentType = LightStateConverter.toColorTemperaturePercentType(state);
updateState(CHANNEL_COLORTEMPERATURE, colorTempPercentType);
} else {
updateState(CHANNEL_COLORTEMPERATURE, UnDefType.NULL);
}

PercentType brightnessPercentType = LightStateConverter.toBrightnessPercentType(state);
if (!state.isOn()) {
brightnessPercentType = new PercentType(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<channels>
<channel id="switch" typeId="switch" />
<channel id="color_temperature" typeId="color_temperature" />
<channel id="brightness" typeId="brightness" />
<channel id="color" typeId="color" />
</channels>
Expand Down

0 comments on commit 646e843

Please sign in to comment.