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.thermostat;
15 import static org.junit.jupiter.api.Assertions.assertSame;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.verify;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
22 import javax.measure.quantity.Temperature;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Test;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Captor;
28 import org.openhab.binding.boschshc.internal.devices.AbstractBatteryPoweredDeviceHandlerTest;
29 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
30 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
31 import org.openhab.binding.boschshc.internal.services.childlock.dto.ChildLockServiceState;
32 import org.openhab.binding.boschshc.internal.services.childlock.dto.ChildLockState;
33 import org.openhab.binding.boschshc.internal.services.silentmode.SilentModeState;
34 import org.openhab.binding.boschshc.internal.services.silentmode.dto.SilentModeServiceState;
35 import org.openhab.core.library.types.DecimalType;
36 import org.openhab.core.library.types.OnOffType;
37 import org.openhab.core.library.types.QuantityType;
38 import org.openhab.core.library.unit.SIUnits;
39 import org.openhab.core.thing.ChannelUID;
40 import org.openhab.core.thing.ThingStatus;
41 import org.openhab.core.thing.ThingStatusDetail;
42 import org.openhab.core.thing.ThingStatusInfo;
43 import org.openhab.core.thing.ThingTypeUID;
44 import org.openhab.core.thing.binding.builder.ThingStatusInfoBuilder;
46 import com.google.gson.JsonElement;
47 import com.google.gson.JsonParser;
50 * Unit Tests for {@link ThermostatHandler}.
52 * @author David Pace - Initial contribution
56 class ThermostatHandlerTest extends AbstractBatteryPoweredDeviceHandlerTest<ThermostatHandler> {
58 private @Captor @NonNullByDefault({}) ArgumentCaptor<ChildLockServiceState> childLockServiceStateCaptor;
60 private @Captor @NonNullByDefault({}) ArgumentCaptor<SilentModeServiceState> silentModeServiceStateCaptor;
63 protected ThermostatHandler createFixture() {
64 return new ThermostatHandler(getThing());
68 protected String getDeviceID() {
69 return "hdm:ZigBee:000d6f0017f1ace2";
73 protected ThingTypeUID getThingTypeUID() {
74 return BoschSHCBindingConstants.THING_TYPE_THERMOSTAT;
78 void testHandleCommandChildLockService()
79 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
80 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_LOCK),
82 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("Thermostat"), childLockServiceStateCaptor.capture());
83 ChildLockServiceState state = childLockServiceStateCaptor.getValue();
84 assertSame(ChildLockState.ON, state.childLock);
88 void testHandleCommandSilentModeService()
89 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
90 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SILENT_MODE),
92 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("SilentMode"),
93 silentModeServiceStateCaptor.capture());
94 SilentModeServiceState state = silentModeServiceStateCaptor.getValue();
95 assertSame(SilentModeState.MODE_SILENT, state.mode);
99 void testHandleCommandUnknownCommandChildLockService() {
100 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_LOCK),
101 new DecimalType(42));
102 ThingStatusInfo expectedThingStatusInfo = ThingStatusInfoBuilder
103 .create(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR)
105 "Error when service Thermostat should handle command org.openhab.core.library.types.DecimalType: Thermostat: Can not handle command org.openhab.core.library.types.DecimalType")
107 verify(getCallback()).statusUpdated(getThing(), expectedThingStatusInfo);
111 void testHandleCommandUnknownCommandSilentModeService() {
112 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SILENT_MODE),
113 new DecimalType(42));
114 ThingStatusInfo expectedThingStatusInfo = ThingStatusInfoBuilder
115 .create(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR)
117 "Error when service SilentMode should handle command org.openhab.core.library.types.DecimalType: SilentMode: Can not handle command org.openhab.core.library.types.DecimalType")
119 verify(getCallback()).statusUpdated(getThing(), expectedThingStatusInfo);
123 void testUpdateChannelsTemperatureLevelService() {
124 JsonElement jsonObject = JsonParser.parseString(
125 "{\n" + " \"@type\": \"temperatureLevelState\",\n" + " \"temperature\": 21.5\n" + " }");
126 getFixture().processUpdate("TemperatureLevel", jsonObject);
127 verify(getCallback()).stateUpdated(
128 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_TEMPERATURE),
129 new QuantityType<Temperature>(21.5, SIUnits.CELSIUS));
133 void testUpdateChannelsValveTappetService() {
134 JsonElement jsonObject = JsonParser
135 .parseString("{\n" + " \"@type\": \"valveTappetState\",\n" + " \"position\": 42\n" + " }");
136 getFixture().processUpdate("ValveTappet", jsonObject);
137 verify(getCallback()).stateUpdated(
138 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_VALVE_TAPPET_POSITION),
139 new DecimalType(42));
143 void testUpdateChannelsChildLockService() {
144 JsonElement jsonObject = JsonParser
145 .parseString("{\n" + " \"@type\": \"childLockState\",\n" + " \"childLock\": \"ON\"\n" + " }");
146 getFixture().processUpdate("Thermostat", jsonObject);
147 verify(getCallback()).stateUpdated(
148 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_LOCK), OnOffType.ON);
152 void testUpdateChannelsSilentModeService() {
153 JsonElement jsonObject = JsonParser.parseString("{\"@type\": \"silentModeState\", \"mode\": \"MODE_SILENT\"}");
154 getFixture().processUpdate("SilentMode", jsonObject);
155 verify(getCallback()).stateUpdated(
156 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SILENT_MODE), OnOffType.ON);
160 void testUpdateChannelsSilentModeServiceNormal() {
161 JsonElement jsonObject = JsonParser.parseString("{\"@type\": \"silentModeState\", \"mode\": \"MODE_NORMAL\"}");
162 getFixture().processUpdate("SilentMode", jsonObject);
163 verify(getCallback()).stateUpdated(
164 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SILENT_MODE), OnOffType.OFF);