]> git.basschouten.com Git - openhab-addons.git/blob
02ac032cdca637af73d3d0fbe53675ec62be8faf
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 testLongPress() 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 testUnsupportedEvents() throws IOException, HomematicClientException {
81         HmDatapoint contPressDp = createPressDatapoint("PRESS_CONT", Boolean.TRUE);
82         HmDatapoint contButtonVirtualDatapoint = getButtonVirtualDatapoint(contPressDp);
83
84         mockEventReceiver.eventReceived(contPressDp);
85
86         HmDatapoint releaseDp = createPressDatapoint("PRESS_LONG_RELEASE", Boolean.TRUE);
87         HmDatapoint releaseButtonVirtualDatapoint = getButtonVirtualDatapoint(releaseDp);
88
89         mockEventReceiver.eventReceived(releaseDp);
90
91         HmDatapoint crapDp = createPressDatapoint("CRAP", Boolean.TRUE);
92         HmDatapoint crapButtonVirtualDatapoint = getButtonVirtualDatapoint(crapDp);
93
94         mockEventReceiver.eventReceived(crapDp);
95
96         // CONT and LONG_RELEASE events without previous LONG event are supposed to yield no trigger
97         assertThat(contButtonVirtualDatapoint.getValue(), nullValue());
98         assertThat(releaseButtonVirtualDatapoint.getValue(), nullValue());
99         assertThat(crapButtonVirtualDatapoint, nullValue());
100     }
101
102     private HmDatapoint createPressDatapoint(String channelName, Object value) {
103         HmDatapoint pressDp = new HmDatapoint(channelName, "", HmValueType.ACTION, value, true, HmParamsetType.VALUES);
104         HmChannel hmChannel = new HmChannel(channelName, 1);
105         HmDevice device = new HmDevice("ABC12345", HmInterface.RF, "HM-MOCK", "mockid", "mockid", "mockfw");
106         hmChannel.setDevice(device);
107         device.addChannel(hmChannel);
108         hmChannel.addDatapoint(pressDp);
109         pressDp.setChannel(hmChannel);
110         bvdpHandler.initialize(device);
111
112         return pressDp;
113     }
114
115     private HmDatapoint createPressDatapointFrom(HmDatapoint originalDatapoint, String channelName, Object value) {
116         HmDatapoint pressDp = new HmDatapoint(channelName, "", HmValueType.ACTION, value, true, HmParamsetType.VALUES);
117         HmChannel hmChannel = originalDatapoint.getChannel();
118         hmChannel.addDatapoint(pressDp);
119         pressDp.setChannel(hmChannel);
120
121         return pressDp;
122     }
123
124     private HmDatapoint getButtonVirtualDatapoint(HmDatapoint originalDatapoint) {
125         return originalDatapoint.getChannel().getDatapoints().stream()
126                 .filter(dp -> HomematicConstants.VIRTUAL_DATAPOINT_NAME_BUTTON.equals(dp.getName())).findFirst()
127                 .orElse(null);
128     }
129
130     /**
131      * Mock parts of {@linkplain org.openhab.binding.homematic.internal.communicator.AbstractHomematicGateway}
132      */
133     private class MockEventReceiver {
134
135         public void eventReceived(HmDatapoint dp) throws IOException, HomematicClientException {
136             if (bvdpHandler.canHandleEvent(dp)) {
137                 bvdpHandler.handleEvent(null, dp);
138             }
139             if (dp.isPressDatapoint() && MiscUtils.isTrueValue(dp.getValue())) {
140                 disableDatapoint(dp);
141             }
142         }
143
144         private void disableDatapoint(HmDatapoint dp) {
145             new Thread(() -> {
146                 try {
147                     Thread.sleep(DISABLE_DATAPOINT_DELAY);
148                     dp.setValue(Boolean.FALSE);
149                 } catch (InterruptedException e) {
150                 }
151             }).start();
152         }
153     }
154 }