]> git.basschouten.com Git - openhab-addons.git/blob
d8614eaa3520969b7e8a8e125c6e2c486c41742d
[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.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.concurrent.CopyOnWriteArrayList;
22 import java.util.concurrent.CountDownLatch;
23 import java.util.concurrent.TimeUnit;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.mockito.junit.jupiter.MockitoExtension;
31 import org.openhab.binding.mqtt.generic.MqttChannelTypeProvider;
32 import org.openhab.binding.mqtt.homeassistant.internal.AbstractHomeAssistantTests;
33 import org.openhab.binding.mqtt.homeassistant.internal.HandlerConfiguration;
34 import org.openhab.core.config.discovery.DiscoveryListener;
35 import org.openhab.core.config.discovery.DiscoveryResult;
36 import org.openhab.core.config.discovery.DiscoveryService;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingTypeUID;
39 import org.openhab.core.thing.ThingUID;
40
41 /**
42  * Tests for {@link HomeAssistantDiscovery}
43  *
44  * @author Anton Kharuzhy - Initial contribution
45  */
46 @SuppressWarnings({ "ConstantConditions", "unchecked" })
47 @ExtendWith(MockitoExtension.class)
48 @NonNullByDefault
49 public class HomeAssistantDiscoveryTests extends AbstractHomeAssistantTests {
50     private @NonNullByDefault({}) 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     private static class LatchDiscoveryListener implements DiscoveryListener {
93         private final CopyOnWriteArrayList<DiscoveryResult> discoveryResults = new CopyOnWriteArrayList<>();
94         private @Nullable CountDownLatch latch;
95
96         @Override
97         public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
98             discoveryResults.add(result);
99             if (latch != null) {
100                 latch.countDown();
101             }
102         }
103
104         @Override
105         public void thingRemoved(DiscoveryService source, ThingUID thingUID) {
106         }
107
108         @Override
109         public @Nullable Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp,
110                 @Nullable Collection<ThingTypeUID> thingTypeUIDs, @Nullable ThingUID bridgeUID) {
111             return Collections.emptyList();
112         }
113
114         public CopyOnWriteArrayList<DiscoveryResult> getDiscoveryResults() {
115             return discoveryResults;
116         }
117
118         public CountDownLatch createWaitForThingsDiscoveredLatch(int count) {
119             final var newLatch = new CountDownLatch(count);
120             latch = newLatch;
121             return newLatch;
122         }
123     }
124 }