Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Implemented Color Mode channel for the Extended Color Light.
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Kostadinov <[email protected]>
  • Loading branch information
alex-kostadinov committed Jun 25, 2018
1 parent 2e00585 commit 3f9e4f6
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.types.Command;
import org.junit.Before;
Expand Down Expand Up @@ -342,7 +343,9 @@ private void assertSendCommand(String channel, Command command, HueLightState cu

Thing mockThing = mock(Thing.class);
when(mockThing.getConfiguration()).thenReturn(new Configuration(Collections.singletonMap(LIGHT_ID, "1")));

when(mockThing.getUID()).thenReturn(new ThingUID(BINDING_ID, expectedModel));
when(mockThing.getThingTypeUID()).thenReturn(new ThingTypeUID(BINDING_ID, expectedModel));

HueClient mockClient = mock(HueClient.class);
when(mockClient.getLightById(any())).thenReturn(light);

Expand All @@ -356,6 +359,11 @@ protected synchronized HueClient getHueClient() {
protected Bridge getBridge() {
return mockBridge;
}

@Override
public Thing getThing() {
return mockThing;
}
};
hueLightHandler.initialize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<channel id="color_temperature" typeId="color_temperature" />
<channel id="alert" typeId="alert" />
<channel id="effect" typeId="effect" />
<channel id="color_mode" typeId="color_mode" />
</channels>

<representation-property>uniqueId</representation-property>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,17 @@
<description>The effect channel allows putting the bulb in a color looping mode.</description>
<category>ColorLight</category>
</channel-type>

<!-- Color Mode Channel -->
<channel-type id="color_mode" advanced="true">
<item-type>String</item-type>
<label>Color Mode</label>
<description>The color mode channel denotes if the bulb is in a Color or Color Temperature mode.</description>
<state readOnly="true">
<options>
<option value="COLOR">Color Mode</option>
<option value="COLOR_TEMPERATURE">Color Temperature Mode</option>
</options>
</state>
</channel-type>
</thing:thing-descriptions>
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class HueBindingConstants {
public static final String CHANNEL_ALERT = "alert";
public static final String CHANNEL_EFFECT = "effect";
public static final String CHANNEL_SWITCH = "switch";
public static final String CHANNEL_COLORMODE = "color_mode";

// Bridge config properties
public static final String HOST = "ipAddress";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,25 @@
import org.eclipse.smarthome.binding.hue.internal.FullLight;
import org.eclipse.smarthome.binding.hue.internal.HueBridge;
import org.eclipse.smarthome.binding.hue.internal.State;
import org.eclipse.smarthome.binding.hue.internal.State.ColorMode;
import org.eclipse.smarthome.binding.hue.internal.StateUpdate;
import org.eclipse.smarthome.core.library.types.HSBType;
import org.eclipse.smarthome.core.library.types.IncreaseDecreaseType;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.PercentType;
import org.eclipse.smarthome.core.library.types.StringType;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingStatusDetail;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder;
import org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder;
import org.eclipse.smarthome.core.thing.type.ChannelTypeUID;
import org.eclipse.smarthome.core.types.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -125,6 +130,11 @@ private void initializeThing(@Nullable ThingStatus bridgeStatus) {
if (getHueClient() != null) {
if (bridgeStatus == ThingStatus.ONLINE) {
initializeProperties();

if (colorModeChannelRequired()) {
addColorModeChannel();
}

updateStatus(ThingStatus.ONLINE);
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
Expand Down Expand Up @@ -422,6 +432,14 @@ public void onLightStateChanged(@Nullable HueBridge bridge, FullLight fullLight)
updateState(CHANNEL_ALERT, stringType);
scheduleAlertStateRestore(stringType);
}

ColorMode colorMode = fullLight.getState().getColorMode();
if (colorMode != null) {
updateState(CHANNEL_COLORMODE, LightStateConverter.toColorModeStringType(colorMode));
} else {
// By default fall back to pure Hue & Saturation color mode
updateState(CHANNEL_COLORMODE, LightStateConverter.toColorModeStringType(ColorMode.HS));
}
}

@Override
Expand Down Expand Up @@ -512,4 +530,30 @@ private int getAlertDuration(Command command) {

return delay;
}

/**
* Performs a check if the color_mode channel needs to
* be added to the remote thing
* @return <b>true</b> if the thing type is 0210 and
* the color_mode channel is missing
*/
private boolean colorModeChannelRequired() {
ChannelUID colorModeChannelUID = new ChannelUID(getThing().getUID(), CHANNEL_COLORMODE);
return (getThing().getThingTypeUID().equals(THING_TYPE_EXTENDED_COLOR_LIGHT))
&& (getThing().getChannel(colorModeChannelUID.getAsString()) == null);
}

/**
* Creates the color_mode channel for
* an already paired remote thing
*/
private void addColorModeChannel() {
ChannelUID colorModeChannelUID = new ChannelUID(getThing().getUID(), CHANNEL_COLORMODE);

Channel colorModeChannel = ChannelBuilder.create(colorModeChannelUID, "String")
.withType(new ChannelTypeUID(BINDING_ID, CHANNEL_COLORMODE)).build();

ThingBuilder thingBuilder = editThing().withChannel(colorModeChannel);
updateThing(thingBuilder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.binding.hue.internal.State;
import org.eclipse.smarthome.binding.hue.internal.State.AlertMode;
import org.eclipse.smarthome.binding.hue.internal.State.ColorMode;
import org.eclipse.smarthome.binding.hue.internal.State.Effect;
import org.eclipse.smarthome.binding.hue.internal.StateUpdate;
import org.eclipse.smarthome.core.library.types.DecimalType;
Expand Down Expand Up @@ -47,6 +48,9 @@ public class LightStateConverter {
private static final int MIN_COLOR_TEMPERATURE = 153;
private static final int MAX_COLOR_TEMPERATURE = 500;
private static final int COLOR_TEMPERATURE_RANGE = MAX_COLOR_TEMPERATURE - MIN_COLOR_TEMPERATURE;

public static final String COLORMODE_COLOR = "COLOR";
public static final String COLORMODE_COLOR_TEMPERATURE = "COLOR_TEMPERATURE";

/**
* {@value #ALERT_MODE_NONE}. The light is not performing an alert effect.
Expand Down Expand Up @@ -265,6 +269,29 @@ public static StateUpdate toOnOffEffectState(OnOffType onOffType) {

return stateUpdate;
}

/**
* Transforms a given {@link ColorMode} to its corresponding {@link StringType} as follows:
* Supported values are:
* <ul>
* <li>XY (CIE color space coordinates) and HS (Hue & saturation) color modes are mapped to {@link #COLORMODE_COLOR}
* <li>CT (color temperature) color mode is mapped to {@link #COLORMODE_COLOR_TEMPERATURE}
* <ul>
*
* @param {@link ColorMode} colorMode
* @return A {@link StringType} representing the given ColorMode
*/
public static StringType toColorModeStringType(@Nullable ColorMode colorMode) {
switch (colorMode) {
case XY:
case HS:
return new StringType(COLORMODE_COLOR);
case CT:
return new StringType(COLORMODE_COLOR_TEMPERATURE);
default:
return new StringType(COLORMODE_COLOR);
}
}

private static int restrictToBounds(int percentValue) {
if (percentValue < 0) {
Expand Down

0 comments on commit 3f9e4f6

Please sign in to comment.