]> git.basschouten.com Git - openhab-addons.git/blob
76cac0294543ccffe10fc4d090bc18a7f9b3e749
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.junit.jupiter.api.Test;
20 import org.openhab.binding.rfxcom.internal.config.RFXComGenericDeviceConfiguration;
21 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
22 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
23 import org.openhab.binding.rfxcom.internal.messages.RFXComLighting1Message.Commands;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.OpenClosedType;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.util.HexUtils;
28
29 /**
30  * Test for RFXCom-binding
31  *
32  * @author Martin van Wingerden - Initial contribution
33  */
34 @NonNullByDefault
35 public class RFXComLighting1MessageTest {
36     private final MockDeviceState deviceState = new MockDeviceState();
37     private static final RFXComGenericDeviceConfiguration config = new RFXComGenericDeviceConfiguration();
38
39     static {
40         config.deviceId = "A.1";
41         config.subType = RFXComLighting1Message.SubType.ARC.toString();
42     }
43
44     private void testMessage(String hexMsg, RFXComLighting1Message.SubType subType, int seqNbr, String deviceId,
45             byte signalLevel, Commands command, String commandString) throws RFXComException {
46         final RFXComLighting1Message msg = (RFXComLighting1Message) RFXComMessageFactoryImpl.INSTANCE
47                 .createMessage(HexUtils.hexToBytes(hexMsg));
48         assertEquals(subType, msg.subType, "SubType");
49         assertEquals(seqNbr, (short) (msg.seqNbr & 0xFF), "Seq Number");
50         assertEquals(deviceId, msg.getDeviceId(), "Sensor Id");
51         assertEquals(signalLevel, msg.signalLevel, "Signal Level");
52         assertEquals(command, msg.command, "Command");
53
54         RFXComGenericDeviceConfiguration config = new RFXComGenericDeviceConfiguration();
55         config.deviceId = deviceId;
56         config.subType = subType.toString();
57         assertEquals(commandString, msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState).toString(),
58                 "Command String");
59
60         byte[] decoded = msg.decodeMessage();
61
62         assertEquals(hexMsg, HexUtils.bytesToHex(decoded), "Message converted back");
63     }
64
65     @Test
66     public void testSomeMessages() throws RFXComException {
67         testMessage("0710015242080780", RFXComLighting1Message.SubType.ARC, 82, "B.8", (byte) 8, Commands.CHIME,
68                 "CHIME");
69
70         testMessage("0710010047010070", RFXComLighting1Message.SubType.ARC, 0, "G.1", (byte) 7, Commands.OFF, "OFF");
71         testMessage("071001014D090160", RFXComLighting1Message.SubType.ARC, 1, "M.9", (byte) 6, Commands.ON, "ON");
72         testMessage("0710010543080060", RFXComLighting1Message.SubType.ARC, 5, "C.8", (byte) 6, Commands.OFF, "OFF");
73         testMessage("0710010B43080160", RFXComLighting1Message.SubType.ARC, 11, "C.8", (byte) 6, Commands.ON, "ON");
74
75         testMessage("0710000843010150", RFXComLighting1Message.SubType.X10, 8, "C.1", (byte) 5, Commands.ON, "ON");
76         testMessage("0710007F41010000", RFXComLighting1Message.SubType.X10, 127, "A.1", (byte) 0, Commands.OFF, "OFF");
77         testMessage("0710009A41010170", RFXComLighting1Message.SubType.X10, 154, "A.1", (byte) 7, Commands.ON, "ON");
78     }
79
80     @Test
81     public void testCommandStringOff() throws RFXComUnsupportedChannelException {
82         RFXComLighting1Message msg = new RFXComLighting1Message();
83
84         msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("OFF"));
85
86         assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
87         assertEquals(OpenClosedType.CLOSED, msg.convertToState(CHANNEL_CONTACT, config, deviceState));
88         assertEquals(StringType.valueOf("OFF"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
89     }
90
91     @Test
92     public void testCommandStringChime() throws RFXComUnsupportedChannelException {
93         RFXComLighting1Message msg = new RFXComLighting1Message();
94
95         msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("chime"));
96
97         assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
98         assertEquals(OpenClosedType.OPEN, msg.convertToState(CHANNEL_CONTACT, config, deviceState));
99         assertEquals(StringType.valueOf("CHIME"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
100     }
101
102     @Test
103     public void testCommandStringBright() throws RFXComUnsupportedChannelException {
104         RFXComLighting1Message msg = new RFXComLighting1Message();
105
106         msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("bright"));
107
108         assertEquals(OnOffType.ON, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
109         assertEquals(OpenClosedType.OPEN, msg.convertToState(CHANNEL_CONTACT, config, deviceState));
110         assertEquals(StringType.valueOf("BRIGHT"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
111     }
112
113     @Test
114     public void testCommandStringDim() throws RFXComUnsupportedChannelException {
115         RFXComLighting1Message msg = new RFXComLighting1Message();
116
117         msg.convertFromState(CHANNEL_COMMAND_STRING, StringType.valueOf("dim"));
118
119         assertEquals(OnOffType.OFF, msg.convertToState(CHANNEL_COMMAND, config, deviceState));
120         assertEquals(OpenClosedType.CLOSED, msg.convertToState(CHANNEL_CONTACT, config, deviceState));
121         assertEquals(StringType.valueOf("DIM"), msg.convertToState(CHANNEL_COMMAND_STRING, config, deviceState));
122     }
123 }