]> git.basschouten.com Git - openhab-addons.git/blob
cfb2a005041663f2dd6869ac212434590392a330
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.homeassistant.internal.discovery;
14
15 import static org.hamcrest.CoreMatchers.hasItems;
16 import static org.hamcrest.CoreMatchers.is;
17 import static org.hamcrest.MatcherAssert.assertThat;
18
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;
25
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;
41
42 /**
43  * Tests for {@link HomeAssistantDiscovery}
44  *
45  * @author Anton Kharuzhy - Initial contribution
46  */
47 @SuppressWarnings({ "ConstantConditions", "unchecked" })
48 @ExtendWith(MockitoExtension.class)
49 public class HomeAssistantDiscoveryTests extends AbstractHomeAssistantTests {
50     private HomeAssistantDiscovery discovery;
51
52     @BeforeEach
53     public void beforeEach() {
54         discovery = new TestHomeAssistantDiscovery(channelTypeProvider);
55     }
56
57     @Test
58     public void testOneThingDiscovery() throws Exception {
59         var discoveryListener = new LatchDiscoveryListener();
60         var latch = discoveryListener.createWaitForThingsDiscoveredLatch(1);
61
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"));
70
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"));
84     }
85
86     private static class TestHomeAssistantDiscovery extends HomeAssistantDiscovery {
87         public TestHomeAssistantDiscovery(MqttChannelTypeProvider typeProvider) {
88             this.typeProvider = typeProvider;
89         }
90     }
91
92     @NonNullByDefault
93     private static class LatchDiscoveryListener implements DiscoveryListener {
94         private final CopyOnWriteArrayList<DiscoveryResult> discoveryResults = new CopyOnWriteArrayList<>();
95         private @Nullable CountDownLatch latch;
96
97         public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
98             discoveryResults.add(result);
99             if (latch != null) {
100                 latch.countDown();
101             }
102         }
103
104         public void thingRemoved(DiscoveryService source, ThingUID thingUID) {
105         }
106
107         public @Nullable Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp,
108                 @Nullable Collection<ThingTypeUID> thingTypeUIDs, @Nullable ThingUID bridgeUID) {
109             return Collections.emptyList();
110         }
111
112         public CopyOnWriteArrayList<DiscoveryResult> getDiscoveryResults() {
113             return discoveryResults;
114         }
115
116         public CountDownLatch createWaitForThingsDiscoveredLatch(int count) {
117             final var newLatch = new CountDownLatch(count);
118             latch = newLatch;
119             return newLatch;
120         }
121     }
122 }