Skip to content

Commit 0ff8564

Browse files
authored
[MQTT] Fix tests after core change (openhab#16774)
* fix mqtt tests Signed-off-by: Mark Herwege <[email protected]>
1 parent 045065f commit 0ff8564

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/ChannelState.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.openhab.binding.mqtt.generic.values.Value;
2929
import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
3030
import org.openhab.core.io.transport.mqtt.MqttMessageSubscriber;
31+
import org.openhab.core.library.types.DecimalType;
32+
import org.openhab.core.library.types.QuantityType;
3133
import org.openhab.core.library.types.StringType;
3234
import org.openhab.core.thing.ChannelUID;
3335
import org.openhab.core.types.Command;
@@ -378,7 +380,13 @@ public CompletableFuture<Boolean> publishValue(Command command) {
378380

379381
// Outgoing transformations
380382
for (ChannelStateTransformation t : transformationsOut) {
381-
String commandString = mqttFormatter.getMQTTpublishValue(mqttCommandValue, null);
383+
Command cValue = mqttCommandValue;
384+
// Only pass numeric value for QuantityType.
385+
if (mqttCommandValue instanceof QuantityType<?> qtCommandValue) {
386+
cValue = new DecimalType(qtCommandValue.toBigDecimal());
387+
388+
}
389+
String commandString = mqttFormatter.getMQTTpublishValue(cValue, "%s");
382390
String transformedValue = t.processValue(commandString);
383391
if (transformedValue != null) {
384392
mqttFormatter = new TextValue();
@@ -395,7 +403,13 @@ public CompletableFuture<Boolean> publishValue(Command command) {
395403
// Formatter: Applied before the channel state value is published to the MQTT broker.
396404
if (config.formatBeforePublish.length() > 0) {
397405
try {
398-
commandString = mqttFormatter.getMQTTpublishValue(mqttCommandValue, config.formatBeforePublish);
406+
Command cValue = mqttCommandValue;
407+
// Only pass numeric value for QuantityType of format pattern is %s.
408+
if ((mqttCommandValue instanceof QuantityType<?> qtCommandValue)
409+
&& ("%s".equals(config.formatBeforePublish) || "%S".equals(config.formatBeforePublish))) {
410+
cValue = new DecimalType(qtCommandValue.toBigDecimal());
411+
}
412+
commandString = mqttFormatter.getMQTTpublishValue(cValue, config.formatBeforePublish);
399413
} catch (IllegalFormatException e) {
400414
logger.debug("Format pattern incorrect for {}", channelUID, e);
401415
commandString = mqttFormatter.getMQTTpublishValue(mqttCommandValue, null);

bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ protected boolean checkConditions(BigDecimal newValue) {
8484
public String getMQTTpublishValue(Command command, @Nullable String pattern) {
8585
String formatPattern = pattern;
8686
if (formatPattern == null) {
87-
formatPattern = "%s";
87+
if (command instanceof DecimalType || command instanceof QuantityType<?>) {
88+
formatPattern = "%.0f";
89+
} else {
90+
formatPattern = "%s";
91+
}
8892
}
8993

9094
return command.format(formatPattern);

0 commit comments

Comments
 (0)