2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.enocean.internal.eep.D2_14;
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
17 import java.util.function.Function;
19 import org.openhab.binding.enocean.internal.eep.Base._VLDMessage;
20 import org.openhab.binding.enocean.internal.messages.ERP1Message;
21 import org.openhab.core.config.core.Configuration;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.library.unit.SIUnits;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.types.State;
29 import org.openhab.core.types.UnDefType;
33 * @author Daniel Weber - Initial contribution
35 public class D2_14_30 extends _VLDMessage {
37 private final String[] hygroComfortIndexValues = { "GOOD", "MEDIUM", "BAD", "ERROR" };
38 private final String[] indoorAirAnalysisValues = { "OPTIMAL", "DRY", "HIGH_HUMIDITY", "HIGH_TEMPHUMI",
39 "OUT_OF_RANGE", "RESERVED1", "RESERVED2", "ERROR" };
45 public D2_14_30(ERP1Message packet) {
49 protected State getBatteryLevel() {
50 switch ((bytes[1] & 0b110) >>> 1) {
52 return new QuantityType<>(100, Units.PERCENT); // High
54 return new QuantityType<>(50, Units.PERCENT); // Medium
56 return new QuantityType<>(25, Units.PERCENT); // Low
58 return new QuantityType<>(5, Units.PERCENT); // Critical
61 return UnDefType.UNDEF;
65 public State convertToStateImpl(String channelId, String channelTypeId, Function<String, State> getCurrentStateFunc,
66 Configuration config) {
68 case CHANNEL_SMOKEDETECTION:
69 return getBit(bytes[0], 7) ? OnOffType.ON : OnOffType.OFF;
70 case CHANNEL_SENSORFAULT:
71 return getBit(bytes[0], 6) ? OnOffType.ON : OnOffType.OFF;
72 case CHANNEL_MAINTENANCESTATUS:
73 return getBit(bytes[0], 5) ? OnOffType.ON : OnOffType.OFF;
74 case CHANNEL_SENSORANALYSISHUMIDITYRANGE:
75 return getBit(bytes[0], 4) ? OnOffType.ON : OnOffType.OFF;
76 case CHANNEL_SENSORANALYSISTEMPERATURRANGE:
77 return getBit(bytes[0], 3) ? OnOffType.ON : OnOffType.OFF;
78 case CHANNEL_TIMESINCELASTMAINTENANCE:
79 return new QuantityType<>(((bytes[0] << 5) + (bytes[0] >>> 3)) & 0xFF, Units.WEEK);
80 case CHANNEL_BATTERY_LEVEL:
81 return getBatteryLevel();
82 case CHANNEL_REMAININGPLT: {
83 int months = ((bytes[1] << 7) + (bytes[2] >>> 1)) & 0xFF;
84 return months < 121 ? new QuantityType<>(months * 4, Units.WEEK) : UnDefType.NULL;
86 case CHANNEL_TEMPERATURE: {
87 int unscaledValue = ((bytes[2] << 7) + (bytes[3] >>> 1)) & 0xFF;
88 return unscaledValue < 251 ? new QuantityType<>(((double) unscaledValue) / 5.0, SIUnits.CELSIUS)
91 case CHANNEL_HUMIDITY: {
92 int unscaledValue = ((bytes[3] << 7) + (bytes[4] >>> 1)) & 0xFF;
93 return unscaledValue < 251 ? new DecimalType(((double) unscaledValue) / 2.0) : UnDefType.NULL;
95 case CHANNEL_HYGROCOMFORTINDEX:
96 return new StringType(hygroComfortIndexValues[(((bytes[4] & 0b1) << 1) + (bytes[5] >>> 7))]);
97 case CHANNEL_INDOORAIRANALYSIS:
98 return new StringType(indoorAirAnalysisValues[(bytes[5] >>> 4) & 0b111]);
101 return UnDefType.UNDEF;
105 protected boolean validateData(byte[] bytes) {
106 return bytes.length == 6;