mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-25 14:55:55 +01:00
[hue] Shrink step size for increase/decrease commands (#16538)
A step size of 30 with a value range of 0..100 leads to only 4 steps, which additionally are spaced unevenly. Shrink the step size to 10, which yields 10 evenly spaced steps. While at it, also deduplicate the increase/decrease code, which had slightly different implementation in both branches. Signed-off-by: Danny Baumann <dannybaumann@web.de> Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
parent
8aff5e98dc
commit
975e2a0aa9
@ -67,7 +67,6 @@ import com.google.gson.annotations.SerializedName;
|
||||
@NonNullByDefault
|
||||
public class Resource {
|
||||
|
||||
public static final double PERCENT_DELTA = 30f;
|
||||
public static final MathContext PERCENT_MATH_CONTEXT = new MathContext(4, RoundingMode.HALF_UP);
|
||||
|
||||
/**
|
||||
|
@ -356,15 +356,9 @@ public class Clip2ThingHandler extends BaseThingHandler {
|
||||
break;
|
||||
|
||||
case CHANNEL_2_COLOR_TEMP_PERCENT:
|
||||
if (command instanceof IncreaseDecreaseType) {
|
||||
if (Objects.nonNull(cache)) {
|
||||
State current = cache.getColorTemperaturePercentState();
|
||||
if (current instanceof PercentType) {
|
||||
int sign = IncreaseDecreaseType.INCREASE == command ? 1 : -1;
|
||||
int percent = ((PercentType) current).intValue() + (sign * (int) Resource.PERCENT_DELTA);
|
||||
command = new PercentType(Math.min(100, Math.max(0, percent)));
|
||||
}
|
||||
}
|
||||
if (command instanceof IncreaseDecreaseType increaseDecreaseCommand && Objects.nonNull(cache)) {
|
||||
command = translateIncreaseDecreaseCommand(increaseDecreaseCommand,
|
||||
cache.getColorTemperaturePercentState());
|
||||
} else if (command instanceof OnOffType) {
|
||||
command = OnOffType.OFF == command ? PercentType.ZERO : PercentType.HUNDRED;
|
||||
}
|
||||
@ -386,16 +380,8 @@ public class Clip2ThingHandler extends BaseThingHandler {
|
||||
|
||||
case CHANNEL_2_BRIGHTNESS:
|
||||
putResource = Objects.nonNull(putResource) ? putResource : new Resource(lightResourceType);
|
||||
if (command instanceof IncreaseDecreaseType) {
|
||||
if (Objects.nonNull(cache)) {
|
||||
State current = cache.getBrightnessState();
|
||||
if (current instanceof PercentType) {
|
||||
int sign = IncreaseDecreaseType.INCREASE == command ? 1 : -1;
|
||||
double percent = ((PercentType) current).doubleValue() + (sign * Resource.PERCENT_DELTA);
|
||||
command = new PercentType(new BigDecimal(Math.min(100f, Math.max(0f, percent)),
|
||||
Resource.PERCENT_MATH_CONTEXT));
|
||||
}
|
||||
}
|
||||
if (command instanceof IncreaseDecreaseType increaseDecreaseCommand && Objects.nonNull(cache)) {
|
||||
command = translateIncreaseDecreaseCommand(increaseDecreaseCommand, cache.getBrightnessState());
|
||||
}
|
||||
if (command instanceof PercentType) {
|
||||
PercentType brightness = (PercentType) command;
|
||||
@ -547,6 +533,16 @@ public class Clip2ThingHandler extends BaseThingHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private Command translateIncreaseDecreaseCommand(IncreaseDecreaseType command, State currentValue) {
|
||||
if (currentValue instanceof PercentType currentPercent) {
|
||||
int delta = command == IncreaseDecreaseType.INCREASE ? 10 : -10;
|
||||
double newPercent = Math.min(100.0, Math.max(0.0, currentPercent.doubleValue() + delta));
|
||||
return new PercentType(new BigDecimal(newPercent, Resource.PERCENT_MATH_CONTEXT));
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
private void refreshAllChannels() {
|
||||
if (!updateDependenciesDone) {
|
||||
return;
|
||||
|
Loading…
Reference in New Issue
Block a user