]> git.basschouten.com Git - openhab-addons.git/blob
2f273661d3be1f8f1012179fd50c6d4a021203df
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.*;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.*;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.concurrent.CompletableFuture;
27 import java.util.concurrent.CountDownLatch;
28 import java.util.concurrent.ScheduledExecutorService;
29 import java.util.concurrent.ScheduledThreadPoolExecutor;
30 import java.util.concurrent.TimeUnit;
31
32 import org.eclipse.jdt.annotation.NonNullByDefault;
33 import org.eclipse.jdt.annotation.Nullable;
34 import org.junit.jupiter.api.AfterEach;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.mockito.Mock;
39 import org.mockito.junit.jupiter.MockitoExtension;
40 import org.mockito.junit.jupiter.MockitoSettings;
41 import org.mockito.quality.Strictness;
42 import org.openhab.binding.mqtt.generic.AvailabilityTracker;
43 import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
44 import org.openhab.binding.mqtt.generic.MqttChannelTypeProvider;
45 import org.openhab.binding.mqtt.homeassistant.internal.DiscoverComponents;
46 import org.openhab.binding.mqtt.homeassistant.internal.DiscoverComponents.ComponentDiscovered;
47 import org.openhab.binding.mqtt.homeassistant.internal.HaID;
48 import org.openhab.binding.mqtt.homeassistant.internal.component.AbstractComponent;
49 import org.openhab.binding.mqtt.homeassistant.internal.component.Switch;
50 import org.openhab.binding.mqtt.homeassistant.internal.config.ChannelConfigurationTypeAdapterFactory;
51 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
52 import org.openhab.core.io.transport.mqtt.MqttConnectionObserver;
53 import org.openhab.core.io.transport.mqtt.MqttConnectionState;
54 import org.openhab.core.library.types.OnOffType;
55 import org.openhab.core.types.State;
56 import org.openhab.core.types.UnDefType;
57
58 import com.google.gson.Gson;
59 import com.google.gson.GsonBuilder;
60 import com.hubspot.jinjava.Jinjava;
61
62 /**
63  * A full implementation test, that starts the embedded MQTT broker and publishes a homeassistant MQTT discovery device
64  * tree.
65  *
66  * @author David Graeff - Initial contribution
67  */
68 @ExtendWith(MockitoExtension.class)
69 @MockitoSettings(strictness = Strictness.LENIENT)
70 @NonNullByDefault
71 public class HomeAssistantMQTTImplementationTest extends MqttOSGiTest {
72
73     private @NonNullByDefault({}) MqttBrokerConnection haConnection;
74     private int registeredTopics = 100;
75     private @Nullable Throwable failure;
76
77     private @Mock @NonNullByDefault({}) ChannelStateUpdateListener channelStateUpdateListener;
78     private @Mock @NonNullByDefault({}) AvailabilityTracker availabilityTracker;
79
80     /**
81      * Create an observer that fails the test as soon as the broker client connection changes its connection state
82      * to something else then CONNECTED.
83      */
84     private final MqttConnectionObserver failIfChange = (state, error) -> assertThat(state,
85             is(MqttConnectionState.CONNECTED));
86     private final String testObjectTopic = "homeassistant/switch/node/"
87             + ThingChannelConstants.TEST_HOME_ASSISTANT_THING.getId();
88
89     @Override
90     @BeforeEach
91     public void beforeEach() throws Exception {
92         super.beforeEach();
93
94         haConnection = createBrokerConnection("ha_mqtt");
95
96         // If the connection state changes in between -> fail
97         haConnection.addConnectionObserver(failIfChange);
98
99         // Create topic string and config for one example HA component (a Switch)
100         final String config = "{'name':'testname','state_topic':'" + testObjectTopic + "/state','command_topic':'"
101                 + testObjectTopic + "/set'}";
102
103         // Publish component configurations and component states to MQTT
104         List<CompletableFuture<Boolean>> futures = new ArrayList<>();
105         futures.add(publish(testObjectTopic + "/config", config));
106         futures.add(publish(testObjectTopic + "/state", "ON"));
107
108         registeredTopics = futures.size();
109         CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(2, TimeUnit.SECONDS);
110
111         failure = null;
112     }
113
114     @Override
115     @AfterEach
116     public void afterEach() throws Exception {
117         if (haConnection != null) {
118             haConnection.removeConnectionObserver(failIfChange);
119             haConnection.stop().get(5, TimeUnit.SECONDS);
120         }
121
122         super.afterEach();
123     }
124
125     @Test
126     public void reconnectTest() throws Exception {
127         haConnection.removeConnectionObserver(failIfChange);
128         haConnection.stop().get(5, TimeUnit.SECONDS);
129         haConnection = createBrokerConnection("ha_mqtt");
130     }
131
132     @Test
133     public void retrieveAllTopics() throws Exception {
134         CountDownLatch c = new CountDownLatch(registeredTopics);
135         haConnection.subscribe("homeassistant/+/+/" + ThingChannelConstants.TEST_HOME_ASSISTANT_THING.getId() + "/#",
136                 (topic, payload) -> c.countDown()).get(5, TimeUnit.SECONDS);
137         assertTrue(c.await(2, TimeUnit.SECONDS),
138                 "Connection " + haConnection.getClientId() + " not retrieving all topics");
139     }
140
141     @Test
142     public void parseHATree() throws Exception {
143         MqttChannelTypeProvider channelTypeProvider = mock(MqttChannelTypeProvider.class);
144
145         final Map<String, AbstractComponent<?>> haComponents = new HashMap<>();
146         Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory()).create();
147         Jinjava jinjava = new Jinjava();
148
149         ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(4);
150         DiscoverComponents discover = spy(new DiscoverComponents(ThingChannelConstants.TEST_HOME_ASSISTANT_THING,
151                 scheduler, channelStateUpdateListener, availabilityTracker, gson, jinjava, true));
152
153         // The DiscoverComponents object calls ComponentDiscovered callbacks.
154         // In the following implementation we add the found component to the `haComponents` map
155         // and add the types to the channelTypeProvider, like in the real Thing handler.
156         final CountDownLatch latch = new CountDownLatch(1);
157         ComponentDiscovered cd = (haID, c) -> {
158             haComponents.put(c.getGroupId(), c);
159             latch.countDown();
160         };
161
162         // Start the discovery for 2000ms. Forced timeout after 4000ms.
163         HaID haID = new HaID(testObjectTopic + "/config");
164         CompletableFuture<Void> future = discover.startDiscovery(haConnection, 2000, Set.of(haID), cd).thenRun(() -> {
165         }).exceptionally(e -> {
166             failure = e;
167             return null;
168         });
169
170         assertTrue(latch.await(4, TimeUnit.SECONDS));
171         future.get(5, TimeUnit.SECONDS);
172
173         // No failure expected and one discovered result
174         assertNull(failure);
175         assertThat(haComponents.size(), is(1));
176
177         String channelGroupId = "switch_" + ThingChannelConstants.TEST_HOME_ASSISTANT_THING.getId();
178         String channelId = Switch.SWITCH_CHANNEL_ID;
179
180         State value = haComponents.get(channelGroupId).getChannel(channelGroupId).getState().getCache()
181                 .getChannelState();
182         assertThat(value, is(UnDefType.UNDEF));
183
184         haComponents.values().stream().map(e -> e.start(haConnection, scheduler, 100))
185                 .reduce(CompletableFuture.completedFuture(null), (a, v) -> a.thenCompose(b -> v)).exceptionally(e -> {
186                     failure = e;
187                     return null;
188                 }).get();
189
190         // We should have received the retained value, while subscribing to the channels MQTT state topic.
191         verify(channelStateUpdateListener, timeout(4000).times(1)).updateChannelState(any(), any());
192
193         // Value should be ON now.
194         value = haComponents.get(channelGroupId).getChannel(channelGroupId).getState().getCache().getChannelState();
195         assertThat(value, is(OnOffType.ON));
196     }
197 }