]> git.basschouten.com Git - openhab-addons.git/blob
8dfaf7f54fae578149e05883a196b1765b4c0659
[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.dmx.internal.handler;
14
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.core.Is.is;
17 import static org.hamcrest.number.IsCloseTo.closeTo;
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.openhab.binding.dmx.internal.DmxBindingConstants.*;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.openhab.binding.dmx.test.AbstractDmxThingTestParent;
29 import org.openhab.core.config.core.Configuration;
30 import org.openhab.core.library.types.DecimalType;
31 import org.openhab.core.library.types.HSBType;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.library.types.PercentType;
34 import org.openhab.core.thing.Bridge;
35 import org.openhab.core.thing.ChannelUID;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingUID;
38 import org.openhab.core.thing.binding.builder.ChannelBuilder;
39 import org.openhab.core.thing.binding.builder.ThingBuilder;
40
41 /**
42  * Tests cases for {@link org.openhab.binding.dmx.internal.handler.ColorThingHandler} in RGB mode.
43  *
44  * @author Jan N. Klug - Initial contribution
45  */
46 @NonNullByDefault
47 public class ColorThingHandlerTest extends AbstractDmxThingTestParent {
48     private static final String TEST_CHANNEL_CONFIG = "100/3";
49     private static final int TEST_FADE_TIME = 1500;
50     private static final HSBType TEST_COLOR = new HSBType(new DecimalType(280), new PercentType(100),
51             new PercentType(100));
52     private static final ThingUID THING_UID_DIMMER = new ThingUID(THING_TYPE_COLOR, "testdimmer");
53     private static final ChannelUID CHANNEL_UID_COLOR = new ChannelUID(THING_UID_DIMMER, CHANNEL_COLOR);
54     private static final ChannelUID CHANNEL_UID_BRIGHTNESS_R = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_R);
55     private static final ChannelUID CHANNEL_UID_BRIGHTNESS_G = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_G);
56     private static final ChannelUID CHANNEL_UID_BRIGHTNESS_B = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_B);
57
58     private @NonNullByDefault({}) Map<String, Object> thingProperties;
59     private @NonNullByDefault({}) Thing dimmerThing;
60     private @NonNullByDefault({}) ColorThingHandler dimmerThingHandler;
61
62     @BeforeEach
63     public void setUp() {
64         super.setup();
65         thingProperties = new HashMap<>();
66         thingProperties.put(CONFIG_DMX_ID, TEST_CHANNEL_CONFIG);
67         thingProperties.put(CONFIG_DIMMER_FADE_TIME, TEST_FADE_TIME);
68         thingProperties.put(CONFIG_DIMMER_TURNONVALUE, "255,128,0");
69         thingProperties.put(CONFIG_DIMMER_DYNAMICTURNONVALUE, true);
70         dimmerThing = ThingBuilder.create(THING_TYPE_COLOR, "testdimmer").withLabel("Dimmer Thing")
71                 .withBridge(bridge.getUID()).withConfiguration(new Configuration(thingProperties))
72                 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_R, "Brightness R")
73                         .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
74                 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_G, "Brightness G")
75                         .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
76                 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_B, "Brightness B")
77                         .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
78                 .withChannel(ChannelBuilder.create(CHANNEL_UID_COLOR, "Color").withType(COLOR_CHANNEL_TYPEUID).build())
79                 .build();
80         dimmerThingHandler = new ColorThingHandler(dimmerThing) {
81             @Override
82             protected @Nullable Bridge getBridge() {
83                 return bridge;
84             }
85         };
86         initializeHandler(dimmerThingHandler);
87     }
88
89     @Test
90     public void testThingStatus() {
91         assertThingStatus(dimmerThing);
92     }
93
94     @Test
95     public void testThingStatusNoBridge() {
96         // check that thing is offline if no bridge found
97         ColorThingHandler dimmerHandlerWithoutBridge = new ColorThingHandler(dimmerThing) {
98             @Override
99             protected @Nullable Bridge getBridge() {
100                 return null;
101             }
102         };
103         assertThingStatusWithoutBridge(dimmerHandlerWithoutBridge);
104     }
105
106     @Test
107     public void testOnOffCommand() {
108         // on
109         long currentTime = System.currentTimeMillis();
110
111         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, OnOffType.ON);
112         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
113
114         waitForAssert(() -> {
115             assertChannelStateUpdate(CHANNEL_UID_COLOR, state -> assertEquals(OnOffType.ON, state.as(OnOffType.class)));
116             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R, state -> assertEquals(PercentType.HUNDRED, state));
117             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G,
118                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(50.0, 0.5))));
119             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B, state -> assertEquals(PercentType.ZERO, state));
120         });
121
122         // off
123         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, OnOffType.OFF);
124         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
125
126         waitForAssert(() -> {
127             assertChannelStateUpdate(CHANNEL_UID_COLOR,
128                     state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
129             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R, state -> assertEquals(PercentType.ZERO, state));
130             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G, state -> assertEquals(PercentType.ZERO, state));
131             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B, state -> assertEquals(PercentType.ZERO, state));
132         });
133     }
134
135     @Test
136     public void testDynamicTurnOnValue() {
137         long currentTime = System.currentTimeMillis();
138
139         // turn on with arbitrary value
140         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, TEST_COLOR);
141         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
142
143         waitForAssert(() -> {
144             assertChannelStateUpdate(CHANNEL_UID_COLOR,
145                     state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 2))));
146             assertChannelStateUpdate(CHANNEL_UID_COLOR,
147                     state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 1))));
148             assertChannelStateUpdate(CHANNEL_UID_COLOR,
149                     state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(100.0, 1))));
150         });
151
152         // turn off and hopefully store last value
153         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, OnOffType.OFF);
154         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
155
156         waitForAssert(() -> {
157             assertChannelStateUpdate(CHANNEL_UID_COLOR,
158                     state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(0.0, 1))));
159         });
160
161         // turn on and restore value
162         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, OnOffType.ON);
163         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
164
165         waitForAssert(() -> {
166             assertChannelStateUpdate(CHANNEL_UID_COLOR,
167                     state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 2))));
168             assertChannelStateUpdate(CHANNEL_UID_COLOR,
169                     state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 1))));
170             assertChannelStateUpdate(CHANNEL_UID_COLOR,
171                     state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(100.0, 1))));
172         });
173     }
174
175     @Test
176     public void testPercentTypeCommand() {
177         assertPercentTypeCommands(dimmerThingHandler, CHANNEL_UID_COLOR, TEST_FADE_TIME);
178     }
179
180     @Test
181     public void testColorCommand() {
182         // setting of color
183         long currentTime = System.currentTimeMillis();
184
185         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, TEST_COLOR);
186         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
187
188         waitForAssert(() -> {
189             assertChannelStateUpdate(CHANNEL_UID_COLOR,
190                     state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 1))));
191             assertChannelStateUpdate(CHANNEL_UID_COLOR,
192                     state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 0.5))));
193             assertChannelStateUpdate(CHANNEL_UID_COLOR,
194                     state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(100.0, 0.5))));
195             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R,
196                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(66.5, 0.5))));
197             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G, state -> assertEquals(PercentType.ZERO, state));
198             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B, state -> assertEquals(PercentType.HUNDRED, state));
199         });
200
201         // color dimming
202         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, new PercentType(30));
203         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
204
205         waitForAssert(() -> {
206             assertChannelStateUpdate(CHANNEL_UID_COLOR,
207                     state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 2))));
208             assertChannelStateUpdate(CHANNEL_UID_COLOR,
209                     state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 1))));
210             assertChannelStateUpdate(CHANNEL_UID_COLOR,
211                     state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(30.0, 1))));
212             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R,
213                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(19.2, 0.5))));
214             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G, state -> assertEquals(PercentType.ZERO, state));
215             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B,
216                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(29.8, 0.5))));
217         });
218     }
219 }