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.LB130;
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 BulbDeviceTest extends DeviceTestBase<BulbDevice> {
49 private static final String DEVICE_OFF = "bulb_get_sysinfo_response_off";
51 public BulbDeviceTest() throws IOException {
52 super(new BulbDevice(LB130), "bulb_get_sysinfo_response_on");
57 public void setUp() throws IOException {
59 setSocketReturnAssert("bulb_transition_light_state_response");
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");
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");
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");
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");
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");
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");
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");
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");
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");
122 public void testUpdateChannelBrightnessOn() {
123 assertEquals(new PercentType(92), device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState),
124 "Brightness should be on");
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");
135 public void testUpdateChannelColorOn() {
136 assertEquals(new HSBType("7,44,92"), device.updateChannel(CHANNEL_UID_COLOR, deviceState),
137 "Color should be on");
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");
148 public void testUpdateChannelSwitchOn() {
149 assertSame(OnOffType.ON, device.updateChannel(CHANNEL_UID_SWITCH, deviceState), "Switch should be on");
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");
159 public void testUpdateChannelColorTemperature() {
160 assertEquals(new PercentType(2), device.updateChannel(CHANNEL_UID_COLOR_TEMPERATURE, deviceState),
161 "Color temperature should be set");
165 public void testUpdateChannelColorTemperatureAbs() {
166 assertEquals(new DecimalType(2630), device.updateChannel(CHANNEL_UID_COLOR_TEMPERATURE_ABS, deviceState),
167 "Color temperature should be set");
171 public void testUpdateChannelOther() {
172 assertSame(UnDefType.UNDEF, device.updateChannel(CHANNEL_UID_OTHER, deviceState),
173 "Unknown channel should return UNDEF");
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");