2 * Copyright (c) 2010-2021 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.mqtt.homeassistant.internal.discovery;
15 import static org.hamcrest.CoreMatchers.hasItems;
16 import static org.hamcrest.CoreMatchers.is;
17 import static org.hamcrest.MatcherAssert.assertThat;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.concurrent.CopyOnWriteArrayList;
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.TimeUnit;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.extension.ExtendWith;
31 import org.mockito.junit.jupiter.MockitoExtension;
32 import org.openhab.binding.mqtt.generic.MqttChannelTypeProvider;
33 import org.openhab.binding.mqtt.homeassistant.internal.AbstractHomeAssistantTests;
34 import org.openhab.binding.mqtt.homeassistant.internal.HandlerConfiguration;
35 import org.openhab.core.config.discovery.DiscoveryListener;
36 import org.openhab.core.config.discovery.DiscoveryResult;
37 import org.openhab.core.config.discovery.DiscoveryService;
38 import org.openhab.core.thing.Thing;
39 import org.openhab.core.thing.ThingTypeUID;
40 import org.openhab.core.thing.ThingUID;
43 * Tests for {@link HomeAssistantDiscovery}
45 * @author Anton Kharuzhy - Initial contribution
47 @SuppressWarnings({ "ConstantConditions", "unchecked" })
48 @ExtendWith(MockitoExtension.class)
49 public class HomeAssistantDiscoveryTests extends AbstractHomeAssistantTests {
50 private HomeAssistantDiscovery discovery;
53 public void beforeEach() {
54 discovery = new TestHomeAssistantDiscovery(channelTypeProvider);
58 public void testOneThingDiscovery() throws Exception {
59 var discoveryListener = new LatchDiscoveryListener();
60 var latch = discoveryListener.createWaitForThingsDiscoveredLatch(1);
62 // When discover one thing with two channels
63 discovery.addDiscoveryListener(discoveryListener);
64 discovery.receivedMessage(HA_UID, bridgeConnection,
65 "homeassistant/climate/0x847127fffe11dd6a_climate_zigbee2mqtt/config",
66 getResourceAsByteArray("component/configTS0601ClimateThermostat.json"));
67 discovery.receivedMessage(HA_UID, bridgeConnection,
68 "homeassistant/switch/0x847127fffe11dd6a_auto_lock_zigbee2mqtt/config",
69 getResourceAsByteArray("component/configTS0601AutoLock.json"));
71 // Then one thing found
72 assert latch.await(3, TimeUnit.SECONDS);
73 var discoveryResults = discoveryListener.getDiscoveryResults();
74 assertThat(discoveryResults.size(), is(1));
75 var result = discoveryResults.get(0);
76 assertThat(result.getBridgeUID(), is(HA_UID));
77 assertThat(result.getProperties().get(Thing.PROPERTY_MODEL_ID),
78 is("Radiator valve with thermostat (TS0601_thermostat)"));
79 assertThat(result.getProperties().get(Thing.PROPERTY_VENDOR), is("TuYa"));
80 assertThat(result.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is("Zigbee2MQTT 1.18.2"));
81 assertThat(result.getProperties().get(HandlerConfiguration.PROPERTY_BASETOPIC), is("homeassistant"));
82 assertThat((List<String>) result.getProperties().get(HandlerConfiguration.PROPERTY_TOPICS), hasItems(
83 "climate/0x847127fffe11dd6a_climate_zigbee2mqtt", "switch/0x847127fffe11dd6a_auto_lock_zigbee2mqtt"));
86 private static class TestHomeAssistantDiscovery extends HomeAssistantDiscovery {
87 public TestHomeAssistantDiscovery(MqttChannelTypeProvider typeProvider) {
88 this.typeProvider = typeProvider;
93 private static class LatchDiscoveryListener implements DiscoveryListener {
94 private final CopyOnWriteArrayList<DiscoveryResult> discoveryResults = new CopyOnWriteArrayList<>();
95 private @Nullable CountDownLatch latch;
97 public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
98 discoveryResults.add(result);
104 public void thingRemoved(DiscoveryService source, ThingUID thingUID) {
107 public @Nullable Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp,
108 @Nullable Collection<ThingTypeUID> thingTypeUIDs, @Nullable ThingUID bridgeUID) {
109 return Collections.emptyList();
112 public CopyOnWriteArrayList<DiscoveryResult> getDiscoveryResults() {
113 return discoveryResults;
116 public CountDownLatch createWaitForThingsDiscoveredLatch(int count) {
117 final var newLatch = new CountDownLatch(count);