]> git.basschouten.com Git - openhab-addons.git/commitdiff
[hue] Shrink step size for increase/decrease commands (#16538)
authormaniac103 <dannybaumann@web.de>
Mon, 18 Mar 2024 10:56:42 +0000 (11:56 +0100)
committerGitHub <noreply@github.com>
Mon, 18 Mar 2024 10:56:42 +0000 (11:56 +0100)
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>
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/api/dto/clip2/Resource.java
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/Clip2ThingHandler.java

index f4560b84df057a183b7617c67f1ea77c8c13bc08..35c9c6863042fa45d1bb3ba5b516111ae7cd952d 100644 (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);
 
     /**
index d18d7ec1afca207827e92cf1061a0c9a58ed81fb..f66b05a85cdd096e4b73086a2f5613146ee9dca4 100644 (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;