[MQTT] Fix tests after core change (#16774)

* fix mqtt tests

Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Mark Herwege 2024-05-19 13:56:30 +02:00 committed by Ciprian Pascu
parent 4ac5dd40b1
commit c8a1c14bc1
2 changed files with 21 additions and 3 deletions

View File

@ -28,6 +28,8 @@ import org.openhab.binding.mqtt.generic.values.TextValue;
import org.openhab.binding.mqtt.generic.values.Value; import org.openhab.binding.mqtt.generic.values.Value;
import org.openhab.core.io.transport.mqtt.MqttBrokerConnection; import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
import org.openhab.core.io.transport.mqtt.MqttMessageSubscriber; 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.library.types.StringType;
import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.ChannelUID;
import org.openhab.core.types.Command; import org.openhab.core.types.Command;
@ -378,7 +380,13 @@ public class ChannelState implements MqttMessageSubscriber {
// Outgoing transformations // Outgoing transformations
for (ChannelStateTransformation t : transformationsOut) { 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); String transformedValue = t.processValue(commandString);
if (transformedValue != null) { if (transformedValue != null) {
mqttFormatter = new TextValue(); 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. // Formatter: Applied before the channel state value is published to the MQTT broker.
if (config.formatBeforePublish.length() > 0) { if (config.formatBeforePublish.length() > 0) {
try { 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) { } catch (IllegalFormatException e) {
logger.debug("Format pattern incorrect for {}", channelUID, e); logger.debug("Format pattern incorrect for {}", channelUID, e);
commandString = mqttFormatter.getMQTTpublishValue(mqttCommandValue, null); commandString = mqttFormatter.getMQTTpublishValue(mqttCommandValue, null);

View File

@ -84,7 +84,11 @@ public class NumberValue extends Value {
public String getMQTTpublishValue(Command command, @Nullable String pattern) { public String getMQTTpublishValue(Command command, @Nullable String pattern) {
String formatPattern = pattern; String formatPattern = pattern;
if (formatPattern == null) { if (formatPattern == null) {
formatPattern = "%s"; if (command instanceof DecimalType || command instanceof QuantityType<?>) {
formatPattern = "%.0f";
} else {
formatPattern = "%s";
}
} }
return command.format(formatPattern); return command.format(formatPattern);