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.onewire.device;
15 import static org.mockito.ArgumentMatchers.*;
16 import static org.mockito.Mockito.times;
17 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22 import org.mockito.InOrder;
23 import org.mockito.Mockito;
24 import org.openhab.binding.onewire.internal.OwException;
25 import org.openhab.binding.onewire.internal.device.DS2438;
26 import org.openhab.binding.onewire.internal.device.DS2438.LightSensorType;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.QuantityType;
32 * Tests cases for {@link DS2438}.
34 * @author Jan N. Klug - Initial contribution
37 public class DS2438Test extends DeviceTestParent<DS2438> {
40 public void setupMocks() {
41 setupMocks(THING_TYPE_MS_TX, DS2438.class);
43 addChannel(CHANNEL_TEMPERATURE, "Number:Temperature");
44 addChannel(CHANNEL_HUMIDITY, "Number:Dimensionless");
45 addChannel(CHANNEL_ABSOLUTE_HUMIDITY, "Number:Density");
46 addChannel(CHANNEL_DEWPOINT, "Number:Temperature");
47 addChannel(CHANNEL_VOLTAGE, "Number:Voltage");
48 addChannel(CHANNEL_CURRENT, "Number:Current");
49 addChannel(CHANNEL_LIGHT, "Number:Illuminance");
50 addChannel(CHANNEL_SUPPLYVOLTAGE, "Number:Voltage");
54 public void temperatureChannel() throws OwException {
55 final DS2438 testDevice = instantiateDevice();
56 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
58 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
59 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(10.0));
61 testDevice.enableChannel(CHANNEL_TEMPERATURE);
62 testDevice.configureChannels();
63 inOrder.verify(mockThingHandler).getThing();
64 testDevice.refresh(mockBridgeHandler, true);
66 inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
67 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_TEMPERATURE), eq(new QuantityType<>("10.0 °C")));
69 inOrder.verifyNoMoreInteractions();
73 public void humidityChannel() throws OwException {
74 final DS2438 testDevice = instantiateDevice();
75 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
77 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
78 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(10.0));
80 testDevice.enableChannel(CHANNEL_HUMIDITY);
81 testDevice.enableChannel(CHANNEL_ABSOLUTE_HUMIDITY);
82 testDevice.enableChannel(CHANNEL_DEWPOINT);
83 testDevice.configureChannels();
84 inOrder.verify(mockThingHandler).getThing();
85 testDevice.refresh(mockBridgeHandler, true);
87 inOrder.verify(mockBridgeHandler, times(2)).readDecimalType(eq(testSensorId), any());
88 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_HUMIDITY), eq(new QuantityType<>("10.0 %")));
89 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_ABSOLUTE_HUMIDITY),
90 eq(new QuantityType<>("0.9381970824113001000 g/m³")));
91 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_DEWPOINT),
92 eq(new QuantityType<>("-20.31395053870025 °C")));
94 inOrder.verifyNoMoreInteractions();
98 public void voltageChannel() throws OwException {
99 final DS2438 testDevice = instantiateDevice();
100 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
102 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
103 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(2.0));
105 testDevice.enableChannel(CHANNEL_VOLTAGE);
106 testDevice.configureChannels();
107 inOrder.verify(mockThingHandler).getThing();
108 testDevice.refresh(mockBridgeHandler, true);
110 inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
111 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_VOLTAGE), eq(new QuantityType<>("2.0 V")));
113 inOrder.verifyNoMoreInteractions();
117 public void currentChannel() throws OwException {
118 final DS2438 testDevice = instantiateDevice();
119 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
121 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
123 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(2.0));
125 testDevice.enableChannel(CHANNEL_CURRENT);
126 testDevice.configureChannels();
127 inOrder.verify(mockThingHandler).getThing();
128 testDevice.refresh(mockBridgeHandler, true);
130 inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
131 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_CURRENT), eq(new QuantityType<>("2.0 mA")));
133 inOrder.verifyNoMoreInteractions();
137 public void lightChannel() throws OwException {
138 final DS2438 testDevice = instantiateDevice();
139 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
141 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
142 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(0.1));
144 testDevice.enableChannel(CHANNEL_LIGHT);
145 testDevice.configureChannels();
146 inOrder.verify(mockThingHandler).getThing();
147 testDevice.setLightSensorType(LightSensorType.ELABNET_V1);
148 testDevice.refresh(mockBridgeHandler, true);
150 inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
151 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_LIGHT), eq(new QuantityType<>("97442 lx")));
153 testDevice.setLightSensorType(LightSensorType.ELABNET_V2);
154 testDevice.refresh(mockBridgeHandler, true);
156 inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
157 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_LIGHT), eq(new QuantityType<>("134 lx")));
159 inOrder.verifyNoMoreInteractions();
163 public void supplyVoltageChannel() throws OwException {
164 final DS2438 testDevice = instantiateDevice();
165 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
167 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
168 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(2.0));
170 testDevice.enableChannel(CHANNEL_SUPPLYVOLTAGE);
171 testDevice.configureChannels();
172 inOrder.verify(mockThingHandler).getThing();
173 testDevice.refresh(mockBridgeHandler, true);
175 inOrder.verify(mockBridgeHandler).readDecimalType(eq(testSensorId), any());
176 inOrder.verify(mockThingHandler).postUpdate(eq(CHANNEL_SUPPLYVOLTAGE), eq(new QuantityType<>("2.0 V")));
178 inOrder.verifyNoMoreInteractions();
182 public void noChannel() throws OwException {
183 final DS2438 testDevice = instantiateDevice();
184 final InOrder inOrder = Mockito.inOrder(mockThingHandler, mockBridgeHandler);
186 Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
187 Mockito.when(mockBridgeHandler.readDecimalType(eq(testSensorId), any())).thenReturn(new DecimalType(2.0));
189 testDevice.configureChannels();
190 inOrder.verify(mockThingHandler).getThing();
191 testDevice.refresh(mockBridgeHandler, true);
193 inOrder.verifyNoMoreInteractions();