]> git.basschouten.com Git - openhab-addons.git/commitdiff
[deconz] support for IncreaseDecreaseType commands (#8943)
authorJ-N-K <J-N-K@users.noreply.github.com>
Tue, 3 Nov 2020 22:10:33 +0000 (23:10 +0100)
committerGitHub <noreply@github.com>
Tue, 3 Nov 2020 22:10:33 +0000 (14:10 -0800)
* support for IncreaseDecreaseType commands
* fix spotless

Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
bundles/org.openhab.binding.deconz/src/main/java/org/openhab/binding/deconz/internal/BindingConstants.java
bundles/org.openhab.binding.deconz/src/main/java/org/openhab/binding/deconz/internal/Util.java
bundles/org.openhab.binding.deconz/src/main/java/org/openhab/binding/deconz/internal/handler/LightThingHandler.java

index 123f86000c8a14619831c4a73a69648e365092e0..4e5f12cbf330afb40534e05eaf2eaca018a4c2cb 100644 (file)
@@ -13,6 +13,7 @@
 package org.openhab.binding.deconz.internal;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.openhab.core.library.types.PercentType;
 import org.openhab.core.thing.ThingTypeUID;
 
 /**
@@ -128,5 +129,7 @@ public class BindingConstants {
     public static final int ZCL_CT_INVALID = 65535; // 0xFFFF
 
     public static final double HUE_FACTOR = 65535 / 360.0;
-    public static final double BRIGHTNESS_FACTOR = 2.54;
+    public static final int BRIGHTNESS_MIN = 0;
+    public static final int BRIGHTNESS_MAX = 254;
+    public static final double BRIGHTNESS_FACTOR = BRIGHTNESS_MAX / PercentType.HUNDRED.intValue();
 }
index 3f662bdcb2528ee552ec6cdd5c2da590b13dd21e..35713268b94468f9136b2068dea02ccdc0012bb3 100644 (file)
@@ -69,13 +69,8 @@ public class Util {
      */
     public static PercentType toPercentType(int val) {
         int scaledValue = (int) Math.ceil(val / BRIGHTNESS_FACTOR);
-        if (scaledValue < 0 || scaledValue > 100) {
-            LOGGER.trace("received value {} (converted to {}). Coercing.", val, scaledValue);
-            scaledValue = scaledValue < 0 ? 0 : scaledValue;
-            scaledValue = scaledValue > 100 ? 100 : scaledValue;
-        }
-
-        return new PercentType(scaledValue);
+        return new PercentType(
+                Util.constrainToRange(scaledValue, PercentType.ZERO.intValue(), PercentType.HUNDRED.intValue()));
     }
 
     /**
index 8c788d901370dfb9a5c0fd27ec17e6acdac909db..6ba83ec2b755267b94fe8678230525db0b17f6f5 100644 (file)
@@ -29,12 +29,7 @@ import org.openhab.binding.deconz.internal.dto.LightMessage;
 import org.openhab.binding.deconz.internal.dto.LightState;
 import org.openhab.binding.deconz.internal.netutils.AsyncHttpClient;
 import org.openhab.binding.deconz.internal.types.ResourceType;
-import org.openhab.core.library.types.DecimalType;
-import org.openhab.core.library.types.HSBType;
-import org.openhab.core.library.types.OnOffType;
-import org.openhab.core.library.types.PercentType;
-import org.openhab.core.library.types.StopMoveType;
-import org.openhab.core.library.types.UpDownType;
+import org.openhab.core.library.types.*;
 import org.openhab.core.thing.ChannelUID;
 import org.openhab.core.thing.Thing;
 import org.openhab.core.thing.ThingStatus;
@@ -71,6 +66,7 @@ public class LightThingHandler extends DeconzBaseThingHandler<LightMessage> {
             THING_TYPE_WINDOW_COVERING, THING_TYPE_WARNING_DEVICE);
 
     private static final long DEFAULT_COMMAND_EXPIRY_TIME = 250; // in ms
+    private static final int BRIGHTNESS_DIM_STEP = 26; // ~ 10%
 
     private final Logger logger = LoggerFactory.getLogger(LightThingHandler.class);
 
@@ -151,6 +147,17 @@ public class LightThingHandler extends DeconzBaseThingHandler<LightMessage> {
             case CHANNEL_COLOR:
                 if (command instanceof OnOffType) {
                     newLightState.on = (command == OnOffType.ON);
+                } else if (command instanceof IncreaseDecreaseType) {
+                    // try to get best value for current brightness
+                    int oldBri = currentBri != null ? currentBri
+                            : (Boolean.TRUE.equals(currentOn) ? BRIGHTNESS_MAX : BRIGHTNESS_MIN);
+                    if (command.equals(IncreaseDecreaseType.INCREASE)) {
+                        newLightState.bri = Util.constrainToRange(oldBri + BRIGHTNESS_DIM_STEP, BRIGHTNESS_MIN,
+                                BRIGHTNESS_MAX);
+                    } else {
+                        newLightState.bri = Util.constrainToRange(oldBri - BRIGHTNESS_DIM_STEP, BRIGHTNESS_MIN,
+                                BRIGHTNESS_MAX);
+                    }
                 } else if (command instanceof HSBType) {
                     HSBType hsbCommand = (HSBType) command;
 
@@ -203,10 +210,10 @@ public class LightThingHandler extends DeconzBaseThingHandler<LightMessage> {
                 if (command instanceof UpDownType) {
                     newLightState.on = (command == UpDownType.DOWN);
                 } else if (command == StopMoveType.STOP) {
-                    if (currentOn != null && currentOn && currentBri != null && currentBri <= 254) {
+                    if (currentOn != null && currentOn && currentBri != null && currentBri <= BRIGHTNESS_MAX) {
                         // going down or currently stop (254 because of rounding error)
                         newLightState.on = true;
-                    } else if (currentOn != null && !currentOn && currentBri != null && currentBri > 0) {
+                    } else if (currentOn != null && !currentOn && currentBri != null && currentBri > BRIGHTNESS_MIN) {
                         // going up or currently stopped
                         newLightState.on = false;
                     }