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.mielecloud.internal.handler;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
18 import static org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants.Channels.*;
19 import static org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants.COFFEE_SYSTEM_THING_UID;
21 import java.util.Optional;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants;
26 import org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants;
27 import org.openhab.binding.mielecloud.internal.webservice.api.ActionsState;
28 import org.openhab.binding.mielecloud.internal.webservice.api.DeviceState;
29 import org.openhab.binding.mielecloud.internal.webservice.api.PowerStatus;
30 import org.openhab.binding.mielecloud.internal.webservice.api.json.StateType;
31 import org.openhab.core.library.types.DecimalType;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.library.types.StringType;
36 * @author Björn Lange - Initial contribution
37 * @author Benjamin Bolte - Add info state channel and map signal flags from API tests
38 * @author Björn Lange - Add elapsed time channel
41 public class CoffeeDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
43 protected AbstractMieleThingHandler setUpThingHandler() {
44 return createThingHandler(MieleCloudBindingConstants.THING_TYPE_COFFEE_SYSTEM, COFFEE_SYSTEM_THING_UID,
45 CoffeeSystemThingHandler.class, MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER, "0");
49 public void testChannelUpdatesForNullValues() throws Exception {
51 setUpBridgeAndThing();
53 DeviceState deviceState = mock(DeviceState.class);
54 when(deviceState.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
55 when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.empty());
56 when(deviceState.getSelectedProgram()).thenReturn(Optional.empty());
57 when(deviceState.getSelectedProgramId()).thenReturn(Optional.empty());
58 when(deviceState.getProgramPhase()).thenReturn(Optional.empty());
59 when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.empty());
60 when(deviceState.getStateType()).thenReturn(Optional.empty());
61 when(deviceState.getStatus()).thenReturn(Optional.empty());
62 when(deviceState.getStatusRaw()).thenReturn(Optional.empty());
63 when(deviceState.getElapsedTime()).thenReturn(Optional.empty());
64 when(deviceState.getLightState()).thenReturn(Optional.empty());
67 getBridgeHandler().onDeviceStateUpdated(deviceState);
71 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ACTIVE));
72 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ACTIVE_RAW));
73 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE));
74 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE_RAW));
75 assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
76 assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
77 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ELAPSED_TIME));
78 assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
79 assertEquals(NULL_VALUE_STATE, getChannelState(LIGHT_SWITCH));
84 public void testChannelUpdatesForValidValues() throws Exception {
86 setUpBridgeAndThing();
88 DeviceState deviceState = mock(DeviceState.class);
89 when(deviceState.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
90 when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.of(true));
91 when(deviceState.getSelectedProgram()).thenReturn(Optional.of("Latte Macchiato"));
92 when(deviceState.getSelectedProgramId()).thenReturn(Optional.of(5L));
93 when(deviceState.getProgramPhase()).thenReturn(Optional.of("Spühlen"));
94 when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.of(1));
95 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
96 when(deviceState.getStatus()).thenReturn(Optional.of("Running"));
97 when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
98 when(deviceState.hasError()).thenReturn(false);
99 when(deviceState.hasInfo()).thenReturn(true);
100 when(deviceState.getLightState()).thenReturn(Optional.of(false));
101 when(deviceState.getElapsedTime()).thenReturn(Optional.of(3));
104 getBridgeHandler().onDeviceStateUpdated(deviceState);
107 waitForAssert(() -> {
108 assertEquals(new StringType("Latte Macchiato"), getChannelState(PROGRAM_ACTIVE));
109 assertEquals(new DecimalType(5), getChannelState(PROGRAM_ACTIVE_RAW));
110 assertEquals(new StringType("Spühlen"), getChannelState(PROGRAM_PHASE));
111 assertEquals(new DecimalType(1), getChannelState(PROGRAM_PHASE_RAW));
112 assertEquals(new StringType("Running"), getChannelState(OPERATION_STATE));
113 assertEquals(new DecimalType(StateType.RUNNING.getCode()), getChannelState(OPERATION_STATE_RAW));
114 assertEquals(new DecimalType(3), getChannelState(PROGRAM_ELAPSED_TIME));
115 assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
116 assertEquals(OnOffType.OFF, getChannelState(ERROR_STATE));
117 assertEquals(OnOffType.ON, getChannelState(INFO_STATE));
118 assertEquals(OnOffType.OFF, getChannelState(LIGHT_SWITCH));
123 public void testFinishStateChannelIsSetToOnWhenProgramHasFinished() throws Exception {
125 setUpBridgeAndThing();
127 DeviceState deviceStateBefore = mock(DeviceState.class);
128 when(deviceStateBefore.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
129 when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
130 when(deviceStateBefore.isInState(any())).thenCallRealMethod();
132 getBridgeHandler().onDeviceStateUpdated(deviceStateBefore);
134 DeviceState deviceStateAfter = mock(DeviceState.class);
135 when(deviceStateAfter.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
136 when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
137 when(deviceStateAfter.isInState(any())).thenCallRealMethod();
140 getBridgeHandler().onDeviceStateUpdated(deviceStateAfter);
143 waitForAssert(() -> {
144 assertEquals(OnOffType.ON, getChannelState(FINISH_STATE));
149 public void testTransitionChannelUpdatesForNullValues() throws Exception {
151 setUpBridgeAndThing();
153 DeviceState deviceStateBefore = mock(DeviceState.class);
154 when(deviceStateBefore.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
155 when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
156 when(deviceStateBefore.isInState(any())).thenCallRealMethod();
157 when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.empty());
159 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
161 DeviceState deviceStateAfter = mock(DeviceState.class);
162 when(deviceStateAfter.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
163 when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
164 when(deviceStateAfter.isInState(any())).thenCallRealMethod();
165 when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.empty());
168 getThingHandler().onDeviceStateUpdated(deviceStateAfter);
170 waitForAssert(() -> {
171 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_REMAINING_TIME));
176 public void testTransitionChannelUpdatesForValidValues() throws Exception {
178 setUpBridgeAndThing();
180 DeviceState deviceStateBefore = mock(DeviceState.class);
181 when(deviceStateBefore.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
182 when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
183 when(deviceStateBefore.isInState(any())).thenCallRealMethod();
184 when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.of(10));
186 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
188 DeviceState deviceStateAfter = mock(DeviceState.class);
189 when(deviceStateAfter.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
190 when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
191 when(deviceStateAfter.isInState(any())).thenCallRealMethod();
192 when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.of(10));
195 getThingHandler().onDeviceStateUpdated(deviceStateAfter);
197 waitForAssert(() -> {
198 assertEquals(new DecimalType(10), getChannelState(PROGRAM_REMAINING_TIME));
203 public void testActionsChannelUpdatesForValidValues() throws Exception {
205 setUpBridgeAndThing();
207 ActionsState actionsState = mock(ActionsState.class);
208 when(actionsState.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
209 when(actionsState.canBeSwitchedOn()).thenReturn(true);
210 when(actionsState.canBeSwitchedOff()).thenReturn(false);
211 when(actionsState.canControlLight()).thenReturn(true);
214 getBridgeHandler().onProcessActionUpdated(actionsState);
217 waitForAssert(() -> {
218 assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_ON));
219 assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_OFF));
220 assertEquals(OnOffType.ON, getChannelState(LIGHT_CAN_BE_CONTROLLED));