Skip to content

Commit

Permalink
[fineoffsetweatherstation] Fix handling of undefined lightning distan…
Browse files Browse the repository at this point in the history
…ce and time (openhab#15979)

Signed-off-by: Andreas Berger <[email protected]>
  • Loading branch information
Andy2003 authored Nov 30, 2023
1 parent 73ec188 commit 4fbb6c9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
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.
Expand Down Expand Up @@ -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.),
Expand All @@ -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);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,42 @@ void testLiveDataWH45() {
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(
Expand Down

0 comments on commit 4fbb6c9

Please sign in to comment.