]> git.basschouten.com Git - openhab-addons.git/commitdiff
[avmfritz] Allow to set every userdefined color (#13317)
authorChristoph Weitkamp <github@christophweitkamp.de>
Sat, 3 Sep 2022 12:42:31 +0000 (14:42 +0200)
committerGitHub <noreply@github.com>
Sat, 3 Sep 2022 12:42:31 +0000 (14:42 +0200)
* Added INCREASE/DECREASE commands for brightness/color Channels
* Fixed NPE in HeatingModel
* Use set unmapped color command for color control of bulbs

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/dto/ColorControlModel.java
bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/dto/HeatingModel.java
bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/handler/AVMFritzBaseThingHandler.java
bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/hardware/FritzAhaWebInterface.java
bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/hardware/callbacks/FritzAhaSetColorCallback.java
bundles/org.openhab.binding.avmfritz/src/test/java/org/openhab/binding/avmfritz/internal/dto/AVMFritzDeviceListModelTest.java
bundles/org.openhab.binding.avmfritz/src/test/java/org/openhab/binding/avmfritz/internal/dto/ColorControlModelTest.java
bundles/org.openhab.binding.avmfritz/src/test/java/org/openhab/binding/avmfritz/internal/dto/HeatingModelTest.java

index 9a12ed6740b24ff9e91fd028c8fd47000ae0d39e..b77b63cd69b791ead325363727a2910e7b4765ab 100644 (file)
@@ -15,8 +15,10 @@ package org.openhab.binding.avmfritz.internal.dto;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 
+import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.core.library.types.PercentType;
 
 /**
@@ -36,6 +38,10 @@ public class ColorControlModel {
     public int currentMode;
     public int hue;
     public int saturation;
+    @XmlElement(name = "unmapped_hue")
+    public @Nullable Integer unmappedHue;
+    @XmlElement(name = "unmapped_saturation")
+    public @Nullable Integer unmappedSaturation;
     public int temperature;
 
     /**
@@ -62,7 +68,8 @@ public class ColorControlModel {
     @Override
     public String toString() {
         return new StringBuilder("[supportedModes=").append(supportedModes).append(",currentMode=").append(currentMode)
-                .append(",hue=").append(hue).append(",saturation=").append(saturation).append(",temperature=")
+                .append(",hue=").append(hue).append(",saturation=").append(saturation).append(",unmapped_hue=")
+                .append(unmappedHue).append(",unmapped_saturation=").append(unmappedSaturation).append(",temperature=")
                 .append(temperature).append("]").toString();
     }
 
index 424e2569382f59a247c8481473e62dc5b4a6716d..f5bd7e87225f1af9360d24349dd6e6f051264466 100644 (file)
@@ -111,9 +111,9 @@ public class HeatingModel implements BatteryModel {
             return MODE_OFF;
         } else if (BigDecimal.ONE.equals(getWindowopenactiv())) {
             return MODE_WINDOW_OPEN;
-        } else if (tsoll.compareTo(komfort) == 0) {
+        } else if (komfort != null && komfort.compareTo(tsoll) == 0) {
             return MODE_COMFORT;
-        } else if (tsoll.compareTo(absenk) == 0) {
+        } else if (absenk != null && absenk.compareTo(tsoll) == 0) {
             return MODE_ECO;
         } else if (BigDecimal.ONE.equals(getBoostactive()) || TEMP_FRITZ_MAX.compareTo(tsoll) == 0) {
             return MODE_BOOST;
index 3d404ec5588461265cd2772e991e65f3240815b7..6641749972c00a63d01f877b0be13ab463727fee 100644 (file)
@@ -411,12 +411,19 @@ public abstract class AVMFritzBaseThingHandler extends BaseThingHandler implemen
                 if (command instanceof HSBType) {
                     HSBType hsbType = (HSBType) command;
                     brightness = hsbType.getBrightness().toBigDecimal();
-                    fritzBox.setHueAndSaturation(ain, hsbType.getHue().intValue(),
+                    fritzBox.setUnmappedHueAndSaturation(ain, hsbType.getHue().intValue(),
                             ColorControlModel.fromPercent(hsbType.getSaturation()), 0);
                 } else if (command instanceof PercentType) {
                     brightness = ((PercentType) command).toBigDecimal();
                 } else if (command instanceof OnOffType) {
                     fritzBox.setSwitch(ain, OnOffType.ON.equals(command));
+                } else if (command instanceof IncreaseDecreaseType) {
+                    brightness = ((DeviceModel) currentDevice).getLevelControlModel().getLevelPercentage();
+                    if (IncreaseDecreaseType.INCREASE.equals(command)) {
+                        brightness.add(BigDecimal.TEN);
+                    } else {
+                        brightness.subtract(BigDecimal.TEN);
+                    }
                 }
                 if (brightness != null) {
                     fritzBox.setLevelPercentage(ain, brightness);
index d53b3817eea04f31e16fd99cd3a039447986a299..4dbb6a66a7b5abc3186b67dd8228919cb3019f22 100644 (file)
@@ -330,11 +330,16 @@ public class FritzAhaWebInterface {
         return asyncGet(callback);
     }
 
-    public FritzAhaContentExchange setHueAndSaturation(String ain, int hue, int saturation, int duration) {
+    public FritzAhaContentExchange setMappedHueAndSaturation(String ain, int hue, int saturation, int duration) {
         FritzAhaSetColorCallback callback = new FritzAhaSetColorCallback(this, ain, hue, saturation, duration);
         return asyncGet(callback);
     }
 
+    public FritzAhaContentExchange setUnmappedHueAndSaturation(String ain, int hue, int saturation, int duration) {
+        FritzAhaSetColorCallback callback = new FritzAhaSetColorCallback(this, ain, hue, saturation, duration, false);
+        return asyncGet(callback);
+    }
+
     public FritzAhaContentExchange setBlind(String ain, BlindCommand command) {
         FritzAhaSetBlindTargetCallback callback = new FritzAhaSetBlindTargetCallback(this, ain, command);
         return asyncGet(callback);
index 66dea6b7e5182066f83e498147fb3f96ab0b4bc6..ed78836f58a759c2201b862e19c824b1adfd5416 100644 (file)
@@ -27,6 +27,9 @@ import org.slf4j.LoggerFactory;
 @NonNullByDefault
 public class FritzAhaSetColorCallback extends FritzAhaReauthCallback {
 
+    private static final String WEBSERVICE_SET_COLOR_COMMAND = "switchcmd=setcolor";
+    private static final String WEBSERVICE_SET_UNMAPPED_COLOR_COMMAND = "switchcmd=setunmappedcolor";
+
     private final Logger logger = LoggerFactory.getLogger(FritzAhaSetColorCallback.class);
 
     private final String ain;
@@ -41,8 +44,25 @@ public class FritzAhaSetColorCallback extends FritzAhaReauthCallback {
      * @param duration Duration of the change in 100ms. 0 immediately.
      */
     public FritzAhaSetColorCallback(FritzAhaWebInterface webIface, String ain, int hue, int saturation, int duration) {
+        this(webIface, ain, hue, saturation, duration, true);
+    }
+
+    /**
+     * Constructor
+     *
+     * @param webIface Interface to FRITZ!Box
+     * @param ain AIN of the device that should be switched
+     * @param hue New hue
+     * @param saturation New saturation
+     * @param duration Duration of the change in 100ms. 0 immediately.
+     * @param mapped Use mapped or unmapped color command
+     */
+    public FritzAhaSetColorCallback(FritzAhaWebInterface webIface, String ain, int hue, int saturation, int duration,
+            boolean mapped) {
         super(WEBSERVICE_PATH,
-                "switchcmd=setcolor&ain=" + ain + "&hue=" + hue + "&saturation=" + saturation + "&duration=" + duration,
+                mapped ? WEBSERVICE_SET_COLOR_COMMAND
+                        : WEBSERVICE_SET_UNMAPPED_COLOR_COMMAND + "&ain=" + ain + "&hue=" + hue + "&saturation="
+                                + saturation + "&duration=" + duration,
                 webIface, GET, 1);
         this.ain = ain;
     }
index d6ca008c246607b9f61483491659dea34304bc2b..b479d8d1af67d2a178fbe79e94e0daea7bdcfa23 100644 (file)
@@ -725,6 +725,8 @@ public class AVMFritzDeviceListModelTest {
         assertNotNull(colorModel);
         assertEquals(254, colorModel.hue);
         assertEquals(100, colorModel.saturation);
+        assertEquals(0, colorModel.unmappedHue);
+        assertEquals(0, colorModel.unmappedSaturation);
         assertEquals(2700, colorModel.temperature);
     }
 
index 4d7ec7255e860bdf0d9bfd57c6d500016872585b..82060850794302bb6c15e764b267c2fe2e5c5603 100644 (file)
@@ -37,6 +37,16 @@ class ColorControlModelTest {
         }
     }
 
+    @Test
+    public void testColorControlModelPercentConversionRestrictsToLowerBounds() {
+        assertThat(ColorControlModel.toPercent(-1), is(PercentType.ZERO));
+    }
+
+    @Test
+    public void testColorControlModelPercentConversionRestrictsToUpperBounds() {
+        assertThat(ColorControlModel.toPercent(999), is(PercentType.HUNDRED));
+    }
+
     @Test
     public void hsbSaturationAlwaysGreaterThanZero() {
         // a saturation greater than 1 should result in a percentage greater than 1
index 0ea218247911a3e26b1c785bc888f192f9338b0f..90590b5877324a89a3738e9e8f02a6ef40cbc806 100644 (file)
@@ -12,7 +12,8 @@
  */
 package org.openhab.binding.avmfritz.internal.dto;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.*;
 
 import java.math.BigDecimal;
 
@@ -60,4 +61,16 @@ public class HeatingModelTest {
         assertEquals(BIGDECIMAL_FOURTEEN_POINT_FIVE, HeatingModel.normalizeCelsius(new BigDecimal("14.4")));
         assertEquals(BIGDECIMAL_FOURTEEN_POINT_FIVE, HeatingModel.normalizeCelsius(new BigDecimal("14.6")));
     }
+
+    @Test
+    public void validateGetRadiatorModeReturnsValidMode() {
+        HeatingModel heatingModel = new HeatingModel();
+        assertEquals(MODE_UNKNOWN, heatingModel.getRadiatorMode());
+
+        heatingModel.setTsoll(BigDecimal.ONE);
+        assertEquals(MODE_ON, heatingModel.getRadiatorMode());
+
+        heatingModel.setKomfort(BigDecimal.ONE);
+        assertEquals(MODE_COMFORT, heatingModel.getRadiatorMode());
+    }
 }