]> git.basschouten.com Git - openhab-addons.git/blob
c8e404085f615614b75f70dba27770826d31f2ad
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.rfxcom.internal.messages;
14
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;
19
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;
29
30 /**
31  * Test for RFXCom-binding
32  *
33  * @author Martin van Wingerden - Initial contribution
34  */
35 @NonNullByDefault
36 public class RFXComFanMessageTest {
37     private static final MockDeviceState DEVICE_STATE = new MockDeviceState();
38
39     @Test
40     public void checkForSupportTest() throws RFXComException {
41         RFXComMessageFactory.createMessage(FAN);
42     }
43
44     @Test
45     public void basicBoundaryCheck() throws RFXComException {
46         RFXComFanMessage message = (RFXComFanMessage) RFXComMessageFactory.createMessage(FAN);
47
48         message.setSubType(RFXComFanMessage.SubType.CASAFAN);
49         message.convertFromState(CHANNEL_FAN_SPEED, StringType.valueOf("OFF"));
50
51         RFXComTestHelper.basicBoundaryCheck(FAN, message);
52     }
53
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);
61
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));
65
66         assertEquals(packetType, msg.getPacketType());
67
68         byte[] decoded = msg.decodeMessage();
69
70         assertEquals("Message converted back", hexMsg, HexUtils.bytesToHex(decoded));
71     }
72
73     @Test
74     public void testSomeMessages() throws RFXComException {
75         testMessage("0817060052D4000500", 0, "5428224", 0, null, OnOffType.ON, null, FAN);
76     }
77
78     @Test
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);
82     }
83
84     @Test
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);
88     }
89
90     @Test
91     public void testFanSpeedStringOff() throws RFXComException {
92         testFanSpeedString("OFF", OnOffType.OFF, StringType.valueOf("OFF"));
93     }
94
95     @Test
96     public void testFanSpeedStringHi() throws RFXComException {
97         testFanSpeedString("HI", OnOffType.ON, StringType.valueOf("HI"));
98     }
99
100     @Test
101     public void testFanSpeedStringMed() throws RFXComException {
102         testFanSpeedString("MED", OnOffType.ON, StringType.valueOf("MED"));
103     }
104
105     @Test
106     public void testFanSpeedStringLow() throws RFXComException {
107         testFanSpeedString("LOW", OnOffType.ON, StringType.valueOf("LOW"));
108     }
109
110     @Test
111     public void testFanLightOn() throws RFXComException {
112         testCommand(CASAFAN, CHANNEL_FAN_LIGHT, OnOffType.ON, null, OnOffType.ON, null, StringType.valueOf("LIGHT"),
113                 FAN);
114     }
115
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);
120     }
121
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();
126
127         msg.setSubType(subType);
128         msg.convertFromState(channel, inputValue);
129
130         assertValues(msg, expectedCommand, expectedLightCommand, expectedFanSpeed, packetType, expectedCommandString);
131
132         RFXComFanMessage result = new RFXComFanMessage();
133         result.encodeMessage(msg.decodeMessage());
134
135         assertValues(msg, expectedCommand, expectedLightCommand, expectedFanSpeed, packetType, expectedCommandString);
136     }
137
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());
146     }
147 }