2 * Copyright (c) 2010-2023 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.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;
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;
36 * Test for RFXCom-binding
38 * @author Martin van Wingerden - Initial contribution
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");
47 config.deviceId = "5428224";
48 config.subType = RFXComFanMessage.SubType.CASAFAN.toString();
51 private static final MockDeviceState DEVICE_STATE = new MockDeviceState();
54 public void checkForSupportTest() throws RFXComException {
55 RFXComMessageFactoryImpl.INSTANCE.createMessage(FAN, config, fanSpeedChannelUID, fanSpeedOff);
59 public void basicBoundaryCheck() throws RFXComException {
60 RFXComFanMessage message = (RFXComFanMessage) RFXComMessageFactoryImpl.INSTANCE.createMessage(FAN, config,
61 fanSpeedChannelUID, fanSpeedOff);
63 RFXComTestHelper.basicBoundaryCheck(FAN, message);
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");
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));
79 assertEquals(packetType, msg.getPacketType());
81 byte[] decoded = msg.decodeMessage();
83 assertEquals(hexMsg, HexUtils.bytesToHex(decoded), "Message converted back");
87 public void testSomeMessages() throws RFXComException {
88 testMessage("0817060052D4000500", 0, "5428224", 0, null, OnOffType.ON, null, FAN);
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);
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);
104 public void testFanSpeedStringOff() throws RFXComException {
105 testFanSpeedString("OFF", OnOffType.OFF, StringType.valueOf("OFF"));
109 public void testFanSpeedStringHi() throws RFXComException {
110 testFanSpeedString("HI", OnOffType.ON, StringType.valueOf("HI"));
114 public void testFanSpeedStringMed() throws RFXComException {
115 testFanSpeedString("MED", OnOffType.ON, StringType.valueOf("MED"));
119 public void testFanSpeedStringLow() throws RFXComException {
120 testFanSpeedString("LOW", OnOffType.ON, StringType.valueOf("LOW"));
124 public void testFanLightOn() throws RFXComException {
125 testCommand(CASAFAN, CHANNEL_FAN_LIGHT, OnOffType.ON, null, OnOffType.ON, null, StringType.valueOf("LIGHT"),
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);
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();
140 msg.setSubType(subType);
141 msg.convertFromState(channel, inputValue);
143 assertValues(msg, expectedCommand, expectedLightCommand, expectedFanSpeed, packetType, expectedCommandString);
145 RFXComFanMessage result = new RFXComFanMessage();
146 result.encodeMessage(msg.decodeMessage());
148 assertValues(msg, expectedCommand, expectedLightCommand, expectedFanSpeed, packetType, expectedCommandString);
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());