2 * Copyright (c) 2010-2021 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.RFXComBaseMessage.PacketType.FAN;
18 import static org.openhab.binding.rfxcom.internal.messages.RFXComFanMessage.SubType.CASAFAN;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
28 import org.openhab.core.util.HexUtils;
31 * Test for RFXCom-binding
33 * @author Martin van Wingerden - Initial contribution
36 public class RFXComFanMessageTest {
37 private static final MockDeviceState DEVICE_STATE = new MockDeviceState();
40 public void checkForSupportTest() throws RFXComException {
41 RFXComMessageFactoryImpl.INSTANCE.createMessage(FAN);
45 public void basicBoundaryCheck() throws RFXComException {
46 RFXComFanMessage message = (RFXComFanMessage) RFXComMessageFactoryImpl.INSTANCE.createMessage(FAN);
48 message.setSubType(RFXComFanMessage.SubType.CASAFAN);
49 message.convertFromState(CHANNEL_FAN_SPEED, StringType.valueOf("OFF"));
51 RFXComTestHelper.basicBoundaryCheck(FAN, message);
54 private void testMessage(String hexMsg, int seqNbr, String deviceId, int signalLevel,
55 @Nullable State expectedCommand, State expectedLightCommand, @Nullable State expectedFanSpeed,
56 RFXComBaseMessage.PacketType packetType) throws RFXComException {
57 final RFXComFanMessage msg = (RFXComFanMessage) RFXComMessageFactoryImpl.INSTANCE
58 .createMessage(HexUtils.hexToBytes(hexMsg));
59 assertEquals(seqNbr, (short) (msg.seqNbr & 0xFF), "Seq Number");
60 assertEquals(deviceId, msg.getDeviceId(), "Sensor Id");
61 assertEquals(signalLevel, msg.signalLevel, "Signal Level");
63 assertEquals(expectedCommand, msg.convertToState(CHANNEL_COMMAND, DEVICE_STATE));
64 assertEquals(expectedLightCommand, msg.convertToState(CHANNEL_FAN_LIGHT, DEVICE_STATE));
65 assertEquals(expectedFanSpeed, msg.convertToState(CHANNEL_FAN_SPEED, DEVICE_STATE));
67 assertEquals(packetType, msg.getPacketType());
69 byte[] decoded = msg.decodeMessage();
71 assertEquals(hexMsg, HexUtils.bytesToHex(decoded), "Message converted back");
75 public void testSomeMessages() throws RFXComException {
76 testMessage("0817060052D4000500", 0, "5428224", 0, null, OnOffType.ON, null, FAN);
80 public void testCommandOn() throws RFXComException {
81 testCommand(CASAFAN, CHANNEL_COMMAND, OnOffType.ON, OnOffType.ON, UnDefType.UNDEF, StringType.valueOf("MED"),
82 StringType.valueOf("MED"), FAN);
86 public void testCommandOff() throws RFXComException {
87 testCommand(CASAFAN, CHANNEL_COMMAND, OnOffType.OFF, OnOffType.OFF, UnDefType.UNDEF, StringType.valueOf("OFF"),
88 StringType.valueOf("OFF"), FAN);
92 public void testFanSpeedStringOff() throws RFXComException {
93 testFanSpeedString("OFF", OnOffType.OFF, StringType.valueOf("OFF"));
97 public void testFanSpeedStringHi() throws RFXComException {
98 testFanSpeedString("HI", OnOffType.ON, StringType.valueOf("HI"));
102 public void testFanSpeedStringMed() throws RFXComException {
103 testFanSpeedString("MED", OnOffType.ON, StringType.valueOf("MED"));
107 public void testFanSpeedStringLow() throws RFXComException {
108 testFanSpeedString("LOW", OnOffType.ON, StringType.valueOf("LOW"));
112 public void testFanLightOn() throws RFXComException {
113 testCommand(CASAFAN, CHANNEL_FAN_LIGHT, OnOffType.ON, null, OnOffType.ON, null, StringType.valueOf("LIGHT"),
117 private void testFanSpeedString(String value, OnOffType expectedCommand, State expectedFanSpeed)
118 throws RFXComException {
119 testCommand(CASAFAN, CHANNEL_FAN_SPEED, StringType.valueOf(value), expectedCommand, UnDefType.UNDEF,
120 expectedFanSpeed, expectedFanSpeed, FAN);
123 static void testCommand(RFXComFanMessage.SubType subType, String channel, State inputValue,
124 @Nullable OnOffType expectedCommand, State expectedLightCommand, @Nullable State expectedFanSpeed,
125 State expectedCommandString, RFXComBaseMessage.PacketType packetType) throws RFXComException {
126 RFXComFanMessage msg = new RFXComFanMessage();
128 msg.setSubType(subType);
129 msg.convertFromState(channel, inputValue);
131 assertValues(msg, expectedCommand, expectedLightCommand, expectedFanSpeed, packetType, expectedCommandString);
133 RFXComFanMessage result = new RFXComFanMessage();
134 result.encodeMessage(msg.decodeMessage());
136 assertValues(msg, expectedCommand, expectedLightCommand, expectedFanSpeed, packetType, expectedCommandString);
139 private static void assertValues(RFXComFanMessage msg, @Nullable OnOffType expectedCommand,
140 State expectedLightCommand, @Nullable State expectedFanSpeed, RFXComBaseMessage.PacketType packetType,
141 State expectedCommandString) throws RFXComException {
142 assertEquals(expectedCommand, msg.convertToState(CHANNEL_COMMAND, DEVICE_STATE));
143 assertEquals(expectedLightCommand, msg.convertToState(CHANNEL_FAN_LIGHT, DEVICE_STATE));
144 assertEquals(expectedFanSpeed, msg.convertToState(CHANNEL_FAN_SPEED, DEVICE_STATE));
145 assertEquals(expectedCommandString, msg.convertToState(CHANNEL_COMMAND_STRING, DEVICE_STATE));
146 assertEquals(packetType, msg.getPacketType());