Skip to content

Commit

Permalink
[dali] handle and provide QuantityType for color-temperature-abs chan…
Browse files Browse the repository at this point in the history
…nel (openhab#14021)

see openhab/openhab-core#3129

Signed-off-by: Cody Cutrer <cody@cutrer.us>
Signed-off-by: Andras Uhrin <andras.uhrin@gmail.com>
  • Loading branch information
ccutrer authored and andrasU committed Jan 6, 2024
1 parent 4caecfb commit c79ca46
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
* Copyright (c) 2010-2022 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
Expand Down Expand Up @@ -65,18 +65,18 @@ public void handleCommand(ChannelUID channelUID, Command command) {
throw new DaliException("unknown device type");
}
int mirek;
if (command instanceof DecimalType decimalCommand) {
if (command instanceof DecimalType) {
// Color temperature in DALI is represented in mirek ("reciprocal megakelvin")
// It is one million times the reciprocal of the color temperature (in Kelvin)
mirek = (int) (1E6f / (Math.min(Math.max(decimalCommand.intValue(), 1000), 20000)));
} else if (command instanceof QuantityType quantityCommand) {
mirek = (int) (1E6f / (Math.min(Math.max(((DecimalType) command).intValue(), 1000), 20000)));
} else if (command instanceof QuantityType) {
// ensure it's in the correct units
QuantityType<?> commandQuantity = quantityCommand.toInvertibleUnit(Units.MIRED);
QuantityType<?> commandQuantity = ((QuantityType) command).toInvertibleUnit(Units.MIRED);
if (commandQuantity == null) {
logger.warn("Unable to convert command {} to mireks", command);
return;
}
mirek = commandQuantity.intValue();
mirek = commandQuantity.toBigDecimal().intValue();
} else {
logger.warn("Unable to convert command {} to mireks", command);
return;
Expand Down Expand Up @@ -124,6 +124,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
logger.warn("Error querying device status: {}", e.getMessage());
return null;
});

} else if (CHANNEL_COLOR.equals(channelUID.getId())) {
DaliAddress address;
if (THING_TYPE_DEVICE_DT8.equals(this.thing.getThingTypeUID())) {
Expand All @@ -133,8 +134,8 @@ public void handleCommand(ChannelUID channelUID, Command command) {
} else {
throw new DaliException("unknown device type");
}
if (command instanceof HSBType hsbCommand) {
PercentType[] rgb = hsbCommand.toRGB();
if (command instanceof HSBType) {
PercentType[] rgb = ((HSBType) command).toRGB();
final int r = (int) (254 * (rgb[0].floatValue() / 100));
final int g = (int) (254 * (rgb[1].floatValue() / 100));
final int b = (int) (254 * (rgb[2].floatValue() / 100));
Expand Down Expand Up @@ -188,6 +189,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
logger.warn("Error querying device status: {}", e.getMessage());
return null;
});

} else {
super.handleCommand(channelUID, command);
}
Expand Down

0 comments on commit c79ca46

Please sign in to comment.