From d049995e56b412f9dea8b62fb640bafc23ad7d73 Mon Sep 17 00:00:00 2001 From: jimtng <2554958+jimtng@users.noreply.github.com> Date: Thu, 26 Dec 2024 01:32:26 +1000 Subject: [PATCH] [mqtt.generic] Fix ClassCastException when receiving ON/OFF on a dimmer channel (#17980) Signed-off-by: Jimmy Tanagra --- .../openhab/binding/mqtt/generic/values/PercentageValue.java | 2 +- .../org/openhab/binding/mqtt/generic/values/ValueTests.java | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/PercentageValue.java b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/PercentageValue.java index 6a521fbe547..eb4bfb280be 100644 --- a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/PercentageValue.java +++ b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/PercentageValue.java @@ -75,7 +75,7 @@ public class PercentageValue extends Value { @Override public Command parseCommand(Command command) throws IllegalArgumentException { - PercentType oldvalue = (state instanceof UnDefType) ? new PercentType() : (PercentType) state; + PercentType oldvalue = (state instanceof UnDefType) ? new PercentType() : state.as(PercentType.class); // Nothing do to -> We have received a percentage if (command instanceof PercentType percent) { return percent; diff --git a/bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java b/bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java index b602e16897c..c79a0c8834a 100644 --- a/bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java +++ b/bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java @@ -313,6 +313,8 @@ public class ValueTests { assertThat(v.parseCommand(new DecimalType(10.0)), is(PercentType.ZERO)); assertThat(v.getMQTTpublishValue(PercentType.ZERO, null), is("10")); + v.update(OnOffType.OFF); + assertThat(v.parseCommand(OnOffType.ON), is(OnOffType.ON)); assertThat(v.getMQTTpublishValue(OnOffType.ON, null), is("110")); assertThat(v.parseCommand(OnOffType.OFF), is(OnOffType.OFF));