Skip to content

Commit afa65f2

Browse files
authored
[hue] Shrink step size for increase/decrease commands (openhab#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 <[email protected]>
1 parent 26fd8c6 commit afa65f2

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/api/dto/clip2/Resource.java

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
@NonNullByDefault
6868
public class Resource {
6969

70-
public static final double PERCENT_DELTA = 30f;
7170
public static final MathContext PERCENT_MATH_CONTEXT = new MathContext(4, RoundingMode.HALF_UP);
7271

7372
/**

bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/Clip2ThingHandler.java

+15-19
Original file line numberDiff line numberDiff line change
@@ -356,15 +356,9 @@ public void handleCommand(ChannelUID channelUID, Command commandParam) {
356356
break;
357357

358358
case CHANNEL_2_COLOR_TEMP_PERCENT:
359-
if (command instanceof IncreaseDecreaseType) {
360-
if (Objects.nonNull(cache)) {
361-
State current = cache.getColorTemperaturePercentState();
362-
if (current instanceof PercentType) {
363-
int sign = IncreaseDecreaseType.INCREASE == command ? 1 : -1;
364-
int percent = ((PercentType) current).intValue() + (sign * (int) Resource.PERCENT_DELTA);
365-
command = new PercentType(Math.min(100, Math.max(0, percent)));
366-
}
367-
}
359+
if (command instanceof IncreaseDecreaseType increaseDecreaseCommand && Objects.nonNull(cache)) {
360+
command = translateIncreaseDecreaseCommand(increaseDecreaseCommand,
361+
cache.getColorTemperaturePercentState());
368362
} else if (command instanceof OnOffType) {
369363
command = OnOffType.OFF == command ? PercentType.ZERO : PercentType.HUNDRED;
370364
}
@@ -386,16 +380,8 @@ public void handleCommand(ChannelUID channelUID, Command commandParam) {
386380

387381
case CHANNEL_2_BRIGHTNESS:
388382
putResource = Objects.nonNull(putResource) ? putResource : new Resource(lightResourceType);
389-
if (command instanceof IncreaseDecreaseType) {
390-
if (Objects.nonNull(cache)) {
391-
State current = cache.getBrightnessState();
392-
if (current instanceof PercentType) {
393-
int sign = IncreaseDecreaseType.INCREASE == command ? 1 : -1;
394-
double percent = ((PercentType) current).doubleValue() + (sign * Resource.PERCENT_DELTA);
395-
command = new PercentType(new BigDecimal(Math.min(100f, Math.max(0f, percent)),
396-
Resource.PERCENT_MATH_CONTEXT));
397-
}
398-
}
383+
if (command instanceof IncreaseDecreaseType increaseDecreaseCommand && Objects.nonNull(cache)) {
384+
command = translateIncreaseDecreaseCommand(increaseDecreaseCommand, cache.getBrightnessState());
399385
}
400386
if (command instanceof PercentType) {
401387
PercentType brightness = (PercentType) command;
@@ -547,6 +533,16 @@ public void handleCommand(ChannelUID channelUID, Command commandParam) {
547533
}
548534
}
549535

536+
private Command translateIncreaseDecreaseCommand(IncreaseDecreaseType command, State currentValue) {
537+
if (currentValue instanceof PercentType currentPercent) {
538+
int delta = command == IncreaseDecreaseType.INCREASE ? 10 : -10;
539+
double newPercent = Math.min(100.0, Math.max(0.0, currentPercent.doubleValue() + delta));
540+
return new PercentType(new BigDecimal(newPercent, Resource.PERCENT_MATH_CONTEXT));
541+
}
542+
543+
return command;
544+
}
545+
550546
private void refreshAllChannels() {
551547
if (!updateDependenciesDone) {
552548
return;

0 commit comments

Comments
 (0)