diff --git a/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/DPTUnits.java b/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/DPTUnits.java index a550f2027..b7d43bed6 100644 --- a/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/DPTUnits.java +++ b/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/DPTUnits.java @@ -63,8 +63,8 @@ public class DPTUnits { * * @return stream of all unit strings */ - static Stream getAllUnitStrings() { - return DPT_UNIT_MAP.values().stream(); + static Stream> getAllUnitStrings() { + return DPT_UNIT_MAP.entrySet().stream(); } static { diff --git a/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/handler/DeviceThingHandler.java b/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/handler/DeviceThingHandler.java index 0f684320c..5c29712c8 100644 --- a/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/handler/DeviceThingHandler.java +++ b/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/handler/DeviceThingHandler.java @@ -120,13 +120,16 @@ public class DeviceThingHandler extends BaseThingHandler implements GroupAddress for (Channel channel : getThing().getChannels()) { KNXChannel knxChannel = KNXChannelFactory.createKnxChannel(channel); - knxChannels.put(channel.getUID(), knxChannel); - groupAddresses.addAll(knxChannel.getAllGroupAddresses()); if (knxChannel.getChannelType().startsWith("number")) { // check if we need to update the accepted item-type List inboundSpecs = knxChannel.getAllGroupAddresses().stream() .map(knxChannel::getListenSpec).filter(Objects::nonNull).map(Objects::requireNonNull).toList(); + if (inboundSpecs.isEmpty()) { + logger.warn("Skipping {}: group address / DPT not according to Group Address Notation", + channel.getUID()); + continue; + } String dpt = inboundSpecs.get(0).getDPT(); // there can be only one DPT on number channels Unit unit = UnitUtils.parseUnit(DPTUnits.getUnitForDpt(dpt)); @@ -149,6 +152,10 @@ public class DeviceThingHandler extends BaseThingHandler implements GroupAddress modified = true; } } + + // add channels only if they could be successfully processed + knxChannels.put(channel.getUID(), knxChannel); + groupAddresses.addAll(knxChannel.getAllGroupAddresses()); } if (modified) { diff --git a/bundles/org.openhab.binding.knx/src/test/java/org/openhab/binding/knx/internal/dpt/DPTTest.java b/bundles/org.openhab.binding.knx/src/test/java/org/openhab/binding/knx/internal/dpt/DPTTest.java index cda7f4c79..f969b265d 100644 --- a/bundles/org.openhab.binding.knx/src/test/java/org/openhab/binding/knx/internal/dpt/DPTTest.java +++ b/bundles/org.openhab.binding.knx/src/test/java/org/openhab/binding/knx/internal/dpt/DPTTest.java @@ -14,6 +14,7 @@ package org.openhab.binding.knx.internal.dpt; import static org.junit.jupiter.api.Assertions.*; +import java.util.Map; import java.util.Objects; import java.util.regex.Matcher; import java.util.stream.IntStream; @@ -386,16 +387,20 @@ class DPTTest { assertNotEquals(DPTXlator64BitSigned.DPT_REACTIVE_ENERGY.getUnit(), Units.VAR_HOUR.toString()); } - private static Stream unitProvider() { + private static Stream> unitProvider() { return DPTUnits.getAllUnitStrings(); } @ParameterizedTest @MethodSource("unitProvider") - public void unitsValid(String unit) { - String valueStr = "1 " + unit; - QuantityType value = new QuantityType<>(valueStr); - Assertions.assertNotNull(value); + public void unitsValid(Map.Entry unit) { + String valueStr = "1 " + unit.getValue(); + try { + QuantityType value = new QuantityType<>(valueStr); + Assertions.assertNotNull(value, "Failed to parse " + unit + "(result null)"); + } catch (Exception e) { + fail("Failed to parse " + unit + ": " + e.getMessage()); + } } private static Stream rgbValueProvider() {