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

[amazonechocontrol] Support QuantityType Color Temperature command #17919

Merged
merged 5 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeCapabilities.SmartHomeCapability;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
import org.openhab.core.library.types.DecimalType;
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.DefaultSystemChannelTypeProvider;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.types.Command;
Expand Down Expand Up @@ -130,15 +132,21 @@ public boolean handleCommand(Connection connection, SmartHomeDevice shd, String
if (channelId.equals(COLOR_TEMPERATURE_IN_KELVIN.channelId)) {
// WRITING TO THIS CHANNEL DOES CURRENTLY NOT WORK, BUT WE LEAVE THE CODE FOR FUTURE USE!
if (containsCapabilityProperty(capabilities, COLOR_TEMPERATURE_IN_KELVIN.propertyName)) {
if (command instanceof DecimalType) {
int intValue = ((DecimalType) command).intValue();
if (intValue < 1000) {
intValue = 1000;
QuantityType<?> kelvinQuantity = null;
if (command instanceof QuantityType<?> genericQuantity) {
kelvinQuantity = genericQuantity.toInvertibleUnit(Units.KELVIN);
} else if (command instanceof DecimalType decimal) {
kelvinQuantity = QuantityType.valueOf(decimal.intValue(), Units.KELVIN);
}
if (kelvinQuantity != null) {
int kelvin = kelvinQuantity.intValue();
if (kelvin < 1000) {
kelvin = 1000;
}
if (intValue > 10000) {
intValue = 10000;
if (kelvin > 10000) {
kelvin = 10000;
}
connection.smartHomeCommand(entityId, "setColorTemperature", "colorTemperatureInKelvin", intValue);
connection.smartHomeCommand(entityId, "setColorTemperature", "colorTemperatureInKelvin", kelvin);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import static org.openhab.binding.deconz.internal.BindingConstants.*;
import static org.openhab.binding.deconz.internal.Util.constrainToRange;
import static org.openhab.binding.deconz.internal.Util.kelvinToMired;

import java.util.Collection;
import java.util.Map;
Expand All @@ -36,7 +35,9 @@
import org.openhab.core.library.types.HSBType;
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 @@ -139,9 +140,15 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}
}
case CHANNEL_COLOR_TEMPERATURE -> {
if (command instanceof DecimalType decimalCommand) {
int miredValue = kelvinToMired(decimalCommand.intValue());
newGroupAction.ct = constrainToRange(miredValue, ZCL_CT_MIN, ZCL_CT_MAX);
QuantityType<?> miredQuantity = null;
if (command instanceof QuantityType<?> genericQuantity) {
miredQuantity = genericQuantity.toInvertibleUnit(Units.MIRED);
} else if (command instanceof DecimalType decimal) {
miredQuantity = QuantityType.valueOf(decimal.intValue(), Units.KELVIN)
.toInvertibleUnit(Units.MIRED);
}
if (miredQuantity != null) {
newGroupAction.ct = constrainToRange(miredQuantity.intValue(), ZCL_CT_MIN, ZCL_CT_MAX);
newGroupAction.on = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,15 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}
}
case CHANNEL_COLOR_TEMPERATURE -> {
if (command instanceof DecimalType) {
int miredValue = kelvinToMired(((DecimalType) command).intValue());
newLightState.ct = constrainToRange(miredValue, ctMin, ctMax);
QuantityType<?> miredQuantity = null;
if (command instanceof QuantityType<?> genericQuantity) {
miredQuantity = genericQuantity.toInvertibleUnit(Units.MIRED);
} else if (command instanceof DecimalType decimal) {
miredQuantity = QuantityType.valueOf(decimal.intValue(), Units.KELVIN)
.toInvertibleUnit(Units.MIRED);
}
if (miredQuantity != null) {
newLightState.ct = constrainToRange(miredQuantity.intValue(), ctMin, ctMax);
newLightState.on = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.Objects;
import java.util.Set;

import javax.measure.Unit;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.hue.internal.api.dto.clip2.Alerts;
Expand Down Expand Up @@ -97,31 +95,21 @@ public static Resource setAlert(Resource target, Command command, @Nullable Reso
* @return the target resource.
*/
public static Resource setColorTemperatureAbsolute(Resource target, Command command, @Nullable Resource source) {
QuantityType<?> mirek;
if (command instanceof QuantityType<?> quantity) {
Unit<?> unit = quantity.getUnit();
if (Units.KELVIN.equals(unit)) {
mirek = quantity.toInvertibleUnit(Units.MIRED);
} else if (Units.MIRED.equals(unit)) {
mirek = quantity;
} else {
QuantityType<?> kelvin = quantity.toInvertibleUnit(Units.KELVIN);
mirek = Objects.nonNull(kelvin) ? kelvin.toInvertibleUnit(Units.MIRED) : null;
}
QuantityType<?> mirekQuantity = null;
if (command instanceof QuantityType<?> genericQuantity) {
mirekQuantity = genericQuantity.toInvertibleUnit(Units.MIRED);
} else if (command instanceof DecimalType decimal) {
mirek = QuantityType.valueOf(decimal.doubleValue(), Units.KELVIN).toInvertibleUnit(Units.MIRED);
} else {
mirek = null;
mirekQuantity = QuantityType.valueOf(decimal.intValue(), Units.KELVIN).toInvertibleUnit(Units.MIRED);
}
if (Objects.nonNull(mirek)) {
if (Objects.nonNull(mirekQuantity)) {
MirekSchema schema = target.getMirekSchema();
schema = Objects.nonNull(schema) ? schema : Objects.nonNull(source) ? source.getMirekSchema() : null;
schema = Objects.nonNull(schema) ? schema : MirekSchema.DEFAULT_SCHEMA;
ColorTemperature colorTemperature = target.getColorTemperature();
colorTemperature = Objects.nonNull(colorTemperature) ? colorTemperature : new ColorTemperature();
double min = schema.getMirekMinimum();
double max = schema.getMirekMaximum();
double val = Math.max(min, Math.min(max, mirek.doubleValue()));
double val = Math.max(min, Math.min(max, mirekQuantity.doubleValue()));
target.setColorTemperature(colorTemperature.setMirek(val));
}
return target;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static int percentTypeToKelvin(PercentType temperature, TemperatureRange
temperatureRange.getMaximum() - (temperature.floatValue() * (temperatureRange.getRange() / 100)));
}

public static int quantityTypeToKelvin(QuantityType temperature, TemperatureRange temperatureRange) {
public static int quantityTypeToKelvin(QuantityType<?> temperature, TemperatureRange temperatureRange) {
QuantityType<?> asKelvin = temperature.toInvertibleUnit(Units.KELVIN);
if (asKelvin == null) {
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
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.ChannelUID;
Expand Down Expand Up @@ -187,6 +188,12 @@ public boolean handleDeviceCommand(ChannelUID channelUID, Command command) throw
} else if (command instanceof DecimalType decimalCommand) {
temp = decimalCommand.intValue();
logger.debug("{}: Set color temp to {}K (Integer)", thingName, temp);
} else if (command instanceof QuantityType<?> genericQuantity) {
QuantityType<?> kelvinQuantity = genericQuantity.toInvertibleUnit(Units.KELVIN);
if (kelvinQuantity != null) {
temp = kelvinQuantity.intValue();
logger.debug("{}: Set color temp to {}K (Integer)", thingName, temp);
}
}
validateRange(CHANNEL_COLOR_TEMP, temp, col.minTemp, col.maxTemp);
col.setTemp(temp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openhab.core.library.types.HSBType;
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.unit.Units;
import org.openhab.core.thing.ChannelGroupUID;
import org.openhab.core.thing.ChannelUID;
Expand Down Expand Up @@ -147,8 +148,14 @@ private void handleColorCommand(Command command) {
}

private void handleColorTempCommand(Command command) {
if (command instanceof DecimalType decimalCommand) {
setColorTemp(decimalCommand.intValue());
QuantityType<?> kelvinQuantity = null;
if (command instanceof QuantityType<?> genericQuantity) {
kelvinQuantity = genericQuantity.toInvertibleUnit(Units.KELVIN);
} else if (command instanceof DecimalType decimal) {
kelvinQuantity = QuantityType.valueOf(decimal.intValue(), Units.KELVIN);
}
if (kelvinQuantity != null) {
setColorTemp(kelvinQuantity.intValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.openhab.core.library.types.HSBType;
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.unit.Units;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
Expand All @@ -43,7 +44,7 @@ public class TapoLightStripHandler extends TapoBaseDeviceHandler {

/**
* Constructor
*
*
* @param thing Thing object representing device
*/
public TapoLightStripHandler(Thing thing) {
Expand All @@ -53,7 +54,7 @@ public TapoLightStripHandler(Thing thing) {
/**
* Function called by {@link org.openhab.binding.tapocontrol.internal.api.TapoDeviceConnector} if new data were
* received
*
*
* @param queryCommand command where new data belong to
*/
@Override
Expand All @@ -71,7 +72,7 @@ public void newDataResult(String queryCommand) {

/**
* handle command sent to device
*
*
* @param channelUID channelUID command is sent to
* @param command command to be sent
*/
Expand Down Expand Up @@ -128,8 +129,14 @@ private void handleColorCommand(Command command) {
}

private void handleColorTempCommand(Command command) {
if (command instanceof DecimalType decimalCommand) {
setColorTemp(decimalCommand.intValue());
QuantityType<?> kelvinQuantity = null;
if (command instanceof QuantityType<?> genericQuantity) {
kelvinQuantity = genericQuantity.toInvertibleUnit(Units.KELVIN);
} else if (command instanceof DecimalType decimal) {
kelvinQuantity = QuantityType.valueOf(decimal.intValue(), Units.KELVIN);
}
if (kelvinQuantity != null) {
setColorTemp(kelvinQuantity.intValue());
}
}

Expand Down Expand Up @@ -164,7 +171,7 @@ private void handleLightFx(String channel, Command command) {

/**
* Switch device On or Off
*
*
* @param on if true device will switch on. Otherwise switch off
*/
protected void switchOnOff(boolean on) {
Expand All @@ -174,7 +181,7 @@ protected void switchOnOff(boolean on) {

/**
* Set Britghtness of device
*
*
* @param newBrightness percentage 0-100 of new brightness
*/
protected void setBrightness(Integer newBrightness) {
Expand All @@ -190,7 +197,7 @@ protected void setBrightness(Integer newBrightness) {

/**
* Set Color of Device
*
*
* @param command HSBType
*/
protected void setColor(HSBType command) {
Expand All @@ -203,7 +210,7 @@ protected void setColor(HSBType command) {

/**
* Set ColorTemp
*
*
* @param colorTemp (Integer) in Kelvin
*/
protected void setColorTemp(Integer colorTemp) {
Expand All @@ -214,7 +221,7 @@ protected void setColorTemp(Integer colorTemp) {

/**
* Set light effect
*
*
* @param lightEffect TapoLightEffect
*/
protected void setLightEffect(TapoLightEffect lightEffect) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ public boolean handleCommand(final ChannelUID channelUid, final Command command)
response = handleOnOffType(channelId, onOffCommand, transitionPeriod);
} else if (command instanceof HSBType hsbCommand && CHANNEL_COLOR.equals(channelId)) {
response = handleHSBType(channelId, hsbCommand, transitionPeriod);
} else if (command instanceof QuantityType<?> genericQuantity
&& CHANNEL_COLOR_TEMPERATURE_ABS.equals(channelId)) {
QuantityType<?> kelvinQuantity = genericQuantity.toInvertibleUnit(Units.KELVIN);
if (kelvinQuantity == null) {
return false;
}
response = handleDecimalType(channelId, new DecimalType(kelvinQuantity.intValue()), transitionPeriod);
} else if (command instanceof DecimalType decimalCommand) {
response = handleDecimalType(channelId, decimalCommand, transitionPeriod);
} else {
Expand Down