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

* Allow handling of QuantityType for color temperature channel
* Fixed log messages and reduce log level.

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2022-12-21 20:10:21 +01:00 committed by GitHub
parent 528140d7ad
commit a0c2c76fd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 21 deletions

View File

@ -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 | | 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 | | 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 | 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 | | 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 | | 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 | | dimmer_switch | Number | This channel shows which button was last pressed on the dimmer switch. | 0820 |

View File

@ -35,7 +35,9 @@ import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.IncreaseDecreaseType; import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType; 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.types.StringType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing; import org.openhab.core.thing.Thing;
@ -225,8 +227,18 @@ public class HueGroupHandler extends BaseThingHandler implements HueLightActions
} }
break; break;
case CHANNEL_COLORTEMPERATURE_ABS: case CHANNEL_COLORTEMPERATURE_ABS:
if (command instanceof DecimalType) { if (command instanceof QuantityType) {
newState = LightStateConverter.toColorTemperatureLightState((DecimalType) command, 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); colorTemperatureCapabilties);
newState.setTransitionTime(fadeTime); newState.setTransitionTime(fadeTime);
} }
@ -284,13 +296,14 @@ public class HueGroupHandler extends BaseThingHandler implements HueLightActions
} }
break; break;
default: default:
logger.debug("Command sent to an unknown channel id: {}:{}", getThing().getUID(), channel);
break; break;
} }
if (newState != null) { if (newState != null) {
cacheNewState(newState); cacheNewState(newState);
bridgeHandler.updateGroupState(group, newState, fadeTime); bridgeHandler.updateGroupState(group, newState, fadeTime);
} else { } 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);
} }
} }

View File

@ -34,7 +34,9 @@ import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.IncreaseDecreaseType; import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType; 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.types.StringType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing; import org.openhab.core.thing.Thing;
@ -267,8 +269,18 @@ public class HueLightHandler extends BaseThingHandler implements HueLightActions
} }
break; break;
case CHANNEL_COLORTEMPERATURE_ABS: case CHANNEL_COLORTEMPERATURE_ABS:
if (command instanceof DecimalType) { if (command instanceof QuantityType) {
newState = LightStateConverter.toColorTemperatureLightState((DecimalType) command, 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); colorTemperatureCapabilties);
newState.setTransitionTime(fadeTime); newState.setTransitionTime(fadeTime);
} }
@ -356,6 +368,9 @@ public class HueLightHandler extends BaseThingHandler implements HueLightActions
newState = LightStateConverter.toOnOffEffectState((OnOffType) command); newState = LightStateConverter.toOnOffEffectState((OnOffType) command);
} }
break; break;
default:
logger.debug("Command sent to an unknown channel id: {}:{}", getThing().getUID(), channel);
break;
} }
if (newState != null) { if (newState != null) {
// Cache values which we have sent // Cache values which we have sent
@ -369,7 +384,7 @@ public class HueLightHandler extends BaseThingHandler implements HueLightActions
} }
bridgeHandler.updateLightState(this, light, newState, fadeTime); bridgeHandler.updateLightState(this, light, newState, fadeTime);
} else { } 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);
} }
} }

View File

@ -12,6 +12,8 @@
*/ */
package org.openhab.binding.hue.internal.handler; package org.openhab.binding.hue.internal.handler;
import javax.measure.quantity.Temperature;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.hue.internal.dto.ColorTemperature; import org.openhab.binding.hue.internal.dto.ColorTemperature;
@ -25,7 +27,9 @@ import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.IncreaseDecreaseType; import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType; 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.types.StringType;
import org.openhab.core.library.unit.Units;
/** /**
* The {@link LightStateConverter} is responsible for mapping to/from jue types. * The {@link LightStateConverter} is responsible for mapping to/from jue types.
@ -161,14 +165,14 @@ public class LightStateConverter {
} }
/** /**
* Transforms the given {@link DecimalType} into a light state containing * Transforms the given color temperature in Kelvin into a Hue Light {@link State}.
* the color temperature in Kelvin.
* *
* @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 * @return light state containing the color temperature
*/ */
public static StateUpdate toColorTemperatureLightState(DecimalType decimalType, ColorTemperature capabilities) { public static StateUpdate toColorTemperatureLightState(int kelvinValue, ColorTemperature capabilities) {
return new StateUpdate().setColorTemperature(kelvinToMired(decimalType.intValue()), capabilities); return new StateUpdate().setColorTemperature(kelvinToMired(kelvinValue), capabilities);
} }
/** /**
@ -207,14 +211,14 @@ public class LightStateConverter {
} }
/** /**
* Transforms Hue Light {@link State} into {@link DecimalType} representing * Transforms Hue Light {@link State} into {@link QuantityType} representing
* the color temperature in Kelvin. * the color temperature in Kelvin.
* *
* @param lightState light state * @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) { public static QuantityType<Temperature> toColorTemperature(State lightState) {
return new DecimalType(miredToKelvin(lightState.getColorTemperature())); return new QuantityType<>(miredToKelvin(lightState.getColorTemperature()), Units.KELVIN);
} }
/** /**

View File

@ -33,7 +33,9 @@ import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.IncreaseDecreaseType; import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType; 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.types.StringType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing; import org.openhab.core.thing.Thing;
@ -148,23 +150,47 @@ public class HueLightHandlerTest {
} }
@Test @Test
public void assertCommandForColorTemperatureAbsChannel6500Kelvin() { public void assertDecimalTypeCommandForColorTemperatureAbsChannel6500Kelvin() {
String expectedReply = "{\"ct\" : 153, \"transitiontime\" : 4}"; String expectedReply = "{\"ct\" : 153, \"transitiontime\" : 4}";
assertSendCommandForColorTempAbs(new DecimalType(6500), new HueLightState(), expectedReply); assertSendCommandForColorTempAbs(new DecimalType(6500), new HueLightState(), expectedReply);
} }
@Test @Test
public void assertCommandForColorTemperatureAbsChannel4500Kelvin() { public void assertDecimalTypeCommandForColorTemperatureAbsChannel4500Kelvin() {
String expectedReply = "{\"ct\" : 222, \"transitiontime\" : 4}"; String expectedReply = "{\"ct\" : 222, \"transitiontime\" : 4}";
assertSendCommandForColorTempAbs(new DecimalType(4500), new HueLightState(), expectedReply); assertSendCommandForColorTempAbs(new DecimalType(4500), new HueLightState(), expectedReply);
} }
@Test @Test
public void assertCommandForColorTemperatureAbsChannel2000Kelvin() { public void assertDecimalTypeCommandForColorTemperatureAbsChannel2000Kelvin() {
String expectedReply = "{\"ct\" : 500, \"transitiontime\" : 4}"; String expectedReply = "{\"ct\" : 500, \"transitiontime\" : 4}";
assertSendCommandForColorTempAbs(new DecimalType(2000), new HueLightState(), expectedReply); 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 @Test
public void assertPercentageValueOfColorTemperatureWhenCt153() { public void assertPercentageValueOfColorTemperatureWhenCt153() {
int expectedReply = 0; int expectedReply = 0;
@ -250,7 +276,7 @@ public class HueLightHandlerTest {
} }
@Test @Test
public void asserCommandForColorChannelIncrease() { public void assertCommandForColorChannelIncrease() {
HueLightState currentState = new HueLightState().bri(1).on(false); HueLightState currentState = new HueLightState().bri(1).on(false);
String expectedReply = "{\"bri\" : 30, \"on\" : true, \"transitiontime\" : 4}"; String expectedReply = "{\"bri\" : 30, \"on\" : true, \"transitiontime\" : 4}";
assertSendCommandForColor(IncreaseDecreaseType.INCREASE, currentState, expectedReply); assertSendCommandForColor(IncreaseDecreaseType.INCREASE, currentState, expectedReply);
@ -265,7 +291,7 @@ public class HueLightHandlerTest {
} }
@Test @Test
public void asserCommandForColorChannelDecrease() { public void assertCommandForColorChannelDecrease() {
HueLightState currentState = new HueLightState().bri(200); HueLightState currentState = new HueLightState().bri(200);
String expectedReply = "{\"bri\" : 170, \"transitiontime\" : 4}"; String expectedReply = "{\"bri\" : 170, \"transitiontime\" : 4}";
assertSendCommandForColor(IncreaseDecreaseType.DECREASE, currentState, expectedReply); assertSendCommandForColor(IncreaseDecreaseType.DECREASE, currentState, expectedReply);