]> git.basschouten.com Git - openhab-addons.git/blob
6eea21da322c13aac1b6d7df34e15d2d24cfdfee
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.jupiter.api.Assertions.assertEquals;
16 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
17 import static org.openhab.binding.rfxcom.internal.RFXComTestHelper.thingUID;
18 import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType.FAN;
19 import static org.openhab.binding.rfxcom.internal.messages.RFXComFanMessage.SubType.CASAFAN;
20
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.RFXComBindingConstants;
25 import org.openhab.binding.rfxcom.internal.RFXComTestHelper;
26 import org.openhab.binding.rfxcom.internal.config.RFXComGenericDeviceConfiguration;
27 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.types.State;
32 import org.openhab.core.types.UnDefType;
33 import org.openhab.core.util.HexUtils;
34
35 /**
36  * Test for RFXCom-binding
37  *
38  * @author Martin van Wingerden - Initial contribution
39  */
40 @NonNullByDefault
41 public class RFXComFanMessageTest {
42     private static RFXComGenericDeviceConfiguration config = new RFXComGenericDeviceConfiguration();
43     private static ChannelUID fanSpeedChannelUID = new ChannelUID(thingUID, RFXComBindingConstants.CHANNEL_FAN_SPEED);
44     private static StringType fanSpeedOff = StringType.valueOf("OFF");
45
46     static {
47         config.deviceId = "5428224";
48         config.subType = RFXComFanMessage.SubType.CASAFAN.toString();
49     }
50
51     private static final MockDeviceState DEVICE_STATE = new MockDeviceState();
52
53     @Test
54     public void checkForSupportTest() throws RFXComException {
55         RFXComMessageFactoryImpl.INSTANCE.createMessage(FAN, config, fanSpeedChannelUID, fanSpeedOff);
56     }
57
58     @Test
59     public void basicBoundaryCheck() throws RFXComException {
60         RFXComFanMessage message = (RFXComFanMessage) RFXComMessageFactoryImpl.INSTANCE.createMessage(FAN, config,
61                 fanSpeedChannelUID, fanSpeedOff);
62
63         RFXComTestHelper.basicBoundaryCheck(FAN, message);
64     }
65
66     private void testMessage(String hexMsg, int seqNbr, String deviceId, int signalLevel,
67             @Nullable State expectedCommand, State expectedLightCommand, @Nullable State expectedFanSpeed,
68             RFXComBaseMessage.PacketType packetType) throws RFXComException {
69         final RFXComFanMessage msg = (RFXComFanMessage) RFXComMessageFactoryImpl.INSTANCE
70                 .createMessage(HexUtils.hexToBytes(hexMsg));
71         assertEquals(seqNbr, (short) (msg.seqNbr & 0xFF), "Seq Number");
72         assertEquals(deviceId, msg.getDeviceId(), "Sensor Id");
73         assertEquals(signalLevel, msg.signalLevel, "Signal Level");
74
75         assertEquals(expectedCommand, msg.convertToState(CHANNEL_COMMAND, config, DEVICE_STATE));
76         assertEquals(expectedLightCommand, msg.convertToState(CHANNEL_FAN_LIGHT, config, DEVICE_STATE));
77         assertEquals(expectedFanSpeed, msg.convertToState(CHANNEL_FAN_SPEED, config, DEVICE_STATE));
78
79         assertEquals(packetType, msg.getPacketType());
80
81         byte[] decoded = msg.decodeMessage();
82
83         assertEquals(hexMsg, HexUtils.bytesToHex(decoded), "Message converted back");
84     }
85
86     @Test
87     public void testSomeMessages() throws RFXComException {
88         testMessage("0817060052D4000500", 0, "5428224", 0, null, OnOffType.ON, null, FAN);
89     }
90
91     @Test
92     public void testCommandOn() throws RFXComException {
93         testCommand(CASAFAN, CHANNEL_COMMAND, OnOffType.ON, OnOffType.ON, UnDefType.UNDEF, StringType.valueOf("MED"),
94                 StringType.valueOf("MED"), FAN);
95     }
96
97     @Test
98     public void testCommandOff() throws RFXComException {
99         testCommand(CASAFAN, CHANNEL_COMMAND, OnOffType.OFF, OnOffType.OFF, UnDefType.UNDEF, StringType.valueOf("OFF"),
100                 StringType.valueOf("OFF"), FAN);
101     }
102
103     @Test
104     public void testFanSpeedStringOff() throws RFXComException {
105         testFanSpeedString("OFF", OnOffType.OFF, StringType.valueOf("OFF"));
106     }
107
108     @Test
109     public void testFanSpeedStringHi() throws RFXComException {
110         testFanSpeedString("HI", OnOffType.ON, StringType.valueOf("HI"));
111     }
112
113     @Test
114     public void testFanSpeedStringMed() throws RFXComException {
115         testFanSpeedString("MED", OnOffType.ON, StringType.valueOf("MED"));
116     }
117
118     @Test
119     public void testFanSpeedStringLow() throws RFXComException {
120         testFanSpeedString("LOW", OnOffType.ON, StringType.valueOf("LOW"));
121     }
122
123     @Test
124     public void testFanLightOn() throws RFXComException {
125         testCommand(CASAFAN, CHANNEL_FAN_LIGHT, OnOffType.ON, null, OnOffType.ON, null, StringType.valueOf("LIGHT"),
126                 FAN);
127     }
128
129     private void testFanSpeedString(String value, OnOffType expectedCommand, State expectedFanSpeed)
130             throws RFXComException {
131         testCommand(CASAFAN, CHANNEL_FAN_SPEED, StringType.valueOf(value), expectedCommand, UnDefType.UNDEF,
132                 expectedFanSpeed, expectedFanSpeed, FAN);
133     }
134
135     static void testCommand(RFXComFanMessage.SubType subType, String channel, State inputValue,
136             @Nullable OnOffType expectedCommand, State expectedLightCommand, @Nullable State expectedFanSpeed,
137             State expectedCommandString, RFXComBaseMessage.PacketType packetType) throws RFXComException {
138         RFXComFanMessage msg = new RFXComFanMessage();
139
140         msg.setSubType(subType);
141         msg.convertFromState(channel, inputValue);
142
143         assertValues(msg, expectedCommand, expectedLightCommand, expectedFanSpeed, packetType, expectedCommandString);
144
145         RFXComFanMessage result = new RFXComFanMessage();
146         result.encodeMessage(msg.decodeMessage());
147
148         assertValues(msg, expectedCommand, expectedLightCommand, expectedFanSpeed, packetType, expectedCommandString);
149     }
150
151     private static void assertValues(RFXComFanMessage msg, @Nullable OnOffType expectedCommand,
152             State expectedLightCommand, @Nullable State expectedFanSpeed, RFXComBaseMessage.PacketType packetType,
153             State expectedCommandString) throws RFXComException {
154         assertEquals(expectedCommand, msg.convertToState(CHANNEL_COMMAND, config, DEVICE_STATE));
155         assertEquals(expectedLightCommand, msg.convertToState(CHANNEL_FAN_LIGHT, config, DEVICE_STATE));
156         assertEquals(expectedFanSpeed, msg.convertToState(CHANNEL_FAN_SPEED, config, DEVICE_STATE));
157         assertEquals(expectedCommandString, msg.convertToState(CHANNEL_COMMAND_STRING, config, DEVICE_STATE));
158         assertEquals(packetType, msg.getPacketType());
159     }
160 }