Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hue] Allow handling of QuantityType for color temperature channel #14024

Merged
merged 5 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bundles/org.openhab.binding.hue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ The devices support some of the following channels:
| color | Color | This channel supports full color control with hue, saturation and brightness values. | 0200, 0210, group |
| brightness | Dimmer | This channel supports adjusting the brightness value. Note that this is not available, if the color channel is supported. | 0100, 0110, 0220, group |
| color_temperature | Dimmer | This channel supports adjusting the color temperature from cold (0%) to warm (100%). | 0210, 0220, group |
| color_temperature_abs | Number | This channel supports adjusting the color temperature in Kelvin. **Advanced** | 0210, 0220, group |
| color_temperature_abs | Number:Temperature | This channel supports adjusting the color temperature in Kelvin. **Advanced** | 0210, 0220, group |
| alert | String | This channel supports displaying alerts by flashing the bulb either once or multiple times. Valid values are: NONE, SELECT and LSELECT. | 0000, 0100, 0200, 0210, 0220, group |
| effect | Switch | This channel supports color looping. | 0200, 0210, 0220 |
| dimmer_switch | Number | This channel shows which button was last pressed on the dimmer switch. | 0820 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
Expand Down Expand Up @@ -225,8 +227,18 @@ public void handleCommand(String channel, Command command, long fadeTime) {
}
break;
case CHANNEL_COLORTEMPERATURE_ABS:
if (command instanceof DecimalType) {
newState = LightStateConverter.toColorTemperatureLightState((DecimalType) command,
if (command instanceof QuantityType) {
QuantityType<?> convertedCommand = ((QuantityType<?>) command).toInvertibleUnit(Units.KELVIN);
if (convertedCommand != null) {
newState = LightStateConverter.toColorTemperatureLightState(convertedCommand.intValue(),
colorTemperatureCapabilties);
newState.setTransitionTime(fadeTime);
} else {
logger.warn("Unable to convert unit from '{}' to '{}'. Skipping command.",
((QuantityType<?>) command).getUnit(), Units.KELVIN);
}
} else if (command instanceof DecimalType) {
newState = LightStateConverter.toColorTemperatureLightState(((DecimalType) command).intValue(),
colorTemperatureCapabilties);
newState.setTransitionTime(fadeTime);
}
Expand Down Expand Up @@ -284,13 +296,14 @@ public void handleCommand(String channel, Command command, long fadeTime) {
}
break;
default:
logger.debug("Command sent to an unknown channel id: {}:{}", getThing().getUID(), channel);
break;
}
if (newState != null) {
cacheNewState(newState);
bridgeHandler.updateGroupState(group, newState, fadeTime);
} else {
logger.debug("Command sent to an unknown channel id: {}:{}", getThing().getUID(), channel);
logger.debug("Unable to handle command '{}' for channel '{}'. Skipping command.", command, channel);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
Expand Down Expand Up @@ -267,8 +269,18 @@ public void handleCommand(String channel, Command command, long fadeTime) {
}
break;
case CHANNEL_COLORTEMPERATURE_ABS:
if (command instanceof DecimalType) {
newState = LightStateConverter.toColorTemperatureLightState((DecimalType) command,
if (command instanceof QuantityType) {
QuantityType<?> convertedCommand = ((QuantityType<?>) command).toInvertibleUnit(Units.KELVIN);
if (convertedCommand != null) {
newState = LightStateConverter.toColorTemperatureLightState(convertedCommand.intValue(),
colorTemperatureCapabilties);
newState.setTransitionTime(fadeTime);
} else {
logger.warn("Unable to convert unit from '{}' to '{}'. Skipping command.",
((QuantityType<?>) command).getUnit(), Units.KELVIN);
}
} else if (command instanceof DecimalType) {
newState = LightStateConverter.toColorTemperatureLightState(((DecimalType) command).intValue(),
colorTemperatureCapabilties);
newState.setTransitionTime(fadeTime);
}
Expand Down Expand Up @@ -356,6 +368,9 @@ public void handleCommand(String channel, Command command, long fadeTime) {
newState = LightStateConverter.toOnOffEffectState((OnOffType) command);
}
break;
default:
logger.debug("Command sent to an unknown channel id: {}:{}", getThing().getUID(), channel);
break;
}
if (newState != null) {
// Cache values which we have sent
Expand All @@ -369,7 +384,7 @@ public void handleCommand(String channel, Command command, long fadeTime) {
}
bridgeHandler.updateLightState(this, light, newState, fadeTime);
} else {
logger.warn("Command sent to an unknown channel id: {}:{}", getThing().getUID(), channel);
logger.debug("Unable to handle command '{}' for channel '{}'. Skipping command.", command, channel);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.binding.hue.internal.handler;

import javax.measure.quantity.Temperature;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.hue.internal.dto.ColorTemperature;
Expand All @@ -25,7 +27,9 @@
import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.unit.Units;

/**
* The {@link LightStateConverter} is responsible for mapping to/from jue types.
Expand Down Expand Up @@ -161,14 +165,14 @@ public static int kelvinToMired(int kelvinValue) {
}

/**
* Transforms the given {@link DecimalType} into a light state containing
* the color temperature in Kelvin.
* Transforms the given color temperature in Kelvin into a Hue Light {@link State}.
*
* @param decimalType color temperature in Kelvin
* @param kelvinValue color temperature in Kelvin
* @param capabilities color temperature capabilities (e.g. min and max values)
* @return light state containing the color temperature
*/
public static StateUpdate toColorTemperatureLightState(DecimalType decimalType, ColorTemperature capabilities) {
return new StateUpdate().setColorTemperature(kelvinToMired(decimalType.intValue()), capabilities);
public static StateUpdate toColorTemperatureLightState(int kelvinValue, ColorTemperature capabilities) {
return new StateUpdate().setColorTemperature(kelvinToMired(kelvinValue), capabilities);
}

/**
Expand Down Expand Up @@ -207,14 +211,14 @@ public static int miredToKelvin(int miredValue) {
}

/**
* Transforms Hue Light {@link State} into {@link DecimalType} representing
* Transforms Hue Light {@link State} into {@link QuantityType} representing
* the color temperature in Kelvin.
*
* @param lightState light state
* @return percent type representing the color temperature in Kelvin
* @return quantity type representing the color temperature in Kelvin
*/
public static DecimalType toColorTemperature(State lightState) {
return new DecimalType(miredToKelvin(lightState.getColorTemperature()));
public static QuantityType<Temperature> toColorTemperature(State lightState) {
return new QuantityType<>(miredToKelvin(lightState.getColorTemperature()), Units.KELVIN);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
Expand Down Expand Up @@ -148,23 +150,47 @@ public void assertCommandForColorTemperatureChannel1000Percent() {
}

@Test
public void assertCommandForColorTemperatureAbsChannel6500Kelvin() {
public void assertDecimalTypeCommandForColorTemperatureAbsChannel6500Kelvin() {
String expectedReply = "{\"ct\" : 153, \"transitiontime\" : 4}";
assertSendCommandForColorTempAbs(new DecimalType(6500), new HueLightState(), expectedReply);
}

@Test
public void assertCommandForColorTemperatureAbsChannel4500Kelvin() {
public void assertDecimalTypeCommandForColorTemperatureAbsChannel4500Kelvin() {
String expectedReply = "{\"ct\" : 222, \"transitiontime\" : 4}";
assertSendCommandForColorTempAbs(new DecimalType(4500), new HueLightState(), expectedReply);
}

@Test
public void assertCommandForColorTemperatureAbsChannel2000Kelvin() {
public void assertDecimalTypeCommandForColorTemperatureAbsChannel2000Kelvin() {
String expectedReply = "{\"ct\" : 500, \"transitiontime\" : 4}";
assertSendCommandForColorTempAbs(new DecimalType(2000), new HueLightState(), expectedReply);
}

@Test
public void assertQuantityTypeCommandForColorTemperatureAbsChannel6500Kelvin() {
String expectedReply = "{\"ct\" : 153, \"transitiontime\" : 4}";
assertSendCommandForColorTempAbs(new QuantityType<>(6500, Units.KELVIN), new HueLightState(), expectedReply);
}

@Test
public void assertQuantityTypeCommandForColorTemperatureAbsChannel4500Kelvin() {
String expectedReply = "{\"ct\" : 222, \"transitiontime\" : 4}";
assertSendCommandForColorTempAbs(new QuantityType<>(4500, Units.KELVIN), new HueLightState(), expectedReply);
}

@Test
public void assertQuantityTypeCommandForColorTemperatureAbsChannel2000Kelvin() {
String expectedReply = "{\"ct\" : 500, \"transitiontime\" : 4}";
assertSendCommandForColorTempAbs(new QuantityType<>(2000, Units.KELVIN), new HueLightState(), expectedReply);
}

@Test
public void assertQuantityTypeCommandForColorTemperatureAbsChannel500Mired() {
String expectedReply = "{\"ct\" : 500, \"transitiontime\" : 4}";
assertSendCommandForColorTempAbs(new QuantityType<>(500, Units.MIRED), new HueLightState(), expectedReply);
}

@Test
public void assertPercentageValueOfColorTemperatureWhenCt153() {
int expectedReply = 0;
Expand Down Expand Up @@ -250,7 +276,7 @@ public void assertXYCommandForColorChannelColorful() {
}

@Test
public void asserCommandForColorChannelIncrease() {
public void assertCommandForColorChannelIncrease() {
HueLightState currentState = new HueLightState().bri(1).on(false);
String expectedReply = "{\"bri\" : 30, \"on\" : true, \"transitiontime\" : 4}";
assertSendCommandForColor(IncreaseDecreaseType.INCREASE, currentState, expectedReply);
Expand All @@ -265,7 +291,7 @@ public void asserCommandForColorChannelIncrease() {
}

@Test
public void asserCommandForColorChannelDecrease() {
public void assertCommandForColorChannelDecrease() {
HueLightState currentState = new HueLightState().bri(200);
String expectedReply = "{\"bri\" : 170, \"transitiontime\" : 4}";
assertSendCommandForColor(IncreaseDecreaseType.DECREASE, currentState, expectedReply);
Expand Down