]> git.basschouten.com Git - openhab-addons.git/commitdiff
[somfytahoma] Improved handling of target temperature command (#10336)
authorlolodomo <lg.hc@free.fr>
Sun, 21 Mar 2021 13:23:07 +0000 (14:23 +0100)
committerGitHub <noreply@github.com>
Sun, 21 Mar 2021 13:23:07 +0000 (14:23 +0100)
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBaseThingHandler.java
bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaElectricitySensorHandler.java
bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaValveHeatingSystemHandler.java
bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaZwaveHeatingSystemHandler.java

index 54ee305b3a0bb85d74efa24a106b31d24b76cdd5..9861a59c7256d92357820ad242ac665041e19d27 100644 (file)
@@ -14,9 +14,11 @@ package org.openhab.binding.somfytahoma.internal.handler;
 
 import static org.openhab.binding.somfytahoma.internal.SomfyTahomaBindingConstants.*;
 
+import java.math.BigDecimal;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 import javax.measure.Unit;
 
@@ -231,6 +233,10 @@ public abstract class SomfyTahomaBaseThingHandler extends BaseThingHandler {
         }
     }
 
+    protected Unit<?> getTemperatureUnit() {
+        return Objects.requireNonNull(units.get("Number:Temperature"));
+    }
+
     private void updateUnits(List<SomfyTahomaState> attributes) {
         for (SomfyTahomaState attr : attributes) {
             if ("core:MeasuredValueType".equals(attr.getName()) && attr.getType() == TYPE_STRING) {
@@ -387,12 +393,7 @@ public abstract class SomfyTahomaBaseThingHandler extends BaseThingHandler {
     }
 
     private @Nullable SomfyTahomaState getStatusState(List<SomfyTahomaState> states) {
-        for (SomfyTahomaState state : states) {
-            if (STATUS_STATE.equals(state.getName()) && state.getType() == TYPE_STRING) {
-                return state;
-            }
-        }
-        return null;
+        return getState(states, STATUS_STATE, TYPE_STRING);
     }
 
     private void updateThingStatus(@Nullable SomfyTahomaState state) {
@@ -456,4 +457,29 @@ public abstract class SomfyTahomaBaseThingHandler extends BaseThingHandler {
     public int toInteger(Command command) {
         return (command instanceof DecimalType) ? ((DecimalType) command).intValue() : 0;
     }
+
+    public @Nullable BigDecimal toTemperature(Command command) {
+        BigDecimal temperature = null;
+        if (command instanceof QuantityType<?>) {
+            QuantityType<?> quantity = (QuantityType<?>) command;
+            QuantityType<?> convertedQuantity = quantity.toUnit(getTemperatureUnit());
+            if (convertedQuantity != null) {
+                quantity = convertedQuantity;
+            }
+            temperature = quantity.toBigDecimal();
+        } else if (command instanceof DecimalType) {
+            temperature = ((DecimalType) command).toBigDecimal();
+        }
+        return temperature;
+    }
+
+    public static @Nullable SomfyTahomaState getState(List<SomfyTahomaState> states, String stateName,
+            @Nullable Integer stateType) {
+        for (SomfyTahomaState state : states) {
+            if (stateName.equals(state.getName()) && (stateType == null || stateType == state.getType())) {
+                return state;
+            }
+        }
+        return null;
+    }
 }
index 96059de1d156f9e7c9874717eaebf4d52462d772..4f197a54b16e93ac52e3edd339d2bc22d14c9286 100644 (file)
@@ -14,6 +14,7 @@ package org.openhab.binding.somfytahoma.internal.handler;
 
 import static org.openhab.binding.somfytahoma.internal.SomfyTahomaBindingConstants.*;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.core.thing.Thing;
 
 /**
@@ -22,6 +23,7 @@ import org.openhab.core.thing.Thing;
  *
  * @author Ondrej Pecta - Initial contribution
  */
+@NonNullByDefault
 public class SomfyTahomaElectricitySensorHandler extends SomfyTahomaBaseThingHandler {
 
     public SomfyTahomaElectricitySensorHandler(Thing thing) {
index 6b8aa1f56dc16fa3414af2e75fb172f6e5e842a9..2bc81ecd2eb674b895b75f919704635ea64fb33d 100644 (file)
@@ -14,8 +14,9 @@ package org.openhab.binding.somfytahoma.internal.handler;
 
 import static org.openhab.binding.somfytahoma.internal.SomfyTahomaBindingConstants.*;
 
+import java.math.BigDecimal;
+
 import org.eclipse.jdt.annotation.NonNullByDefault;
-import org.openhab.core.library.types.QuantityType;
 import org.openhab.core.thing.ChannelUID;
 import org.openhab.core.thing.Thing;
 import org.openhab.core.types.Command;
@@ -47,10 +48,12 @@ public class SomfyTahomaValveHeatingSystemHandler extends SomfyTahomaBaseThingHa
         if (command instanceof RefreshType) {
             return;
         } else {
-            if (DEROGATED_TARGET_TEMPERATURE.equals(channelUID.getId()) && command instanceof QuantityType) {
-                QuantityType type = (QuantityType) command;
-                String param = "[" + type.doubleValue() + ", \"next_mode\"]";
-                sendCommand(COMMAND_SET_DEROGATION, param);
+            if (DEROGATED_TARGET_TEMPERATURE.equals(channelUID.getId())) {
+                BigDecimal temperature = toTemperature(command);
+                if (temperature != null) {
+                    String param = "[" + temperature.toPlainString() + ", \"next_mode\"]";
+                    sendCommand(COMMAND_SET_DEROGATION, param);
+                }
             } else if (DEROGATION_HEATING_MODE.equals(channelUID.getId())) {
                 switch (command.toString()) {
                     case "auto":
index d166336d714fc4ca6e1780fc2611ee6958d297c3..791170411bd3866fff019d98decac7503e629ca1 100644 (file)
@@ -14,6 +14,8 @@ package org.openhab.binding.somfytahoma.internal.handler;
 
 import static org.openhab.binding.somfytahoma.internal.SomfyTahomaBindingConstants.*;
 
+import java.math.BigDecimal;
+
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.core.thing.ChannelUID;
 import org.openhab.core.thing.Thing;
@@ -44,8 +46,11 @@ public class SomfyTahomaZwaveHeatingSystemHandler extends SomfyTahomaBaseThingHa
             return;
         } else {
             if (TARGET_TEMPERATURE.equals(channelUID.getId())) {
-                String param = "[" + command.toString() + "]";
-                sendCommand("setTargetTemperature", param);
+                BigDecimal temperature = toTemperature(command);
+                if (temperature != null) {
+                    String param = "[" + temperature.toPlainString() + "]";
+                    sendCommand("setTargetTemperature", param);
+                }
             }
         }
     }