]> git.basschouten.com Git - openhab-addons.git/commitdiff
[MQTT] Fix tests after core change (#16774)
authorMark Herwege <mherwege@users.noreply.github.com>
Sun, 19 May 2024 11:56:30 +0000 (13:56 +0200)
committerGitHub <noreply@github.com>
Sun, 19 May 2024 11:56:30 +0000 (13:56 +0200)
* fix mqtt tests

Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/ChannelState.java
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java

index 41482b93e2962a8a5ab1c6d6df152938d2bfa631..832f94397e13a39cf52b4a2394ddc332e133cf37 100644 (file)
@@ -28,6 +28,8 @@ import org.openhab.binding.mqtt.generic.values.TextValue;
 import org.openhab.binding.mqtt.generic.values.Value;
 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
 import org.openhab.core.io.transport.mqtt.MqttMessageSubscriber;
+import org.openhab.core.library.types.DecimalType;
+import org.openhab.core.library.types.QuantityType;
 import org.openhab.core.library.types.StringType;
 import org.openhab.core.thing.ChannelUID;
 import org.openhab.core.types.Command;
@@ -378,7 +380,13 @@ public class ChannelState implements MqttMessageSubscriber {
 
         // Outgoing transformations
         for (ChannelStateTransformation t : transformationsOut) {
-            String commandString = mqttFormatter.getMQTTpublishValue(mqttCommandValue, null);
+            Command cValue = mqttCommandValue;
+            // Only pass numeric value for QuantityType.
+            if (mqttCommandValue instanceof QuantityType<?> qtCommandValue) {
+                cValue = new DecimalType(qtCommandValue.toBigDecimal());
+
+            }
+            String commandString = mqttFormatter.getMQTTpublishValue(cValue, "%s");
             String transformedValue = t.processValue(commandString);
             if (transformedValue != null) {
                 mqttFormatter = new TextValue();
@@ -395,7 +403,13 @@ public class ChannelState implements MqttMessageSubscriber {
         // Formatter: Applied before the channel state value is published to the MQTT broker.
         if (config.formatBeforePublish.length() > 0) {
             try {
-                commandString = mqttFormatter.getMQTTpublishValue(mqttCommandValue, config.formatBeforePublish);
+                Command cValue = mqttCommandValue;
+                // Only pass numeric value for QuantityType of format pattern is %s.
+                if ((mqttCommandValue instanceof QuantityType<?> qtCommandValue)
+                        && ("%s".equals(config.formatBeforePublish) || "%S".equals(config.formatBeforePublish))) {
+                    cValue = new DecimalType(qtCommandValue.toBigDecimal());
+                }
+                commandString = mqttFormatter.getMQTTpublishValue(cValue, config.formatBeforePublish);
             } catch (IllegalFormatException e) {
                 logger.debug("Format pattern incorrect for {}", channelUID, e);
                 commandString = mqttFormatter.getMQTTpublishValue(mqttCommandValue, null);
index cfece9d67bf815a1e257be3d0ea312935113425d..3ae6ee2e427e9f403c007c950ef6dbf74316d6ea 100644 (file)
@@ -84,7 +84,11 @@ public class NumberValue extends Value {
     public String getMQTTpublishValue(Command command, @Nullable String pattern) {
         String formatPattern = pattern;
         if (formatPattern == null) {
-            formatPattern = "%s";
+            if (command instanceof DecimalType || command instanceof QuantityType<?>) {
+                formatPattern = "%.0f";
+            } else {
+                formatPattern = "%s";
+            }
         }
 
         return command.format(formatPattern);