]> git.basschouten.com Git - openhab-addons.git/commitdiff
[fineoffsetweatherstation] Fix handling of undefined lightning distance and time...
authorAndreas Berger <Andy2003@users.noreply.github.com>
Thu, 30 Nov 2023 10:13:41 +0000 (11:13 +0100)
committerGitHub <noreply@github.com>
Thu, 30 Nov 2023 10:13:41 +0000 (11:13 +0100)
Signed-off-by: Andreas Berger <andreas@berger-freelancer.com>
bundles/org.openhab.binding.fineoffsetweatherstation/src/main/java/org/openhab/binding/fineoffsetweatherstation/internal/domain/MeasureType.java
bundles/org.openhab.binding.fineoffsetweatherstation/src/test/java/org/openhab/binding/fineoffsetweatherstation/internal/service/FineOffsetDataParserTest.java

index 9c052850b15c0735d82d4e94d0cb990f082b62bf..68c43b97721642f0f7db52ca6b9dbc003e6c0812 100644 (file)
@@ -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<byte[], Integer, @Nullable Number> 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);
         });
     }
 
index 1625d7fb44f819783eb66f9ecdcfbfd380b7983c..d58e50d62b1793b03579b5982611fe6512d702c6 100644 (file)
@@ -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<MeasuredValue> 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(