]> git.basschouten.com Git - openhab-addons.git/blob
2507bf64b7cc117631e3341a7abcbed70048b772
[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 java.math.BigDecimal;
16
17 import org.junit.jupiter.api.BeforeEach;
18 import org.junit.jupiter.api.Test;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.library.types.IncreaseDecreaseType;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.library.types.PercentType;
23 import org.openhab.core.library.types.StopMoveType;
24
25 /**
26  * Test class for (@link LxControlValueSelector}
27  *
28  * @author Pawel Pieczul - initial contribution
29  *
30  */
31 public class LxControlValueSelectorTest extends LxControlTest {
32     private static final String NUMBER_CHANNEL = " / Number";
33
34     @BeforeEach
35     public void setup() {
36         setupControl("432a7b7e-0022-3aac-ffff403fb0c34b9e", "0b734138-037d-034e-ffff403fb0c34b9e",
37                 "0fe650c2-0004-d446-ffff504f9410790f", "Selection Switch");
38     }
39
40     @Test
41     public void testControlCreation() {
42         testControlCreation(LxControlValueSelector.class, 2, 0, 2, 2, 4);
43     }
44
45     @Test
46     public void testChannels() {
47         testChannel("Dimmer");
48         testChannel("Number", NUMBER_CHANNEL);
49     }
50
51     @Test
52     public void testLoxoneValueMinMaxChanges() {
53         // filling in missing state values
54         testChannelState(null);
55         changeLoxoneState("step", 1.0);
56         testChannelState(null);
57         changeLoxoneState("value", 50.0);
58         testChannelState(null);
59         changeLoxoneState("min", 0.0);
60         testChannelState(null);
61         changeLoxoneState("max", 100.0);
62         testChannelState(new PercentType(50));
63         testChannel("Dimmer", null, BigDecimal.ZERO, new BigDecimal(100), BigDecimal.ONE, "%.0f", false, null);
64         testChannel("Number", NUMBER_CHANNEL, BigDecimal.ZERO, new BigDecimal(100), BigDecimal.ONE, "%.0f", false,
65                 null);
66
67         // potential division by zero
68         changeLoxoneState("min", 55.0);
69         changeLoxoneState("max", 55.0);
70         testChannelState(null);
71
72         changeLoxoneState("min", 200.0);
73         changeLoxoneState("max", 400.0);
74         testChannel("Dimmer", null, new BigDecimal(200), new BigDecimal(400), BigDecimal.ONE, "%.0f", false, null);
75         testChannel("Number", NUMBER_CHANNEL, new BigDecimal(200), new BigDecimal(400), BigDecimal.ONE, "%.0f", false,
76                 null);
77
78         // out of range
79         changeLoxoneState("value", 199.9);
80         testChannelState(null);
81         changeLoxoneState("value", 400.1);
82         testChannelState(null);
83         changeLoxoneState("value", 0.0);
84         testChannelState(null);
85         // scaling within range
86         changeLoxoneState("value", 200.0);
87         testChannelState(PercentType.ZERO);
88         changeLoxoneState("value", 400.0);
89         testChannelState(PercentType.HUNDRED);
90         changeLoxoneState("value", 300.0);
91         testChannelState(new PercentType(50));
92         // reversed range boundaries
93         changeLoxoneState("min", 50.0);
94         changeLoxoneState("max", 20.0);
95         changeLoxoneState("value", 30.0);
96         testChannelState(null);
97     }
98
99     @Test
100     public void testOnOffPercentCommands() {
101         changeLoxoneState("min", 1000.0);
102         changeLoxoneState("max", 3000.0);
103         changeLoxoneState("step", 100.0);
104
105         executeCommand(OnOffType.ON);
106         testAction("3000.0");
107         executeCommand(OnOffType.OFF);
108         testAction("1000.0");
109
110         executeCommand(PercentType.HUNDRED);
111         testAction("3000.0");
112         executeCommand(new PercentType(50));
113         testAction("2000.0");
114         executeCommand(new PercentType(1));
115         testAction("1020.0");
116         executeCommand(PercentType.ZERO);
117         testAction("1000.0");
118
119         executeCommand(StopMoveType.MOVE);
120         testAction(null);
121     }
122
123     @Test
124     public void testIncreaseDecreaseCommands() {
125         changeLoxoneState("min", 123.0);
126         changeLoxoneState("max", 456.0);
127         changeLoxoneState("step", 23.0);
128         changeLoxoneState("value", 400.0);
129         testChannelState(new PercentType(83));
130         executeCommand(IncreaseDecreaseType.INCREASE);
131         testAction("423.0");
132         changeLoxoneState("value", 423.0);
133         testChannelState(new PercentType(90));
134         executeCommand(IncreaseDecreaseType.INCREASE);
135         testAction("446.0");
136         changeLoxoneState("value", 446.0);
137         testChannelState(new PercentType(96));
138         executeCommand(IncreaseDecreaseType.INCREASE);
139         testAction("456.0"); // trim to max
140         changeLoxoneState("value", 456.0);
141         testChannelState(PercentType.HUNDRED);
142         changeLoxoneState("step", 100.0);
143         executeCommand(IncreaseDecreaseType.DECREASE);
144         testAction("356.0");
145         changeLoxoneState("value", 356.0);
146         testChannelState(new PercentType(69));
147         executeCommand(IncreaseDecreaseType.DECREASE);
148         testAction("256.0");
149         changeLoxoneState("value", 256.0);
150         testChannelState(new PercentType(39));
151         executeCommand(IncreaseDecreaseType.DECREASE);
152         testAction("156.0");
153         changeLoxoneState("value", 156.0);
154         testChannelState(new PercentType(9));
155         executeCommand(IncreaseDecreaseType.DECREASE);
156         testAction("123.0"); // trim to min
157         changeLoxoneState("value", 123.0);
158         testChannelState(PercentType.ZERO);
159     }
160
161     @Test
162     public void testNumberCommands() {
163         changeLoxoneState("min", 100.0);
164         changeLoxoneState("max", 300.0);
165         changeLoxoneState("step", 10.0);
166         for (Double i = 0.0; i < 100.0; i += 0.35) {
167             executeCommand(NUMBER_CHANNEL, new DecimalType(i));
168             testAction(null);
169         }
170         for (Double i = 100.0; i <= 300.0; i += 0.47) {
171             executeCommand(NUMBER_CHANNEL, new DecimalType(i));
172             testAction(i.toString());
173         }
174         for (Double i = 300.01; i < 400.0; i += 0.59) {
175             executeCommand(NUMBER_CHANNEL, new DecimalType(i));
176             testAction(null);
177         }
178     }
179
180     @Test
181     public void testLoxoneNumberChanges() {
182         testChannelState(null);
183         changeLoxoneState("min", 100.0);
184         changeLoxoneState("max", 300.0);
185         changeLoxoneState("step", 10.0);
186         for (Double i = 0.0; i < 100.0; i += 0.35) {
187             changeLoxoneState("value", i);
188             testChannelState(NUMBER_CHANNEL, null);
189         }
190         for (Double i = 100.0; i <= 300.0; i += 0.47) {
191             changeLoxoneState("value", i);
192             testChannelState(NUMBER_CHANNEL, new DecimalType(i));
193         }
194         for (Double i = 300.01; i < 400.0; i += 0.59) {
195             changeLoxoneState("value", i);
196             testChannelState(NUMBER_CHANNEL, null);
197         }
198     }
199 }