2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.tplinksmarthome.internal.device;
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;
27 import java.io.IOException;
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;
42 * Test class for {@link BulbDevice} class.
44 * @author Hilbrand Bouwkamp - Initial contribution
47 public class LightStripDeviceTest extends DeviceTestBase<LightStripDevice> {
49 private static final String DEVICE_OFF = "bulb_get_sysinfo_response_off";
51 public LightStripDeviceTest() throws IOException {
52 super(new LightStripDevice(KL430), "bulb_get_sysinfo_response_on");
57 public void setUp() throws IOException {
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");
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");
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");
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");
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");
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");
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");
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");
125 public void testUpdateChannelBrightnessOn() {
126 assertEquals(new PercentType(92), device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState),
127 "Brightness should be on");
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");
138 public void testUpdateChannelColorOn() {
139 assertEquals(new HSBType("7,44,92"), device.updateChannel(CHANNEL_UID_COLOR, deviceState),
140 "Color should be on");
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");
151 public void testUpdateChannelSwitchOn() {
152 assertSame(OnOffType.ON, device.updateChannel(CHANNEL_UID_SWITCH, deviceState), "Switch should be on");
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");
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");
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");
176 public void testUpdateChannelOther() {
177 assertSame(UnDefType.UNDEF, device.updateChannel(CHANNEL_UID_OTHER, deviceState),
178 "Unknown channel should return UNDEF");
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");