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.rfxcom.internal.messages;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
17 import static org.openhab.binding.rfxcom.internal.messages.RFXComRFXSensorMessage.SubType.*;
19 import javax.xml.bind.DatatypeConverter;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
25 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.types.State;
30 * Test for RFXCom-binding
32 * @author Martin van Wingerden - Initial contribution
35 public class RFXComRFXSensorMessageTest {
36 private final MockDeviceState mockedDeviceState = new MockDeviceState();
38 private void testMessage(String hexMsg, RFXComRFXSensorMessage.SubType subType, int seqNbr, String deviceId,
39 @Nullable Double temperature, @Nullable Double voltage, @Nullable Double referenceVoltage,
40 @Nullable Double expectedPressure, @Nullable Double expectedHumidity, int signalLevel,
41 DeviceState deviceState) throws RFXComException {
42 final RFXComRFXSensorMessage msg = (RFXComRFXSensorMessage) RFXComMessageFactoryImpl.INSTANCE
43 .createMessage(DatatypeConverter.parseHexBinary(hexMsg));
44 assertEquals(subType, msg.subType, "SubType");
45 assertEquals(seqNbr, (short) (msg.seqNbr & 0xFF), "Seq Number");
46 assertEquals(deviceId, msg.getDeviceId(), "Sensor Id");
47 assertEquals(signalLevel, msg.signalLevel, "Signal Level");
48 assertEquals(temperature, getMessageTemperature(msg, deviceState), "Temperature");
49 assertEquals(voltage, getChannelAsDouble(CHANNEL_VOLTAGE, msg, deviceState), "Voltage");
50 assertEquals(referenceVoltage, getChannelAsDouble(CHANNEL_REFERENCE_VOLTAGE, msg, deviceState),
52 assertEquals(expectedHumidity, getChannelAsDouble(CHANNEL_HUMIDITY, msg, deviceState), "Humidity");
53 assertEquals(expectedPressure, getChannelAsDouble(CHANNEL_PRESSURE, msg, deviceState), "Pressure");
55 byte[] decoded = msg.decodeMessage();
57 assertEquals(hexMsg, DatatypeConverter.printHexBinary(decoded), "Message converted back");
61 public void testSomeMessages() throws RFXComException {
62 testMessage("0770000008080270", TEMPERATURE, 0, "8", 20.5d, null, null, null, null, 7, mockedDeviceState);
63 testMessage("0770000208809650", TEMPERATURE, 2, "8", -1.5d, null, null, null, null, 5, mockedDeviceState);
64 testMessage("077002010801F270", VOLTAGE, 1, "8", null, null, 4.98, null, null, 7, mockedDeviceState);
65 testMessage("077001020800F470", A_D, 2, "8", null, 2.44, null, null, null, 7, mockedDeviceState);
69 public void testPressure() throws RFXComException {
70 MockDeviceState deviceState = new MockDeviceState();
71 deviceState.set(CHANNEL_REFERENCE_VOLTAGE, new DecimalType(4.98));
73 testMessage("077001020800F470", A_D, 2, "8", null, 2.44, null, 650.0, null, 7, deviceState);
77 public void testHumidity() throws RFXComException {
78 MockDeviceState deviceState = new MockDeviceState();
79 deviceState.set(CHANNEL_TEMPERATURE, new DecimalType(20.5));
80 deviceState.set(CHANNEL_REFERENCE_VOLTAGE, new DecimalType(4.98));
82 testMessage("077001020800F470", A_D, 2, "8", null, 2.44, null, 650.0, 52.6821, 7, deviceState);
85 private @Nullable Double getMessageTemperature(RFXComRFXSensorMessage msg, DeviceState deviceState)
86 throws RFXComException {
87 return getChannelAsDouble(CHANNEL_TEMPERATURE, msg, deviceState);
90 private @Nullable Double getChannelAsDouble(String channelId, RFXComRFXSensorMessage msg, DeviceState deviceState)
91 throws RFXComException {
92 return getStateAsDouble(msg.convertToState(channelId, null, deviceState));
95 private @Nullable Double getStateAsDouble(State state) {
96 if (state instanceof DecimalType) {
97 return ((DecimalType) state).doubleValue();