[hue] improve QuantityType command (#17948)

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
This commit is contained in:
Andrew Fiddian-Green 2024-12-21 11:20:45 +00:00 committed by GitHub
parent da6dde5cad
commit 57577f42a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -22,8 +22,6 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import javax.measure.Unit;
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.api.dto.clip2.Alerts; import org.openhab.binding.hue.internal.api.dto.clip2.Alerts;
@ -97,23 +95,13 @@ public class Setters {
* @return the target resource. * @return the target resource.
*/ */
public static Resource setColorTemperatureAbsolute(Resource target, Command command, @Nullable Resource source) { public static Resource setColorTemperatureAbsolute(Resource target, Command command, @Nullable Resource source) {
QuantityType<?> mirek; QuantityType<?> mirekQuantity = null;
if (command instanceof QuantityType<?> quantity) { if (command instanceof QuantityType<?> genericQuantity) {
Unit<?> unit = quantity.getUnit(); mirekQuantity = genericQuantity.toInvertibleUnit(Units.MIRED);
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;
}
} else if (command instanceof DecimalType decimal) { } else if (command instanceof DecimalType decimal) {
mirek = QuantityType.valueOf(decimal.doubleValue(), Units.KELVIN).toInvertibleUnit(Units.MIRED); mirekQuantity = QuantityType.valueOf(decimal.intValue(), Units.KELVIN).toInvertibleUnit(Units.MIRED);
} else {
mirek = null;
} }
if (Objects.nonNull(mirek)) { if (Objects.nonNull(mirekQuantity)) {
MirekSchema schema = target.getMirekSchema(); MirekSchema schema = target.getMirekSchema();
schema = Objects.nonNull(schema) ? schema : Objects.nonNull(source) ? source.getMirekSchema() : null; schema = Objects.nonNull(schema) ? schema : Objects.nonNull(source) ? source.getMirekSchema() : null;
schema = Objects.nonNull(schema) ? schema : MirekSchema.DEFAULT_SCHEMA; schema = Objects.nonNull(schema) ? schema : MirekSchema.DEFAULT_SCHEMA;
@ -121,7 +109,7 @@ public class Setters {
colorTemperature = Objects.nonNull(colorTemperature) ? colorTemperature : new ColorTemperature(); colorTemperature = Objects.nonNull(colorTemperature) ? colorTemperature : new ColorTemperature();
double min = schema.getMirekMinimum(); double min = schema.getMirekMinimum();
double max = schema.getMirekMaximum(); 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)); target.setColorTemperature(colorTemperature.setMirek(val));
} }
return target; return target;