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.Mockito.*;
17 import static org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants.Channels.*;
18 import static org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants.HOOD_DEVICE_THING_UID;
20 import java.util.Optional;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants;
25 import org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants;
26 import org.openhab.binding.mielecloud.internal.webservice.api.ActionsState;
27 import org.openhab.binding.mielecloud.internal.webservice.api.DeviceState;
28 import org.openhab.binding.mielecloud.internal.webservice.api.PowerStatus;
29 import org.openhab.binding.mielecloud.internal.webservice.api.json.StateType;
30 import org.openhab.core.library.types.DecimalType;
31 import org.openhab.core.library.types.OnOffType;
32 import org.openhab.core.library.types.StringType;
35 * @author Björn Lange - Initial contribution
36 * @author Benjamin Bolte - Add info state channel and map signal flags from API tests
39 public class HoodDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
41 protected AbstractMieleThingHandler setUpThingHandler() {
42 return createThingHandler(MieleCloudBindingConstants.THING_TYPE_HOOD, HOOD_DEVICE_THING_UID,
43 HoodDeviceThingHandler.class, MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER, "0");
47 public void testChannelUpdatesForNullValues() throws Exception {
49 setUpBridgeAndThing();
51 DeviceState deviceState = mock(DeviceState.class);
52 when(deviceState.getDeviceIdentifier()).thenReturn(HOOD_DEVICE_THING_UID.getId());
53 when(deviceState.getStateType()).thenReturn(Optional.empty());
54 when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.empty());
55 when(deviceState.getProgramPhase()).thenReturn(Optional.empty());
56 when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.empty());
57 when(deviceState.getStatus()).thenReturn(Optional.empty());
58 when(deviceState.getStatusRaw()).thenReturn(Optional.empty());
59 when(deviceState.getVentilationStep()).thenReturn(Optional.empty());
60 when(deviceState.getVentilationStepRaw()).thenReturn(Optional.empty());
61 when(deviceState.getLightState()).thenReturn(Optional.empty());
64 getBridgeHandler().onDeviceStateUpdated(deviceState);
68 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE));
69 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE_RAW));
70 assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
71 assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
72 assertEquals(NULL_VALUE_STATE, getChannelState(VENTILATION_POWER));
73 assertEquals(NULL_VALUE_STATE, getChannelState(VENTILATION_POWER_RAW));
74 assertEquals(NULL_VALUE_STATE, getChannelState(LIGHT_SWITCH));
75 assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
80 public void testChannelUpdatesForValidValues() throws Exception {
82 setUpBridgeAndThing();
84 DeviceState deviceState = mock(DeviceState.class);
85 when(deviceState.getDeviceIdentifier()).thenReturn(HOOD_DEVICE_THING_UID.getId());
86 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
87 when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.of(false));
88 when(deviceState.getProgramPhase()).thenReturn(Optional.of("Kochen"));
89 when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.of(5));
90 when(deviceState.getStatus()).thenReturn(Optional.of("Running"));
91 when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
92 when(deviceState.getVentilationStep()).thenReturn(Optional.of("2"));
93 when(deviceState.getVentilationStepRaw()).thenReturn(Optional.of(2));
94 when(deviceState.hasError()).thenReturn(false);
95 when(deviceState.hasInfo()).thenReturn(true);
96 when(deviceState.getLightState()).thenReturn(Optional.of(false));
99 getBridgeHandler().onDeviceStateUpdated(deviceState);
102 waitForAssert(() -> {
103 assertEquals(new StringType("Kochen"), getChannelState(PROGRAM_PHASE));
104 assertEquals(new DecimalType(5), getChannelState(PROGRAM_PHASE_RAW));
105 assertEquals(new StringType("Running"), getChannelState(OPERATION_STATE));
106 assertEquals(new DecimalType(StateType.RUNNING.getCode()), getChannelState(OPERATION_STATE_RAW));
107 assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
108 assertEquals(new StringType("2"), getChannelState(VENTILATION_POWER));
109 assertEquals(new DecimalType(2), getChannelState(VENTILATION_POWER_RAW));
110 assertEquals(OnOffType.OFF, getChannelState(ERROR_STATE));
111 assertEquals(OnOffType.ON, getChannelState(INFO_STATE));
112 assertEquals(OnOffType.OFF, getChannelState(LIGHT_SWITCH));
117 public void testActionsChannelUpdatesForValidValues() throws Exception {
119 setUpBridgeAndThing();
121 ActionsState actionsState = mock(ActionsState.class);
122 when(actionsState.getDeviceIdentifier()).thenReturn(HOOD_DEVICE_THING_UID.getId());
123 when(actionsState.canBeSwitchedOn()).thenReturn(true);
124 when(actionsState.canBeSwitchedOff()).thenReturn(false);
125 when(actionsState.canControlLight()).thenReturn(true);
128 getBridgeHandler().onProcessActionUpdated(actionsState);
131 waitForAssert(() -> {
132 assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_ON));
133 assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_OFF));
134 assertEquals(OnOffType.ON, getChannelState(LIGHT_CAN_BE_CONTROLLED));