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.homematic.internal.discovery;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.Mockito.*;
19 import static org.openhab.binding.homematic.test.util.BridgeHelper.createHomematicBridge;
20 import static org.openhab.binding.homematic.test.util.DimmerHelper.createDimmerHmDevice;
22 import java.io.IOException;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.homematic.internal.communicator.HomematicGateway;
27 import org.openhab.binding.homematic.internal.handler.HomematicBridgeHandler;
28 import org.openhab.binding.homematic.internal.model.HmDevice;
29 import org.openhab.binding.homematic.internal.type.HomematicTypeGenerator;
30 import org.openhab.binding.homematic.test.util.SimpleDiscoveryListener;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.test.java.JavaTest;
33 import org.openhab.core.thing.Bridge;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.openhab.core.thing.ThingStatusInfo;
39 * Tests for {@link HomematicDeviceDiscoveryServiceTest}.
41 * @author Florian Stolte - Initial Contribution
44 public class HomematicDeviceDiscoveryServiceTest extends JavaTest {
46 private HomematicDeviceDiscoveryService homematicDeviceDiscoveryService;
47 private HomematicBridgeHandler homematicBridgeHandler;
50 public void setup() throws IOException {
51 this.homematicBridgeHandler = mockHomematicBridgeHandler();
52 this.homematicDeviceDiscoveryService = new HomematicDeviceDiscoveryService();
53 this.homematicDeviceDiscoveryService.setThingHandler(homematicBridgeHandler);
56 private HomematicBridgeHandler mockHomematicBridgeHandler() throws IOException {
57 HomematicBridgeHandler homematicBridgeHandler = mock(HomematicBridgeHandler.class);
58 Bridge bridge = createHomematicBridge();
59 HomematicGateway homematicGateway = mockHomematicGateway();
60 HomematicTypeGenerator homematicTypeGenerator = mockHomematicTypeGenerator();
62 when(homematicBridgeHandler.getThing()).thenReturn(bridge);
63 when(homematicBridgeHandler.getGateway()).thenReturn(homematicGateway);
64 when(homematicBridgeHandler.getTypeGenerator()).thenReturn(homematicTypeGenerator);
66 return homematicBridgeHandler;
69 private HomematicGateway mockHomematicGateway() throws IOException {
70 HomematicGateway homematicGateway = mock(HomematicGateway.class);
72 when(homematicGateway.getInstallMode()).thenReturn(60);
74 return homematicGateway;
77 private HomematicTypeGenerator mockHomematicTypeGenerator() {
78 return mock(HomematicTypeGenerator.class);
82 public void testDiscoveryResultIsReportedForNewDevice() {
83 SimpleDiscoveryListener discoveryListener = new SimpleDiscoveryListener();
84 homematicDeviceDiscoveryService.addDiscoveryListener(discoveryListener);
86 HmDevice hmDevice = createDimmerHmDevice();
87 homematicDeviceDiscoveryService.deviceDiscovered(hmDevice);
89 assertThat(discoveryListener.discoveredResults.size(), is(1));
90 discoveryResultMatchesHmDevice(discoveryListener.discoveredResults.element(), hmDevice);
94 public void testDevicesAreLoadedFromBridgeDuringDiscovery() throws IOException {
95 startScanAndWaitForLoadedDevices();
97 verify(homematicBridgeHandler.getGateway()).loadAllDeviceMetadata();
101 public void testInstallModeIsNotActiveDuringInitialDiscovery() throws IOException {
102 startScanAndWaitForLoadedDevices();
104 verify(homematicBridgeHandler.getGateway(), never()).setInstallMode(eq(true), anyInt());
108 public void testInstallModeIsActiveDuringSubsequentDiscovery() throws IOException {
109 homematicBridgeHandler.getThing()
110 .setStatusInfo(new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, ""));
112 startScanAndWaitForLoadedDevices();
114 verify(homematicBridgeHandler.getGateway()).setInstallMode(true, 60);
118 public void testStoppingDiscoveryDisablesInstallMode() throws IOException {
119 homematicBridgeHandler.getThing()
120 .setStatusInfo(new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, ""));
121 homematicDeviceDiscoveryService.startScan();
123 homematicDeviceDiscoveryService.stopScan();
125 verify(homematicBridgeHandler.getGateway()).setInstallMode(false, 0);
128 private void startScanAndWaitForLoadedDevices() {
129 homematicDeviceDiscoveryService.startScan();
130 waitForAssert(() -> verify(homematicBridgeHandler).setOfflineStatus(), 1000, 50);
133 private void discoveryResultMatchesHmDevice(DiscoveryResult result, HmDevice device) {
134 assertThat(result.getThingTypeUID().getId(), is(device.getType()));
135 assertThat(result.getThingUID().getId(), is(device.getAddress()));
136 assertThat(result.getLabel(), is(device.getName()));