]> git.basschouten.com Git - openhab-addons.git/blob
98a209516ce33c84f2673f955ee169bbf66b16c0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.tradfri.internal.discovery;
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.Mockito.when;
19 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
20 import static org.openhab.binding.tradfri.internal.config.TradfriDeviceConfig.CONFIG_ID;
21
22 import java.util.Collection;
23
24 import org.junit.jupiter.api.AfterEach;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.mockito.Mock;
29 import org.mockito.junit.jupiter.MockitoExtension;
30 import org.mockito.junit.jupiter.MockitoSettings;
31 import org.mockito.quality.Strictness;
32 import org.openhab.binding.tradfri.internal.handler.TradfriGatewayHandler;
33 import org.openhab.core.config.discovery.DiscoveryListener;
34 import org.openhab.core.config.discovery.DiscoveryResult;
35 import org.openhab.core.config.discovery.DiscoveryResultFlag;
36 import org.openhab.core.config.discovery.DiscoveryService;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.ThingUID;
39 import org.openhab.core.thing.binding.builder.BridgeBuilder;
40
41 import com.google.gson.JsonObject;
42 import com.google.gson.JsonParser;
43
44 /**
45  * Tests for {@link TradfriDiscoveryService}.
46  *
47  * @author Kai Kreuzer - Initial contribution
48  * @author Christoph Weitkamp - Added support for remote controller and motion sensor devices (read-only battery level)
49  */
50 @ExtendWith(MockitoExtension.class)
51 @MockitoSettings(strictness = Strictness.LENIENT)
52 public class TradfriDiscoveryServiceTest {
53
54     private static final ThingUID GATEWAY_THING_UID = new ThingUID("tradfri:gateway:1");
55
56     private @Mock TradfriGatewayHandler handler;
57
58     private final DiscoveryListener listener = new DiscoveryListener() {
59         @Override
60         public void thingRemoved(DiscoveryService source, ThingUID thingUID) {
61         }
62
63         @Override
64         public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
65             discoveryResult = result;
66         }
67
68         @Override
69         public Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp,
70                 Collection<ThingTypeUID> thingTypeUIDs, ThingUID bridgeUID) {
71             return null;
72         }
73     };
74
75     private DiscoveryResult discoveryResult;
76
77     private TradfriDiscoveryService discovery;
78
79     @BeforeEach
80     public void setUp() {
81         when(handler.getThing()).thenReturn(BridgeBuilder.create(GATEWAY_TYPE_UID, "1").build());
82
83         discovery = new TradfriDiscoveryService();
84         discovery.setThingHandler(handler);
85         discovery.addDiscoveryListener(listener);
86     }
87
88     @AfterEach
89     public void cleanUp() {
90         discoveryResult = null;
91     }
92
93     @Test
94     public void correctSupportedTypes() {
95         assertThat(discovery.getSupportedThingTypes().size(), is(9));
96         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_DIMMABLE_LIGHT));
97         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_COLOR_TEMP_LIGHT));
98         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_COLOR_LIGHT));
99         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_DIMMER));
100         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_MOTION_SENSOR));
101         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_REMOTE_CONTROL));
102         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_OPEN_CLOSE_REMOTE_CONTROL));
103         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_ONOFF_PLUG));
104         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_BLINDS));
105     }
106
107     @Test
108     public void validDiscoveryResultWhiteLightW() {
109         String json = "{\"9001\":\"TRADFRI bulb E27 W opal 1000lm\",\"9002\":1492856270,\"9020\":1507194357,\"9003\":65537,\"3311\":[{\"5850\":1,\"5851\":254,\"9003\":0}],\"9054\":0,\"5750\":2,\"9019\":1,\"3\":{\"0\":\"IKEA of Sweden\",\"1\":\"TRADFRI bulb E27 W opal 1000lm\",\"2\":\"\",\"3\":\"1.2.214\",\"6\":1}}";
110         JsonObject data = JsonParser.parseString(json).getAsJsonObject();
111
112         discovery.onUpdate("65537", data);
113
114         assertNotNull(discoveryResult);
115         assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
116         assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0100:1:65537")));
117         assertThat(discoveryResult.getThingTypeUID(), is(THING_TYPE_DIMMABLE_LIGHT));
118         assertThat(discoveryResult.getBridgeUID(), is(GATEWAY_THING_UID));
119         assertThat(discoveryResult.getProperties().get(CONFIG_ID), is(65537));
120         assertThat(discoveryResult.getRepresentationProperty(), is(CONFIG_ID));
121     }
122
123     @Test
124     public void validDiscoveryResultWhiteLightWS() {
125         String json = "{\"9001\":\"TRADFRI bulb E27 WS opal 980lm\",\"9002\":1492955148,\"9020\":1507200447,\"9003\":65537,\"3311\":[{\"5710\":26909,\"5850\":1,\"5851\":203,\"5707\":0,\"5708\":0,\"5709\":30140,\"5711\":370,\"5706\":\"f1e0b5\",\"9003\":0}],\"9054\":0,\"5750\":2,\"9019\":1,\"3\":{\"0\":\"IKEA of Sweden\",\"1\":\"TRADFRI bulb E27 WS opal 980lm\",\"2\":\"\",\"3\":\"1.2.217\",\"6\":1}}";
126         JsonObject data = JsonParser.parseString(json).getAsJsonObject();
127
128         discovery.onUpdate("65537", data);
129
130         assertNotNull(discoveryResult);
131         assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
132         assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0220:1:65537")));
133         assertThat(discoveryResult.getThingTypeUID(), is(THING_TYPE_COLOR_TEMP_LIGHT));
134         assertThat(discoveryResult.getBridgeUID(), is(GATEWAY_THING_UID));
135         assertThat(discoveryResult.getProperties().get(CONFIG_ID), is(65537));
136         assertThat(discoveryResult.getRepresentationProperty(), is(CONFIG_ID));
137     }
138
139     @Test
140     public void validDiscoveryResultWhiteLightWSWithIncompleteJson() {
141         // We do not always receive a COLOR = "5706" attribute, even the light supports it - but the gateway does not
142         // seem to have this information, if the bulb is unreachable.
143         String json = "{\"9001\":\"TRADFRI bulb E27 WS opal 980lm\",\"9002\":1492955148,\"9020\":1506968670,\"9003\":65537,\"3311\":[{\"9003\":0}],\"9054\":0,\"5750\":2,\"9019\":0,\"3\":{\"0\":\"IKEA of Sweden\",\"1\":\"TRADFRI bulb E27 WS opal 980lm\",\"2\":\"\",\"3\":\"1.2.217\",\"6\":1}}";
144         JsonObject data = JsonParser.parseString(json).getAsJsonObject();
145
146         discovery.onUpdate("65537", data);
147
148         assertNotNull(discoveryResult);
149         assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
150         assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0220:1:65537")));
151         assertThat(discoveryResult.getThingTypeUID(), is(THING_TYPE_COLOR_TEMP_LIGHT));
152         assertThat(discoveryResult.getBridgeUID(), is(GATEWAY_THING_UID));
153         assertThat(discoveryResult.getProperties().get(CONFIG_ID), is(65537));
154         assertThat(discoveryResult.getRepresentationProperty(), is(CONFIG_ID));
155     }
156
157     @Test
158     public void validDiscoveryResultColorLightCWS() {
159         String json = "{\"9001\":\"TRADFRI bulb E27 CWS opal 600lm\",\"9002\":1505151864,\"9020\":1505433527,\"9003\":65550,\"9019\":1,\"9054\":0,\"5750\":2,\"3\":{\"0\":\"IKEA of Sweden\",\"1\":\"TRADFRI bulb E27 CWS opal 600lm\",\"2\":\"\",\"3\":\"1.3.002\",\"6\":1},\"3311\":[{\"5850\":1,\"5708\":0,\"5851\":254,\"5707\":0,\"5709\":33137,\"5710\":27211,\"5711\":0,\"5706\":\"efd275\",\"9003\":0}]}";
160         JsonObject data = JsonParser.parseString(json).getAsJsonObject();
161
162         discovery.onUpdate("65550", data);
163
164         assertNotNull(discoveryResult);
165         assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
166         assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0210:1:65550")));
167         assertThat(discoveryResult.getThingTypeUID(), is(THING_TYPE_COLOR_LIGHT));
168         assertThat(discoveryResult.getBridgeUID(), is(GATEWAY_THING_UID));
169         assertThat(discoveryResult.getProperties().get(CONFIG_ID), is(65550));
170         assertThat(discoveryResult.getRepresentationProperty(), is(CONFIG_ID));
171     }
172
173     @Test
174     public void validDiscoveryResultAlternativeColorLightCWS() {
175         String json = "{\"3311\":[{\"5850\":1,\"5709\":32886,\"5851\":216,\"5707\":5309,\"5708\":52400,\"5710\":27217,\"5706\":\"efd275\",\"9003\":0}],\"9001\":\"Mushroom lamp\",\"9002\":1571036916,\"9020\":1571588312,\"9003\":65539,\"9054\":0,\"9019\":1,\"3\":{\"0\":\"IKEA of Sweden\",\"1\":\"TRADFRI bulb E27 C\\/WS opal 600\",\"2\":\"\",\"3\":\"1.3.009\",\"6\":1},\"5750\":2}";
176         JsonObject data = JsonParser.parseString(json).getAsJsonObject();
177
178         discovery.onUpdate("65539", data);
179
180         assertNotNull(discoveryResult);
181         assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
182         assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0210:1:65539")));
183         assertThat(discoveryResult.getThingTypeUID(), is(THING_TYPE_COLOR_LIGHT));
184         assertThat(discoveryResult.getBridgeUID(), is(GATEWAY_THING_UID));
185         assertThat(discoveryResult.getProperties().get(CONFIG_ID), is(65539));
186         assertThat(discoveryResult.getRepresentationProperty(), is(CONFIG_ID));
187     }
188
189     @Test
190     public void validDiscoveryResultRemoteControl() {
191         String json = "{\"9001\":\"TRADFRI remote control\",\"9002\":1492843083,\"9020\":1506977986,\"9003\":65536,\"9054\":0,\"5750\":0,\"9019\":1,\"3\":{\"0\":\"IKEA of Sweden\",\"1\":\"TRADFRI remote control\",\"2\":\"\",\"3\":\"1.2.214\",\"6\":3,\"9\":47},\"15009\":[{\"9003\":0}]}";
192         JsonObject data = JsonParser.parseString(json).getAsJsonObject();
193
194         discovery.onUpdate("65536", data);
195
196         assertNotNull(discoveryResult);
197         assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
198         assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0830:1:65536")));
199         assertThat(discoveryResult.getThingTypeUID(), is(THING_TYPE_REMOTE_CONTROL));
200         assertThat(discoveryResult.getBridgeUID(), is(GATEWAY_THING_UID));
201         assertThat(discoveryResult.getProperties().get(CONFIG_ID), is(65536));
202         assertThat(discoveryResult.getRepresentationProperty(), is(CONFIG_ID));
203     }
204
205     @Test
206     public void validDiscoveryResultWirelessDimmer() {
207         String json = "{\"9001\":\"TRADFRI wireless dimmer\",\"9002\":1492843083,\"9020\":1506977986,\"9003\":65536,\"9054\":0,\"5750\":0,\"9019\":1,\"3\":{\"0\":\"IKEA of Sweden\",\"1\":\"TRADFRI wireless dimmer\",\"2\":\"\",\"3\":\"1.2.214\",\"6\":3,\"9\":47},\"15009\":[{\"9003\":0}]}";
208         JsonObject data = JsonParser.parseString(json).getAsJsonObject();
209
210         discovery.onUpdate("65536", data);
211
212         assertNotNull(discoveryResult);
213         assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
214         assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0820:1:65536")));
215         assertThat(discoveryResult.getThingTypeUID(), is(THING_TYPE_DIMMER));
216         assertThat(discoveryResult.getBridgeUID(), is(GATEWAY_THING_UID));
217         assertThat(discoveryResult.getProperties().get(CONFIG_ID), is(65536));
218         assertThat(discoveryResult.getRepresentationProperty(), is(CONFIG_ID));
219     }
220
221     @Test
222     public void validDiscoveryResultMotionSensor() {
223         String json = "{\"9001\":\"TRADFRI motion sensor\",\"9002\":1492955083,\"9020\":1507120083,\"9003\":65538,\"9054\":0,\"5750\":4,\"9019\":1,\"3\":{\"0\":\"IKEA of Sweden\",\"1\":\"TRADFRI motion sensor\",\"2\":\"\",\"3\":\"1.2.214\",\"6\":3,\"9\":60},\"3300\":[{\"9003\":0}]}";
224         JsonObject data = JsonParser.parseString(json).getAsJsonObject();
225
226         discovery.onUpdate("65538", data);
227
228         assertNotNull(discoveryResult);
229         assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
230         assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0107:1:65538")));
231         assertThat(discoveryResult.getThingTypeUID(), is(THING_TYPE_MOTION_SENSOR));
232         assertThat(discoveryResult.getBridgeUID(), is(GATEWAY_THING_UID));
233         assertThat(discoveryResult.getProperties().get(CONFIG_ID), is(65538));
234         assertThat(discoveryResult.getRepresentationProperty(), is(CONFIG_ID));
235     }
236 }