]> git.basschouten.com Git - openhab-addons.git/blob
45dde9f0fe38cda9f044adcdbe394e14d79c4043
[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.boschshc.internal.devices.smartbulb;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.*;
18
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
21
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;
38
39 import com.google.gson.JsonElement;
40 import com.google.gson.JsonParser;
41
42 /**
43  * Unit tests for {@link SmartBulbHandler}.
44  *
45  * @author David Pace - Initial contribution
46  *
47  */
48 @NonNullByDefault
49 class SmartBulbHandlerTest extends AbstractBoschSHCDeviceHandlerTest<SmartBulbHandler> {
50
51     private @Captor @NonNullByDefault({}) ArgumentCaptor<BinarySwitchServiceState> binarySwitchServiceStateCaptor;
52
53     private @Captor @NonNullByDefault({}) ArgumentCaptor<MultiLevelSwitchServiceState> multiLevelSwitchServiceStateCaptor;
54
55     private @Captor @NonNullByDefault({}) ArgumentCaptor<HSBColorActuatorServiceState> hsbColorActuatorServiceStateCaptor;
56
57     @Override
58     protected SmartBulbHandler createFixture() {
59         return new SmartBulbHandler(getThing());
60     }
61
62     @Override
63     protected String getDeviceID() {
64         return "hdm:ZigBee:f0d1b80000f2a3e9";
65     }
66
67     @Override
68     protected ThingTypeUID getThingTypeUID() {
69         return BoschSHCBindingConstants.THING_TYPE_SMART_BULB;
70     }
71
72     @Test
73     void testHandleCommandBinarySwitch()
74             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
75         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_POWER_SWITCH),
76                 OnOffType.ON);
77         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("BinarySwitch"),
78                 binarySwitchServiceStateCaptor.capture());
79         BinarySwitchServiceState state = binarySwitchServiceStateCaptor.getValue();
80         assertTrue(state.on);
81
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);
88     }
89
90     @Test
91     void testHandleCommandMultiLevelSwitch()
92             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
93         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_BRIGHTNESS),
94                 new PercentType(42));
95         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("MultiLevelSwitch"),
96                 multiLevelSwitchServiceStateCaptor.capture());
97         MultiLevelSwitchServiceState state = multiLevelSwitchServiceStateCaptor.getValue();
98         assertEquals(42, state.level);
99     }
100
101     @Test
102     void testHandleCommandHSBColorActuator()
103             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
104         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_COLOR),
105                 HSBType.BLUE);
106         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("HSBColorActuator"),
107                 hsbColorActuatorServiceStateCaptor.capture());
108         HSBColorActuatorServiceState state = hsbColorActuatorServiceStateCaptor.getValue();
109         assertEquals(-16776961, state.rgb);
110     }
111
112     @Test
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);
118
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);
123     }
124
125     @Test
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));
131     }
132
133     @Test
134     void testUpdateChannelHSBColorActuatorState() {
135         JsonElement jsonObject = JsonParser.parseString("{\"colorTemperatureRange\": {\n" + "        \"minCt\": 153,\n"
136                 + "        \"maxCt\": 526\n" + "    },\n" + "    \"@type\": \"colorState\",\n"
137                 + "    \"gamut\": \"LEDVANCE_GAMUT_A\",\n" + "    \"rgb\": -12427}");
138         getFixture().processUpdate("HSBColorActuator", jsonObject);
139         verify(getCallback()).stateUpdated(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_COLOR),
140                 HSBType.fromRGB(255, 207, 117));
141     }
142 }