From 4fbb6c965566ae15ee24822882fc6e35ca30bbfb Mon Sep 17 00:00:00 2001 From: Andreas Berger Date: Thu, 30 Nov 2023 11:13:41 +0100 Subject: [PATCH] [fineoffsetweatherstation] Fix handling of undefined lightning distance and time (#15979) Signed-off-by: Andreas Berger --- .../internal/domain/MeasureType.java | 21 ++++++++--- .../service/FineOffsetDataParserTest.java | 36 +++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/bundles/org.openhab.binding.fineoffsetweatherstation/src/main/java/org/openhab/binding/fineoffsetweatherstation/internal/domain/MeasureType.java b/bundles/org.openhab.binding.fineoffsetweatherstation/src/main/java/org/openhab/binding/fineoffsetweatherstation/internal/domain/MeasureType.java index 9c052850b..68c43b977 100644 --- a/bundles/org.openhab.binding.fineoffsetweatherstation/src/main/java/org/openhab/binding/fineoffsetweatherstation/internal/domain/MeasureType.java +++ b/bundles/org.openhab.binding.fineoffsetweatherstation/src/main/java/org/openhab/binding/fineoffsetweatherstation/internal/domain/MeasureType.java @@ -60,6 +60,7 @@ import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.unit.Units; import org.openhab.core.thing.type.ChannelTypeUID; import org.openhab.core.types.State; +import org.openhab.core.types.UnDefType; /** * Represents the measured type with conversion from the sensors' bytes to the openHAB state. @@ -98,14 +99,24 @@ public enum MeasureType { WATER_LEAK_DETECTION(1, CHANNEL_TYPE_WATER_LEAK_DETECTION, (data, offset, context) -> toUInt8(data[offset]) != 0 ? OnOffType.ON : OnOffType.OFF), - LIGHTNING_DISTANCE(KILO(METRE), 1, CHANNEL_TYPE_LIGHTNING_DISTANCE, (data, offset) -> toUInt8(data[offset])), + LIGHTNING_DISTANCE(KILO(METRE), 1, CHANNEL_TYPE_LIGHTNING_DISTANCE, (data, offset) -> { + int distance = toUInt8(data[offset]); + if (distance == 0xFF) { + return null; + } + return distance; + }), LIGHTNING_COUNTER(4, CHANNEL_TYPE_LIGHTNING_COUNTER, (data, offset, context) -> new DecimalType(toUInt32(data, offset))), - LIGHTNING_TIME(4, CHANNEL_TYPE_LIGHTNING_TIME, - (data, offset, context) -> new DateTimeType( - ZonedDateTime.ofInstant(Instant.ofEpochSecond(toUInt32(data, offset)), context.getZoneId()))), + LIGHTNING_TIME(4, CHANNEL_TYPE_LIGHTNING_TIME, (data, offset, context) -> { + int epochSecond = toUInt32(data, offset); + if (epochSecond == 0xFFFFFFFF) { + return UnDefType.UNDEF; + } + return new DateTimeType(ZonedDateTime.ofInstant(Instant.ofEpochSecond(epochSecond), context.getZoneId())); + }), MILLIWATT_PER_SQUARE_METRE(MILLI(Units.WATT).divide(SQUARE_METRE), 2, CHANNEL_TYPE_UV_RADIATION, (data, offset) -> Utils.toUInt16(data, offset) / 10.), @@ -129,7 +140,7 @@ public enum MeasureType { BiFunction valueExtractor) { this(byteSize, channelTypeUID, (bytes, offset, context) -> { Number value = valueExtractor.apply(bytes, offset); - return value == null ? null : new QuantityType<>(value, unit); + return value == null ? UnDefType.UNDEF : new QuantityType<>(value, unit); }); } diff --git a/bundles/org.openhab.binding.fineoffsetweatherstation/src/test/java/org/openhab/binding/fineoffsetweatherstation/internal/service/FineOffsetDataParserTest.java b/bundles/org.openhab.binding.fineoffsetweatherstation/src/test/java/org/openhab/binding/fineoffsetweatherstation/internal/service/FineOffsetDataParserTest.java index 1625d7fb4..d58e50d62 100644 --- a/bundles/org.openhab.binding.fineoffsetweatherstation/src/test/java/org/openhab/binding/fineoffsetweatherstation/internal/service/FineOffsetDataParserTest.java +++ b/bundles/org.openhab.binding.fineoffsetweatherstation/src/test/java/org/openhab/binding/fineoffsetweatherstation/internal/service/FineOffsetDataParserTest.java @@ -57,6 +57,42 @@ class FineOffsetDataParserTest { new Tuple("sensor-co2-co2", "686 ppm"), new Tuple("sensor-co2-co2-24-hour-average", "655 ppm")); } + @Test + void testLiveDataWithManySensors() { + byte[] bytes = HexUtils.hexToBytes( + "FFFF27007B0100D206240826CC0926CC02004907450A00760B00160C001F150001C00C16000017002A00144D00372C381A0085223E1B00A72333580059005A00620000000061FFFFFFFF60FF1900380E000010002D1100A012000000A013000000A00D009F63004D417000CF2C00250020001B0018020B021E06722164"); + DebugDetails debugDetails = new DebugDetails(bytes, Command.CMD_GW1000_LIVEDATA, Protocol.DEFAULT); + List data = new FineOffsetDataParser(Protocol.DEFAULT).getMeasuredValues(bytes, + new ConversionContext(ZoneOffset.UTC), debugDetails); + Assertions.assertThat(data) + .extracting(MeasuredValue::getChannelId, measuredValue -> measuredValue.getState().toString()) + .containsExactly(new Tuple("temperature-indoor", "21 °C"), new Tuple("humidity-indoor", "36 %"), + new Tuple("pressure-absolute", "993.2 hPa"), new Tuple("pressure-relative", "993.2 hPa"), + new Tuple("temperature-outdoor", "7.3 °C"), new Tuple("humidity-outdoor", "69 %"), + new Tuple("direction-wind", "118 °"), new Tuple("speed-wind", "2.2 m/s"), + new Tuple("speed-gust", "3.1 m/s"), new Tuple("illumination", "11470 lx"), + new Tuple("irradiation-uv", "0 mW/m²"), new Tuple("uv-index", "0"), + new Tuple("air-quality-channel-1", "2 µg/m³"), + new Tuple("air-quality-24-hour-average-channel-1", "5.5 µg/m³"), + new Tuple("moisture-soil-channel-1", "56 %"), new Tuple("temperature-channel-1", "13.3 °C"), + new Tuple("humidity-channel-1", "62 %"), new Tuple("temperature-channel-2", "16.7 °C"), + new Tuple("humidity-channel-2", "51 %"), new Tuple("water-leak-channel-1", "OFF"), + new Tuple("water-leak-channel-2", "OFF"), new Tuple("water-leak-channel-3", "OFF"), + new Tuple("lightning-counter", "0"), new Tuple("lightning-time", "UNDEF"), + new Tuple("lightning-distance", "UNDEF"), new Tuple("wind-max-day", "5.6 m/s"), + new Tuple("rain-rate", "0 mm/h"), new Tuple("rain-day", "4.5 mm"), + new Tuple("rain-week", "16 mm"), new Tuple("rain-month", "16 mm"), + new Tuple("rain-year", "16 mm"), new Tuple("rain-event", "15.9 mm"), + new Tuple("temperature-external-channel-1", "7.7 °C"), + new Tuple("sensor-co2-temperature", "20.7 °C"), new Tuple("sensor-co2-humidity", "44 %"), + new Tuple("sensor-co2-pm10", "3.7 µg/m³"), + new Tuple("sensor-co2-pm10-24-hour-average", "3.2 µg/m³"), + new Tuple("sensor-co2-pm25", "2.7 µg/m³"), + new Tuple("sensor-co2-pm25-24-hour-average", "2.4 µg/m³"), + new Tuple("sensor-co2-co2", "523 ppm"), new Tuple("sensor-co2-co2-24-hour-average", "542 ppm"), + new Tuple("leaf-wetness-channel-1", "33 %")); + } + @Test void testLiveDataWH34AndWh45() { byte[] bytes = HexUtils.hexToBytes(