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.boschshc.internal.devices.smartbulb;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.*;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.Captor;
26 import org.openhab.binding.boschshc.internal.devices.AbstractBoschSHCDeviceHandlerTest;
27 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
28 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
29 import org.openhab.binding.boschshc.internal.services.binaryswitch.dto.BinarySwitchServiceState;
30 import org.openhab.binding.boschshc.internal.services.hsbcoloractuator.dto.HSBColorActuatorServiceState;
31 import org.openhab.binding.boschshc.internal.services.multilevelswitch.dto.MultiLevelSwitchServiceState;
32 import org.openhab.core.library.types.HSBType;
33 import org.openhab.core.library.types.OnOffType;
34 import org.openhab.core.library.types.PercentType;
35 import org.openhab.core.thing.ChannelUID;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.ThingUID;
39 import com.google.gson.JsonElement;
40 import com.google.gson.JsonParser;
43 * Unit tests for {@link SmartBulbHandler}.
45 * @author David Pace - Initial contribution
49 class SmartBulbHandlerTest extends AbstractBoschSHCDeviceHandlerTest<SmartBulbHandler> {
51 private @Captor @NonNullByDefault({}) ArgumentCaptor<BinarySwitchServiceState> binarySwitchServiceStateCaptor;
53 private @Captor @NonNullByDefault({}) ArgumentCaptor<MultiLevelSwitchServiceState> multiLevelSwitchServiceStateCaptor;
55 private @Captor @NonNullByDefault({}) ArgumentCaptor<HSBColorActuatorServiceState> hsbColorActuatorServiceStateCaptor;
58 protected SmartBulbHandler createFixture() {
59 return new SmartBulbHandler(getThing());
63 protected String getDeviceID() {
64 return "hdm:ZigBee:f0d1b80000f2a3e9";
68 protected ThingTypeUID getThingTypeUID() {
69 return BoschSHCBindingConstants.THING_TYPE_SMART_BULB;
73 void testHandleCommandBinarySwitch()
74 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
75 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_POWER_SWITCH),
77 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("BinarySwitch"),
78 binarySwitchServiceStateCaptor.capture());
79 BinarySwitchServiceState state = binarySwitchServiceStateCaptor.getValue();
82 getFixture().handleCommand(new ChannelUID(new ThingUID(getThingTypeUID(), "abcdef"),
83 BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.OFF);
84 verify(getBridgeHandler(), times(2)).putState(eq(getDeviceID()), eq("BinarySwitch"),
85 binarySwitchServiceStateCaptor.capture());
86 state = binarySwitchServiceStateCaptor.getValue();
87 assertFalse(state.on);
91 void testHandleCommandMultiLevelSwitch()
92 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
93 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_BRIGHTNESS),
95 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("MultiLevelSwitch"),
96 multiLevelSwitchServiceStateCaptor.capture());
97 MultiLevelSwitchServiceState state = multiLevelSwitchServiceStateCaptor.getValue();
98 assertEquals(42, state.level);
102 void testHandleCommandHSBColorActuator()
103 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
104 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_COLOR),
106 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("HSBColorActuator"),
107 hsbColorActuatorServiceStateCaptor.capture());
108 HSBColorActuatorServiceState state = hsbColorActuatorServiceStateCaptor.getValue();
109 assertEquals(-16776961, state.rgb);
113 void testUpdateChannelBinarySwitchState() {
114 JsonElement jsonObject = JsonParser.parseString("{\"@type\":\"binarySwitchState\",\"on\":true}");
115 getFixture().processUpdate("BinarySwitch", jsonObject);
116 verify(getCallback()).stateUpdated(
117 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.ON);
119 jsonObject = JsonParser.parseString("{\"@type\":\"binarySwitchState\",\"on\":false}");
120 getFixture().processUpdate("BinarySwitch", jsonObject);
121 verify(getCallback()).stateUpdated(
122 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.OFF);
126 void testUpdateChannelMultiLevelSwitchState() {
127 JsonElement jsonObject = JsonParser.parseString("{\"@type\":\"multiLevelSwitchState\",\"level\":16}");
128 getFixture().processUpdate("MultiLevelSwitch", jsonObject);
129 verify(getCallback()).stateUpdated(
130 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_BRIGHTNESS), new PercentType(16));
134 void testUpdateChannelHSBColorActuatorState() {
135 JsonElement jsonObject = JsonParser.parseString("""
136 {"colorTemperatureRange": {
140 "@type": "colorState",
141 "gamut": "LEDVANCE_GAMUT_A",
144 getFixture().processUpdate("HSBColorActuator", jsonObject);
145 verify(getCallback()).stateUpdated(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_COLOR),
146 HSBType.fromRGB(255, 207, 117));