2 * Copyright (c) 2010-2020 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.Assert.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.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 RFXComMessageFactory.createMessage(FAN);
45 public void basicBoundaryCheck() throws RFXComException {
46 RFXComFanMessage message = (RFXComFanMessage) RFXComMessageFactory.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) RFXComMessageFactory.createMessage(HexUtils.hexToBytes(hexMsg));
58 assertEquals("Seq Number", seqNbr, (short) (msg.seqNbr & 0xFF));
59 assertEquals("Sensor Id", deviceId, msg.getDeviceId());
60 assertEquals("Signal Level", signalLevel, msg.signalLevel);
62 assertEquals(expectedCommand, msg.convertToState(CHANNEL_COMMAND, DEVICE_STATE));
63 assertEquals(expectedLightCommand, msg.convertToState(CHANNEL_FAN_LIGHT, DEVICE_STATE));
64 assertEquals(expectedFanSpeed, msg.convertToState(CHANNEL_FAN_SPEED, DEVICE_STATE));
66 assertEquals(packetType, msg.getPacketType());
68 byte[] decoded = msg.decodeMessage();
70 assertEquals("Message converted back", hexMsg, HexUtils.bytesToHex(decoded));
74 public void testSomeMessages() throws RFXComException {
75 testMessage("0817060052D4000500", 0, "5428224", 0, null, OnOffType.ON, null, FAN);
79 public void testCommandOn() throws RFXComException {
80 testCommand(CASAFAN, CHANNEL_COMMAND, OnOffType.ON, OnOffType.ON, UnDefType.UNDEF, StringType.valueOf("MED"),
81 StringType.valueOf("MED"), FAN);
85 public void testCommandOff() throws RFXComException {
86 testCommand(CASAFAN, CHANNEL_COMMAND, OnOffType.OFF, OnOffType.OFF, UnDefType.UNDEF, StringType.valueOf("OFF"),
87 StringType.valueOf("OFF"), FAN);
91 public void testFanSpeedStringOff() throws RFXComException {
92 testFanSpeedString("OFF", OnOffType.OFF, StringType.valueOf("OFF"));
96 public void testFanSpeedStringHi() throws RFXComException {
97 testFanSpeedString("HI", OnOffType.ON, StringType.valueOf("HI"));
101 public void testFanSpeedStringMed() throws RFXComException {
102 testFanSpeedString("MED", OnOffType.ON, StringType.valueOf("MED"));
106 public void testFanSpeedStringLow() throws RFXComException {
107 testFanSpeedString("LOW", OnOffType.ON, StringType.valueOf("LOW"));
111 public void testFanLightOn() throws RFXComException {
112 testCommand(CASAFAN, CHANNEL_FAN_LIGHT, OnOffType.ON, null, OnOffType.ON, null, StringType.valueOf("LIGHT"),
116 private void testFanSpeedString(String value, OnOffType expectedCommand, State expectedFanSpeed)
117 throws RFXComException {
118 testCommand(CASAFAN, CHANNEL_FAN_SPEED, StringType.valueOf(value), expectedCommand, UnDefType.UNDEF,
119 expectedFanSpeed, expectedFanSpeed, FAN);
122 static void testCommand(RFXComFanMessage.SubType subType, String channel, State inputValue,
123 @Nullable OnOffType expectedCommand, State expectedLightCommand, @Nullable State expectedFanSpeed,
124 State expectedCommandString, RFXComBaseMessage.PacketType packetType) throws RFXComException {
125 RFXComFanMessage msg = new RFXComFanMessage();
127 msg.setSubType(subType);
128 msg.convertFromState(channel, inputValue);
130 assertValues(msg, expectedCommand, expectedLightCommand, expectedFanSpeed, packetType, expectedCommandString);
132 RFXComFanMessage result = new RFXComFanMessage();
133 result.encodeMessage(msg.decodeMessage());
135 assertValues(msg, expectedCommand, expectedLightCommand, expectedFanSpeed, packetType, expectedCommandString);
138 private static void assertValues(RFXComFanMessage msg, @Nullable OnOffType expectedCommand,
139 State expectedLightCommand, @Nullable State expectedFanSpeed, RFXComBaseMessage.PacketType packetType,
140 State expectedCommandString) throws RFXComException {
141 assertEquals(expectedCommand, msg.convertToState(CHANNEL_COMMAND, DEVICE_STATE));
142 assertEquals(expectedLightCommand, msg.convertToState(CHANNEL_FAN_LIGHT, DEVICE_STATE));
143 assertEquals(expectedFanSpeed, msg.convertToState(CHANNEL_FAN_SPEED, DEVICE_STATE));
144 assertEquals(expectedCommandString, msg.convertToState(CHANNEL_COMMAND_STRING, DEVICE_STATE));
145 assertEquals(packetType, msg.getPacketType());