[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:
maniac103 2024-03-18 11:56:42 +01:00 committed by Ciprian Pascu
parent 8aff5e98dc
commit 975e2a0aa9
2 changed files with 15 additions and 20 deletions

View File

@ -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);
/**

View File

@ -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;