]> git.basschouten.com Git - openhab-addons.git/blob
d42070afa7c0d5bfd9d35dd0b458d0cb201780b5
[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.loxone.internal.controls;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.time.LocalDateTime;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.core.library.types.DateTimeType;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.StringType;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.UnDefType;
31
32 /**
33  * Test class for (@link LxControlAlarm} - version for alarm without presence sensors
34  *
35  * @author Pawel Pieczul - initial contribution
36  *
37  */
38 public class LxControlAlarmNoPresenceTest extends LxControlTest {
39     static final String ARM_DELAYED_CHANNEL = " / Arm Delayed";
40     static final String NEXT_LEVEL_CHANNEL = " / Next Level";
41     static final String NEXT_LEVEL_DELAY_CHANNEL = " / Next Level Delay";
42     static final String NEXT_LEVEL_DELAY_TOTAL_CHANNEL = " / Next Level Delay Total";
43     static final String LEVEL_CHANNEL = " / Level";
44     static final String START_TIME_CHANNEL = " / Start Time";
45     static final String ARMED_DELAY_CHANNEL = " / Armed Delay";
46     static final String ARMED_TOTAL_DELAY_CHANNEL = " / Armed Total Delay";
47     static final String SENSORS_CHANNEL = " / Sensors";
48     static final String QUIT_CHANNEL = " / Acknowledge";
49
50     private static final String NUMBER_CHANNELS[] = { NEXT_LEVEL_CHANNEL, NEXT_LEVEL_DELAY_CHANNEL,
51             NEXT_LEVEL_DELAY_TOTAL_CHANNEL, LEVEL_CHANNEL, ARMED_DELAY_CHANNEL, ARMED_TOTAL_DELAY_CHANNEL };
52
53     @BeforeEach
54     public void setup() {
55         setupControl("233d5db0-0333-5865-ffff403fb0c34b9e", "0b734138-037d-034e-ffff403fb0c34b9e",
56                 "0fe650c2-0004-d446-ffff504f9410790f", "Burglar Alarm No Presence");
57     }
58
59     @Test
60     public void testControlCreation() {
61         testControlCreation(LxControlAlarm.class, 2, 1, 11, 12, 10);
62     }
63
64     @Test
65     public void testChannels() {
66         // read-write channels
67         testChannel("Switch");
68         testChannel("Switch", ARM_DELAYED_CHANNEL);
69         testChannel("Switch", QUIT_CHANNEL, null, null, null, null, true, null);
70         // read-only channels
71         testChannel("Number", NEXT_LEVEL_CHANNEL);
72         testChannel("Number", NEXT_LEVEL_DELAY_CHANNEL);
73         testChannel("Number", NEXT_LEVEL_DELAY_TOTAL_CHANNEL);
74         testChannel("Number", LEVEL_CHANNEL);
75         testChannel("Number", ARMED_DELAY_CHANNEL);
76         testChannel("Number", ARMED_TOTAL_DELAY_CHANNEL);
77         testChannel("String", SENSORS_CHANNEL);
78         testChannel("DateTime", START_TIME_CHANNEL);
79     }
80
81     @Test
82     public void testCommandsDefaultChannel() {
83         testAction(null);
84         for (int i = 0; i < 20; i++) {
85             changeLoxoneState("disabledmove", 0.0);
86             executeCommand(OnOffType.ON);
87             testAction("on");
88             executeCommand(OnOffType.OFF);
89             testAction("off");
90             changeLoxoneState("disabledmove", 1.0);
91             executeCommand(OnOffType.ON);
92             testAction("on");
93             executeCommand(OnOffType.OFF);
94             testAction("off");
95         }
96     }
97
98     @Test
99     public void testCommandsArmWithDelayChannel() {
100         testAction(null);
101         for (int i = 0; i < 20; i++) {
102             changeLoxoneState("disabledmove", 0.0);
103             executeCommand(ARM_DELAYED_CHANNEL, OnOffType.ON);
104             testAction("delayedon");
105             executeCommand(ARM_DELAYED_CHANNEL, OnOffType.OFF);
106             testAction("off");
107             changeLoxoneState("disabledmove", 1.0);
108             executeCommand(ARM_DELAYED_CHANNEL, OnOffType.ON);
109             testAction("delayedon");
110             executeCommand(ARM_DELAYED_CHANNEL, OnOffType.OFF);
111             testAction("off");
112         }
113     }
114
115     @Test
116     public void testCommandsQuitChannel() {
117         testAction(null);
118         for (int i = 0; i < 20; i++) {
119             executeCommand(QUIT_CHANNEL, OnOffType.ON);
120             testAction("quit");
121             executeCommand(QUIT_CHANNEL, OnOffType.OFF);
122             testAction(null);
123         }
124     }
125
126     @Test
127     public void testNumberChannels() {
128         testNumberChannel(NEXT_LEVEL_CHANNEL, "nextlevel");
129         testNumberChannel(NEXT_LEVEL_DELAY_CHANNEL, "nextleveldelay");
130         testNumberChannel(NEXT_LEVEL_DELAY_TOTAL_CHANNEL, "nextleveldelaytotal");
131         testNumberChannel(LEVEL_CHANNEL, "level");
132         testNumberChannel(ARMED_DELAY_CHANNEL, "armeddelay");
133         testNumberChannel(ARMED_TOTAL_DELAY_CHANNEL, "armeddelaytotal");
134     }
135
136     @Test
137     public void testStartedTimeChannel() {
138         changeLoxoneState("starttime", "2019-11-18 14:54:21");
139         LocalDateTime ldt = LocalDateTime.of(2019, 11, 18, 14, 54, 21);
140         ZonedDateTime dt = ldt.atZone(ZoneId.systemDefault());
141         testChannelState(START_TIME_CHANNEL, new DateTimeType(dt));
142
143         changeLoxoneState("starttime", "something else");
144         testChannelState(START_TIME_CHANNEL, null);
145
146         changeLoxoneState("starttime", "1981-01-02 03:04:05");
147         ldt = LocalDateTime.of(1981, 1, 2, 3, 4, 5);
148         dt = ldt.atZone(ZoneId.systemDefault());
149         testChannelState(START_TIME_CHANNEL, new DateTimeType(dt));
150
151         changeLoxoneState("starttime", "1981-13-02 03:04:05");
152         testChannelState(START_TIME_CHANNEL, null);
153     }
154
155     @Test
156     public void testSensorsChannel() {
157         testChannelState(SENSORS_CHANNEL, null);
158         for (int i = 0; i < 20; i++) {
159             changeLoxoneState("sensors", "test sensors channel string " + i);
160             testChannelState(SENSORS_CHANNEL, new StringType("test sensors channel string " + i));
161         }
162     }
163
164     @Test
165     public void testLevelAndAcknowledge() {
166         changeLoxoneState("level", 0.0);
167         testChannel("Switch", QUIT_CHANNEL, null, null, null, null, true, null);
168         for (Double i = 1.0; i <= 6.0; i++) {
169             changeLoxoneState("level", i);
170             testChannel("Switch", QUIT_CHANNEL, null, null, null, null, false, null);
171             changeLoxoneState("level", 0.0);
172             testChannel("Switch", QUIT_CHANNEL, null, null, null, null, true, null);
173         }
174     }
175
176     private void testNumberChannel(String channel, String state) {
177         Map<String, State> states = new HashMap<>();
178         for (String s : NUMBER_CHANNELS) {
179             states.put(s, getChannelState(s));
180         }
181         for (Double i = -100.0; i <= 100.0; i += 2.341) {
182             changeLoxoneState(state, i);
183             testChannelState(channel, new DecimalType(i));
184             states.entrySet().stream().filter(v -> !v.getKey().equals(channel)).forEach(v -> {
185                 String key = v.getKey();
186                 assertEquals(states.get(key), getChannelState(key));
187             });
188         }
189         changeLoxoneState(state, Double.NaN);
190         testChannelState(channel, UnDefType.UNDEF);
191     }
192 }