]> git.basschouten.com Git - openhab-addons.git/blob
a4a2d216f6244b9df469662b8282dade79597307
[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.LB130;
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 BulbDeviceTest extends DeviceTestBase<BulbDevice> {
48
49     private static final String DEVICE_OFF = "bulb_get_sysinfo_response_off";
50
51     public BulbDeviceTest() throws IOException {
52         super(new BulbDevice(LB130), "bulb_get_sysinfo_response_on");
53     }
54
55     @BeforeEach
56     @Override
57     public void setUp() throws IOException {
58         super.setUp();
59         setSocketReturnAssert("bulb_transition_light_state_response");
60     }
61
62     @Test
63     public void testHandleCommandBrightness() throws IOException {
64         assertInput("bulb_transition_light_state_brightness");
65         assertTrue(device.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(33)),
66                 "Brightness channel should be handled");
67     }
68
69     @Test
70     public void testHandleCommandBrightnessOnOff() throws IOException {
71         assertInput("bulb_transition_light_state_on");
72         assertTrue(device.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON),
73                 "Brightness channel with OnOff state should be handled");
74     }
75
76     @Test
77     public void testHandleCommandColor() throws IOException {
78         assertInput("bulb_transition_light_state_color");
79         assertTrue(device.handleCommand(CHANNEL_UID_COLOR, new HSBType("55,44,33")), "Color channel should be handled");
80     }
81
82     public void testHandleCommandColorBrightness() throws IOException {
83         assertInput("bulb_transition_light_state_brightness");
84         assertTrue(device.handleCommand(CHANNEL_UID_COLOR, new PercentType(33)),
85                 "Color channel with Percentage state (=brightness) should be handled");
86     }
87
88     public void testHandleCommandColorOnOff() throws IOException {
89         assertInput("bulb_transition_light_state_on");
90         assertTrue(device.handleCommand(CHANNEL_UID_COLOR, OnOffType.ON),
91                 "Color channel with OnOff state should be handled");
92     }
93
94     @Test
95     public void testHandleCommandColorTemperature() throws IOException {
96         assertInput("bulb_transition_light_state_color_temp");
97         assertTrue(device.handleCommand(CHANNEL_UID_COLOR_TEMPERATURE, new PercentType(40)),
98                 "Color temperature channel should be handled");
99     }
100
101     @Test
102     public void testHandleCommandColorTemperatureAbs() throws IOException {
103         assertInput("bulb_transition_light_state_color_temp");
104         assertTrue(device.handleCommand(CHANNEL_UID_COLOR_TEMPERATURE_ABS, new DecimalType(5100)),
105                 "Color temperature channel should be handled");
106     }
107
108     @Test
109     public void testHandleCommandColorTemperatureOnOff() throws IOException {
110         assertInput("bulb_transition_light_state_on");
111         assertTrue(device.handleCommand(CHANNEL_UID_COLOR_TEMPERATURE, OnOffType.ON),
112                 "Color temperature channel with OnOff state should be handled");
113     }
114
115     @Test
116     public void testHandleCommandSwitch() throws IOException {
117         assertInput("bulb_transition_light_state_on");
118         assertTrue(device.handleCommand(CHANNEL_UID_SWITCH, OnOffType.ON), "Switch channel should be handled");
119     }
120
121     @Test
122     public void testUpdateChannelBrightnessOn() {
123         assertEquals(new PercentType(92), device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState),
124                 "Brightness should be on");
125     }
126
127     @Test
128     public void testUpdateChannelBrightnessOff() throws IOException {
129         deviceState = new DeviceState(ModelTestUtil.readJson(DEVICE_OFF));
130         assertEquals(PercentType.ZERO, device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState),
131                 "Brightness should be off");
132     }
133
134     @Test
135     public void testUpdateChannelColorOn() {
136         assertEquals(new HSBType("7,44,92"), device.updateChannel(CHANNEL_UID_COLOR, deviceState),
137                 "Color should be on");
138     }
139
140     @Test
141     public void testUpdateChannelColorOff() throws IOException {
142         deviceState = new DeviceState(ModelTestUtil.readJson(DEVICE_OFF));
143         assertEquals(new HSBType("7,44,0"), device.updateChannel(CHANNEL_UID_COLOR, deviceState),
144                 "Color should be off");
145     }
146
147     @Test
148     public void testUpdateChannelSwitchOn() {
149         assertSame(OnOffType.ON, device.updateChannel(CHANNEL_UID_SWITCH, deviceState), "Switch should be on");
150     }
151
152     @Test
153     public void testUpdateChannelSwitchOff() throws IOException {
154         deviceState = new DeviceState(ModelTestUtil.readJson(DEVICE_OFF));
155         assertSame(OnOffType.OFF, device.updateChannel(CHANNEL_UID_SWITCH, deviceState), "Switch should be off");
156     }
157
158     @Test
159     public void testUpdateChannelColorTemperature() {
160         assertEquals(new PercentType(2), device.updateChannel(CHANNEL_UID_COLOR_TEMPERATURE, deviceState),
161                 "Color temperature should be set");
162     }
163
164     @Test
165     public void testUpdateChannelColorTemperatureAbs() {
166         assertEquals(new DecimalType(2630), device.updateChannel(CHANNEL_UID_COLOR_TEMPERATURE_ABS, deviceState),
167                 "Color temperature should be set");
168     }
169
170     @Test
171     public void testUpdateChannelOther() {
172         assertSame(UnDefType.UNDEF, device.updateChannel(CHANNEL_UID_OTHER, deviceState),
173                 "Unknown channel should return UNDEF");
174     }
175
176     @Test
177     public void testUpdateChannelPower() {
178         assertEquals(new QuantityType<>(10.8, Units.WATT), device.updateChannel(CHANNEL_UID_ENERGY_POWER, deviceState),
179                 "Power values should be set");
180     }
181 }