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

[lifx] Handle and provide QuantityType for color-temperature-abs channel #14025

Merged
merged 3 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 13 additions & 13 deletions bundles/org.openhab.binding.lifx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,19 @@ Thing lifx:colorirlight:porch [ host="10.120.130.4", fadetime=0 ]

All devices support some of the following channels:

| Channel Type ID | Item Type | Description | Thing Types |
|--------------------|-----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------|
| abstemperature | Number | This channel supports adjusting the color temperature in Kelvin. | colorlight, colorhevlight, colorirlight, colormzlight, tilelight, whitelight |
| abstemperaturezone | Number | This channel supports adjusting the zone color temperature in Kelvin. | colormzlight |
| brightness | Dimmer | This channel supports adjusting the brightness value. | whitelight |
| color | Color | This channel supports full color control with hue, saturation and brightness values. | colorlight, colorhevlight, colorirlight, colormzlight, tilelight |
| colorzone | Color | This channel supports full zone color control with hue, saturation and brightness values. | colormzlight |
| effect | String | This channel represents a type of light effect (e.g. for tile light: off, morph, flame) | tilelight |
| hevcycle | Switch | This channel supports starting and stopping the HEV clean cycle. | colorhevlight |
| infrared | Dimmer | This channel supports adjusting the infrared value. _Note:_ IR capable lights only activate their infrared LEDs when the brightness drops below a certain level. | colorirlight |
| signalstrength | Number | This channel represents signal strength with values 0, 1, 2, 3 or 4; 0 being worst strength and 4 being best strength. | colorlight, colorhevlight, colorirlight, colormzlight, tilelight, whitelight |
| temperature | Dimmer | This channel supports adjusting the color temperature from cold (0%) to warm (100%). | colorlight, colorhevlight, colorirlight, colormzlight, tilelight, whitelight |
| temperaturezone | Dimmer | This channel supports adjusting the zone color temperature from cold (0%) to warm (100%). | colormzlight |
| Channel Type ID | Item Type | Description | Thing Types |
|--------------------|--------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------|
| abstemperature | Number:Temperature | This channel supports adjusting the color temperature in Kelvin. | colorlight, colorhevlight, colorirlight, colormzlight, tilelight, whitelight |
| abstemperaturezone | Number:Temperature | This channel supports adjusting the zone color temperature in Kelvin. | colormzlight |
lolodomo marked this conversation as resolved.
Show resolved Hide resolved
| brightness | Dimmer | This channel supports adjusting the brightness value. | whitelight |
| color | Color | This channel supports full color control with hue, saturation and brightness values. | colorlight, colorhevlight, colorirlight, colormzlight, tilelight |
| colorzone | Color | This channel supports full zone color control with hue, saturation and brightness values. | colormzlight |
| effect | String | This channel represents a type of light effect (e.g. for tile light: off, morph, flame) | tilelight |
| hevcycle | Switch | This channel supports starting and stopping the HEV clean cycle. | colorhevlight |
| infrared | Dimmer | This channel supports adjusting the infrared value. _Note:_ IR capable lights only activate their infrared LEDs when the brightness drops below a certain level. | colorirlight |
| signalstrength | Number | This channel represents signal strength with values 0, 1, 2, 3 or 4; 0 being worst strength and 4 being best strength. | colorlight, colorhevlight, colorirlight, colormzlight, tilelight, whitelight |
| temperature | Dimmer | This channel supports adjusting the color temperature from cold (0%) to warm (100%). | colorlight, colorhevlight, colorirlight, colormzlight, tilelight, whitelight |
| temperaturezone | Dimmer | This channel supports adjusting the zone color temperature from cold (0%) to warm (100%). | colormzlight |

The _color_ and _brightness_ channels have a "Power On Brightness" configuration option that is used to determine the brightness when a light is switched on.
When it is left empty, the brightness of a light remains unchanged when a light is switched on or off.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,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.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
Expand Down Expand Up @@ -182,7 +184,7 @@ private void updateColorChannels(@Nullable PowerState powerState, HSBK[] colors)
updateStateIfChanged(CHANNEL_BRIGHTNESS, hsb.getBrightness());
updateStateIfChanged(CHANNEL_TEMPERATURE,
kelvinToPercentType(updateColor.getKelvin(), features.getTemperatureRange()));
updateStateIfChanged(CHANNEL_ABS_TEMPERATURE, new DecimalType(updateColor.getKelvin()));
updateStateIfChanged(CHANNEL_ABS_TEMPERATURE, new QuantityType(updateColor.getKelvin(), Units.KELVIN));

updateZoneChannels(powerState, colors);
}
Expand Down Expand Up @@ -241,7 +243,8 @@ private void updateZoneChannels(@Nullable PowerState powerState, HSBK[] colors)
updateStateIfChanged(CHANNEL_COLOR_ZONE + i, updateColor.getHSB());
updateStateIfChanged(CHANNEL_TEMPERATURE_ZONE + i,
kelvinToPercentType(updateColor.getKelvin(), features.getTemperatureRange()));
updateStateIfChanged(CHANNEL_ABS_TEMPERATURE_ZONE + i, new DecimalType(updateColor.getKelvin()));
updateStateIfChanged(CHANNEL_ABS_TEMPERATURE_ZONE + i,
new QuantityType(updateColor.getKelvin(), Units.KELVIN));
}
}
}
Expand Down Expand Up @@ -558,8 +561,9 @@ public void handleCommand(ChannelUID channelUID, Command command) {
switch (channelUID.getId()) {
case CHANNEL_ABS_TEMPERATURE:
case CHANNEL_TEMPERATURE:
if (command instanceof DecimalType) {
return () -> handleTemperatureCommand((DecimalType) command);
if (command instanceof DecimalType || (command instanceof QuantityType
&& ((QuantityType) command).toInvertibleUnit(Units.KELVIN) != null)) {
return () -> handleTemperatureCommand(command);
} else if (command instanceof IncreaseDecreaseType) {
return () -> handleIncreaseDecreaseTemperatureCommand((IncreaseDecreaseType) command);
}
Expand Down Expand Up @@ -599,8 +603,9 @@ public void handleCommand(ChannelUID channelUID, Command command) {
try {
if (channelUID.getId().startsWith(CHANNEL_ABS_TEMPERATURE_ZONE)) {
int zoneIndex = Integer.parseInt(channelUID.getId().replace(CHANNEL_ABS_TEMPERATURE_ZONE, ""));
if (command instanceof DecimalType) {
return () -> handleTemperatureCommand((DecimalType) command, zoneIndex);
if (command instanceof DecimalType || (command instanceof QuantityType
&& ((QuantityType) command).toInvertibleUnit(Units.KELVIN) != null)) {
return () -> handleTemperatureCommand(command, zoneIndex);
}
} else if (channelUID.getId().startsWith(CHANNEL_COLOR_ZONE)) {
int zoneIndex = Integer.parseInt(channelUID.getId().replace(CHANNEL_COLOR_ZONE, ""));
Expand Down Expand Up @@ -670,14 +675,14 @@ private void handleRefreshCommand(ChannelUID channelUID) {
}
}

private void handleTemperatureCommand(DecimalType temperature) {
private void handleTemperatureCommand(Command temperature) {
HSBK newColor = getLightStateForCommand().getColor();
newColor.setSaturation(PercentType.ZERO);
newColor.setKelvin(commandToKelvin(temperature, features.getTemperatureRange()));
getLightStateForCommand().setColor(newColor);
}

private void handleTemperatureCommand(DecimalType temperature, int zoneIndex) {
private void handleTemperatureCommand(Command temperature, int zoneIndex) {
HSBK newColor = getLightStateForCommand().getColor(zoneIndex);
newColor.setSaturation(PercentType.ZERO);
newColor.setKelvin(commandToKelvin(temperature, features.getTemperatureRange()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.types.Command;

/**
* Utility class for sharing message utility methods between objects.
Expand Down Expand Up @@ -102,9 +105,17 @@ public static PercentType kelvinToPercentType(int kelvin, TemperatureRange tempe
return new PercentType(value);
}

public static int commandToKelvin(DecimalType temperature, TemperatureRange temperatureRange) {
return temperature instanceof PercentType ? percentTypeToKelvin((PercentType) temperature, temperatureRange)
: decimalTypeToKelvin(temperature, temperatureRange);
public static int commandToKelvin(Command temperature, TemperatureRange temperatureRange) {
if (temperature instanceof PercentType) {
return percentTypeToKelvin((PercentType) temperature, temperatureRange);
} else if (temperature instanceof QuantityType) {
return quantityTypeToKelvin((QuantityType) temperature, temperatureRange);
} else if (temperature instanceof DecimalType) {
return decimalTypeToKelvin((DecimalType) temperature, temperatureRange);
} else {
assert false; // we've already done a type check before calling this method
return 0;
wborn marked this conversation as resolved.
Show resolved Hide resolved
}
}

public static int decimalTypeToKelvin(DecimalType temperature, TemperatureRange temperatureRange) {
Expand All @@ -117,6 +128,13 @@ public static int percentTypeToKelvin(PercentType temperature, TemperatureRange
temperatureRange.getMaximum() - (temperature.floatValue() * (temperatureRange.getRange() / 100)));
}

public static int quantityTypeToKelvin(QuantityType temperature, TemperatureRange temperatureRange) {
QuantityType<?> asKelvin = temperature.toInvertibleUnit(Units.KELVIN);
assert asKelvin != null;

return asKelvin.toBigDecimal().intValue();
lolodomo marked this conversation as resolved.
Show resolved Hide resolved
}

public static PercentType infraredToPercentType(int infrared) {
return intToPercentType(infrared);
}
Expand Down