]> git.basschouten.com Git - openhab-addons.git/blob
02b6bdeeb398bcd595ef708eda6a9915747f5fe1
[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.homematic.internal.communicator.virtual;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import java.io.IOException;
19
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.homematic.internal.misc.HomematicClientException;
23 import org.openhab.binding.homematic.internal.misc.HomematicConstants;
24 import org.openhab.binding.homematic.internal.misc.MiscUtils;
25 import org.openhab.binding.homematic.internal.model.HmChannel;
26 import org.openhab.binding.homematic.internal.model.HmDatapoint;
27 import org.openhab.binding.homematic.internal.model.HmDevice;
28 import org.openhab.binding.homematic.internal.model.HmInterface;
29 import org.openhab.binding.homematic.internal.model.HmParamsetType;
30 import org.openhab.binding.homematic.internal.model.HmValueType;
31 import org.openhab.core.test.java.JavaTest;
32 import org.openhab.core.thing.CommonTriggerEvents;
33
34 /**
35  * Tests for {@link ButtonVirtualDatapointHandler}.
36  *
37  * @author Michael Reitler - Initial Contribution
38  *
39  */
40 public class ButtonDatapointTest extends JavaTest {
41
42     private static final int DISABLE_DATAPOINT_DELAY = 50;
43
44     private MockEventReceiver mockEventReceiver;
45     private final ButtonVirtualDatapointHandler bvdpHandler = new ButtonVirtualDatapointHandler();
46
47     @BeforeEach
48     public void setup() throws IOException {
49         this.mockEventReceiver = new MockEventReceiver();
50     }
51
52     @Test
53     public void testShortPress() throws IOException, HomematicClientException {
54         HmDatapoint shortPressDp = createPressDatapoint("PRESS_SHORT", Boolean.TRUE);
55         HmDatapoint buttonVirtualDatapoint = getButtonVirtualDatapoint(shortPressDp);
56
57         mockEventReceiver.eventReceived(shortPressDp);
58
59         assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.SHORT_PRESSED));
60     }
61
62     @Test
63     public void testLongPressHm() throws IOException, HomematicClientException {
64         HmDatapoint longPressDp = createPressDatapoint("PRESS_LONG", Boolean.TRUE);
65         HmDatapoint buttonVirtualDatapoint = getButtonVirtualDatapoint(longPressDp);
66
67         mockEventReceiver.eventReceived(longPressDp);
68         assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.LONG_PRESSED));
69
70         HmDatapoint contPressDp = createPressDatapointFrom(longPressDp, "PRESS_CONT", Boolean.TRUE);
71         mockEventReceiver.eventReceived(contPressDp);
72         assertThat(buttonVirtualDatapoint.getValue(), is("LONG_REPEATED"));
73
74         HmDatapoint releaseDp = createPressDatapointFrom(longPressDp, "PRESS_LONG_RELEASE", Boolean.TRUE);
75         mockEventReceiver.eventReceived(releaseDp);
76         assertThat(buttonVirtualDatapoint.getValue(), is("LONG_RELEASED"));
77     }
78
79     @Test
80     public void testLongPressHmIp() throws IOException, HomematicClientException {
81         HmDatapoint longPressDp = createPressDatapoint("PRESS_LONG_START", Boolean.TRUE);
82         HmDatapoint buttonVirtualDatapoint = getButtonVirtualDatapoint(longPressDp);
83
84         mockEventReceiver.eventReceived(longPressDp);
85         assertThat(buttonVirtualDatapoint.getValue(), is(CommonTriggerEvents.LONG_PRESSED));
86
87         HmDatapoint contPressDp = createPressDatapointFrom(longPressDp, "PRESS_LONG", Boolean.TRUE);
88         mockEventReceiver.eventReceived(contPressDp);
89         assertThat(buttonVirtualDatapoint.getValue(), is("LONG_REPEATED"));
90
91         HmDatapoint releaseDp = createPressDatapointFrom(longPressDp, "PRESS_LONG_RELEASE", Boolean.TRUE);
92         mockEventReceiver.eventReceived(releaseDp);
93         assertThat(buttonVirtualDatapoint.getValue(), is("LONG_RELEASED"));
94     }
95
96     @Test
97     public void testUnsupportedEvents() throws IOException, HomematicClientException {
98         HmDatapoint contPressDp = createPressDatapoint("PRESS_CONT", Boolean.TRUE);
99         HmDatapoint contButtonVirtualDatapoint = getButtonVirtualDatapoint(contPressDp);
100
101         mockEventReceiver.eventReceived(contPressDp);
102
103         HmDatapoint releaseDp = createPressDatapoint("PRESS_LONG_RELEASE", Boolean.TRUE);
104         HmDatapoint releaseButtonVirtualDatapoint = getButtonVirtualDatapoint(releaseDp);
105
106         mockEventReceiver.eventReceived(releaseDp);
107
108         HmDatapoint crapDp = createPressDatapoint("CRAP", Boolean.TRUE);
109         HmDatapoint crapButtonVirtualDatapoint = getButtonVirtualDatapoint(crapDp);
110
111         mockEventReceiver.eventReceived(crapDp);
112
113         // CONT and LONG_RELEASE events without previous LONG event are supposed to yield no trigger
114         assertThat(contButtonVirtualDatapoint.getValue(), nullValue());
115         assertThat(releaseButtonVirtualDatapoint.getValue(), nullValue());
116         assertThat(crapButtonVirtualDatapoint, nullValue());
117     }
118
119     private HmDatapoint createPressDatapoint(String channelName, Object value) {
120         HmDatapoint pressDp = new HmDatapoint(channelName, "", HmValueType.ACTION, value, true, HmParamsetType.VALUES);
121         HmChannel hmChannel = new HmChannel(channelName, 1);
122         HmDevice device = new HmDevice("ABC12345", HmInterface.RF, "HM-MOCK", "mockid", "mockid", "mockfw");
123         hmChannel.setDevice(device);
124         device.addChannel(hmChannel);
125         hmChannel.addDatapoint(pressDp);
126         pressDp.setChannel(hmChannel);
127         bvdpHandler.initialize(device);
128
129         return pressDp;
130     }
131
132     private HmDatapoint createPressDatapointFrom(HmDatapoint originalDatapoint, String channelName, Object value) {
133         HmDatapoint pressDp = new HmDatapoint(channelName, "", HmValueType.ACTION, value, true, HmParamsetType.VALUES);
134         HmChannel hmChannel = originalDatapoint.getChannel();
135         hmChannel.addDatapoint(pressDp);
136         pressDp.setChannel(hmChannel);
137
138         return pressDp;
139     }
140
141     private HmDatapoint getButtonVirtualDatapoint(HmDatapoint originalDatapoint) {
142         return originalDatapoint.getChannel().getDatapoints().stream()
143                 .filter(dp -> HomematicConstants.VIRTUAL_DATAPOINT_NAME_BUTTON.equals(dp.getName())).findFirst()
144                 .orElse(null);
145     }
146
147     /**
148      * Mock parts of {@linkplain org.openhab.binding.homematic.internal.communicator.AbstractHomematicGateway}
149      */
150     private class MockEventReceiver {
151
152         public void eventReceived(HmDatapoint dp) throws IOException, HomematicClientException {
153             if (bvdpHandler.canHandleEvent(dp)) {
154                 bvdpHandler.handleEvent(null, dp);
155             }
156             if (dp.isPressDatapoint() && MiscUtils.isTrueValue(dp.getValue())) {
157                 disableDatapoint(dp);
158             }
159         }
160
161         private void disableDatapoint(HmDatapoint dp) {
162             new Thread(() -> {
163                 try {
164                     Thread.sleep(DISABLE_DATAPOINT_DELAY);
165                     dp.setValue(Boolean.FALSE);
166                 } catch (InterruptedException e) {
167                 }
168             }).start();
169         }
170     }
171 }