]> git.basschouten.com Git - openhab-addons.git/blob
c422a11b6274e4237a2a1e546e4e2bb4dcd45cd6
[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.tplinksmarthome.internal.device;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.junit.jupiter.api.Assertions.assertSame;
17 import static org.junit.jupiter.api.Assertions.assertTrue;
18 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.CHANNEL_UID_BRIGHTNESS;
19 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.CHANNEL_UID_COLOR;
20 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.CHANNEL_UID_COLOR_TEMPERATURE;
21 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.CHANNEL_UID_COLOR_TEMPERATURE_ABS;
22 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.CHANNEL_UID_ENERGY_POWER;
23 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.CHANNEL_UID_OTHER;
24 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.CHANNEL_UID_SWITCH;
25 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeThingType.KL430;
26
27 import java.io.IOException;
28
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.openhab.binding.tplinksmarthome.internal.model.ModelTestUtil;
33 import org.openhab.core.library.types.DecimalType;
34 import org.openhab.core.library.types.HSBType;
35 import org.openhab.core.library.types.OnOffType;
36 import org.openhab.core.library.types.PercentType;
37 import org.openhab.core.library.types.QuantityType;
38 import org.openhab.core.library.unit.Units;
39 import org.openhab.core.types.UnDefType;
40
41 /**
42  * Test class for {@link BulbDevice} class.
43  *
44  * @author Hilbrand Bouwkamp - Initial contribution
45  */
46 @NonNullByDefault
47 public class LightStripDeviceTest extends DeviceTestBase<LightStripDevice> {
48
49     private static final String DEVICE_OFF = "bulb_get_sysinfo_response_off";
50
51     public LightStripDeviceTest() throws IOException {
52         super(new LightStripDevice(KL430), "bulb_get_sysinfo_response_on");
53     }
54
55     @BeforeEach
56     @Override
57     public void setUp() throws IOException {
58         super.setUp();
59     }
60
61     @Test
62     public void testHandleCommandBrightness() throws IOException {
63         assertInput("kl430_set_brightness");
64         setSocketReturnAssert("kl430_set_brightness_response");
65         assertTrue(device.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(73)),
66                 "Brightness channel should be handled");
67     }
68
69     @Test
70     public void testHandleCommandBrightnessOnOff() throws IOException {
71         assertInput("kl430_set_on");
72         setSocketReturnAssert("kl430_set_brightness_response");
73         assertTrue(device.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON),
74                 "Brightness channel with OnOff state should be handled");
75     }
76
77     @Test
78     public void testHandleCommandColor() throws IOException {
79         assertInput("kl430_set_color");
80         setSocketReturnAssert("kl430_set_color_response");
81         assertTrue(device.handleCommand(CHANNEL_UID_COLOR, new HSBType("115,75,73")),
82                 "Color channel should be handled");
83     }
84
85     public void testHandleCommandColorBrightness() throws IOException {
86         assertInput("kl430_set_brightness");
87         setSocketReturnAssert("kl430_set_brightness_response");
88         assertTrue(device.handleCommand(CHANNEL_UID_COLOR, new PercentType(33)),
89                 "Color channel with Percentage state (=brightness) should be handled");
90     }
91
92     public void testHandleCommandColorOnOff() throws IOException {
93         assertInput("bulb_transition_light_state_on");
94         assertTrue(device.handleCommand(CHANNEL_UID_COLOR, OnOffType.ON),
95                 "Color channel with OnOff state should be handled");
96     }
97
98     @Test
99     public void testHandleCommandColorTemperature() throws IOException {
100         assertInput("kl430_set_colortemperature");
101         setSocketReturnAssert("kl430_set_colortemperature_response");
102         assertTrue(device.handleCommand(CHANNEL_UID_COLOR_TEMPERATURE, new PercentType(40)),
103                 "Color temperature channel should be handled");
104     }
105
106     @Test
107     public void testHandleCommandColorTemperatureAbs() throws IOException {
108         assertInput("kl430_set_colortemperature");
109         setSocketReturnAssert("kl430_set_colortemperature_response");
110         assertTrue(device.handleCommand(CHANNEL_UID_COLOR_TEMPERATURE_ABS, new DecimalType(5100)),
111                 "Color temperature channel should be handled");
112     }
113
114     @Test
115     public void testHandleCommandColorTemperatureOnOff() throws IOException {
116         assertInput("kl430_set_on");
117         setSocketReturnAssert("kl430_set_colortemperature_response");
118         assertTrue(device.handleCommand(CHANNEL_UID_COLOR_TEMPERATURE, OnOffType.ON),
119                 "Color temperature channel with OnOff state should be handled");
120     }
121
122     // ---- Update ----
123
124     @Test
125     public void testUpdateChannelBrightnessOn() {
126         assertEquals(new PercentType(92), device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState),
127                 "Brightness should be on");
128     }
129
130     @Test
131     public void testUpdateChannelBrightnessOff() throws IOException {
132         deviceState = new DeviceState(ModelTestUtil.readJson(DEVICE_OFF));
133         assertEquals(PercentType.ZERO, device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState),
134                 "Brightness should be off");
135     }
136
137     @Test
138     public void testUpdateChannelColorOn() {
139         assertEquals(new HSBType("7,44,92"), device.updateChannel(CHANNEL_UID_COLOR, deviceState),
140                 "Color should be on");
141     }
142
143     @Test
144     public void testUpdateChannelColorOff() throws IOException {
145         deviceState = new DeviceState(ModelTestUtil.readJson(DEVICE_OFF));
146         assertEquals(new HSBType("7,44,0"), device.updateChannel(CHANNEL_UID_COLOR, deviceState),
147                 "Color should be off");
148     }
149
150     @Test
151     public void testUpdateChannelSwitchOn() {
152         assertSame(OnOffType.ON, device.updateChannel(CHANNEL_UID_SWITCH, deviceState), "Switch should be on");
153     }
154
155     @Test
156     public void testUpdateChannelSwitchOff() throws IOException {
157         deviceState = new DeviceState(ModelTestUtil.readJson(DEVICE_OFF));
158         assertSame(OnOffType.OFF, device.updateChannel(CHANNEL_UID_SWITCH, deviceState), "Switch should be off");
159     }
160
161     @Test
162     public void testUpdateChannelColorTemperature() throws IOException {
163         assertInput("kl430_set_colortemperature");
164         assertEquals(new PercentType(2), device.updateChannel(CHANNEL_UID_COLOR_TEMPERATURE, deviceState),
165                 "Color temperature should be set");
166     }
167
168     @Test
169     public void testUpdateChannelColorTemperatureAbs() throws IOException {
170         assertInput("kl430_set_colortemperature");
171         assertEquals(new DecimalType(2630), device.updateChannel(CHANNEL_UID_COLOR_TEMPERATURE_ABS, deviceState),
172                 "Color temperature should be set");
173     }
174
175     @Test
176     public void testUpdateChannelOther() {
177         assertSame(UnDefType.UNDEF, device.updateChannel(CHANNEL_UID_OTHER, deviceState),
178                 "Unknown channel should return UNDEF");
179     }
180
181     @Test
182     public void testUpdateChannelPower() {
183         assertEquals(new QuantityType<>(10.8, Units.WATT), device.updateChannel(CHANNEL_UID_ENERGY_POWER, deviceState),
184                 "Power values should be set");
185     }
186 }