]> git.basschouten.com Git - openhab-addons.git/blob
5fc80d96c589bf8ad5050f39b55af72f040a267d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.junit.Assert.*;
17 import static org.mockito.Mockito.when;
18 import static org.mockito.MockitoAnnotations.initMocks;
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.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.openhab.binding.tradfri.internal.handler.TradfriGatewayHandler;
29 import org.openhab.core.config.discovery.DiscoveryListener;
30 import org.openhab.core.config.discovery.DiscoveryResult;
31 import org.openhab.core.config.discovery.DiscoveryResultFlag;
32 import org.openhab.core.config.discovery.DiscoveryService;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.builder.BridgeBuilder;
36
37 import com.google.gson.JsonObject;
38 import com.google.gson.JsonParser;
39
40 /**
41  * Tests for {@link TradfriDiscoveryService}.
42  *
43  * @author Kai Kreuzer - Initial contribution
44  * @author Christoph Weitkamp - Added support for remote controller and motion sensor devices (read-only battery level)
45  */
46 public class TradfriDiscoveryServiceTest {
47
48     private static final ThingUID GATEWAY_THING_UID = new ThingUID("tradfri:gateway:1");
49
50     @Mock
51     private TradfriGatewayHandler handler;
52
53     private final DiscoveryListener listener = new DiscoveryListener() {
54         @Override
55         public void thingRemoved(DiscoveryService source, ThingUID thingUID) {
56         }
57
58         @Override
59         public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
60             discoveryResult = result;
61         }
62
63         @Override
64         public Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp,
65                 Collection<ThingTypeUID> thingTypeUIDs, ThingUID bridgeUID) {
66             return null;
67         }
68     };
69
70     private DiscoveryResult discoveryResult;
71
72     private TradfriDiscoveryService discovery;
73
74     @Before
75     public void setUp() {
76         initMocks(this);
77
78         when(handler.getThing()).thenReturn(BridgeBuilder.create(GATEWAY_TYPE_UID, "1").build());
79
80         discovery = new TradfriDiscoveryService();
81         discovery.setThingHandler(handler);
82         discovery.addDiscoveryListener(listener);
83     }
84
85     @After
86     public void cleanUp() {
87         discoveryResult = null;
88     }
89
90     @Test
91     public void correctSupportedTypes() {
92         assertThat(discovery.getSupportedThingTypes().size(), is(9));
93         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_DIMMABLE_LIGHT));
94         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_COLOR_TEMP_LIGHT));
95         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_COLOR_LIGHT));
96         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_DIMMER));
97         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_MOTION_SENSOR));
98         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_REMOTE_CONTROL));
99         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_OPEN_CLOSE_REMOTE_CONTROL));
100         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_ONOFF_PLUG));
101         assertTrue(discovery.getSupportedThingTypes().contains(THING_TYPE_BLINDS));
102     }
103
104     @Test
105     public void validDiscoveryResultWhiteLightW() {
106         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}}";
107         JsonObject data = new JsonParser().parse(json).getAsJsonObject();
108
109         discovery.onUpdate("65537", data);
110
111         assertNotNull(discoveryResult);
112         assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
113         assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0100:1:65537")));
114         assertThat(discoveryResult.getThingTypeUID(), is(THING_TYPE_DIMMABLE_LIGHT));
115         assertThat(discoveryResult.getBridgeUID(), is(GATEWAY_THING_UID));
116         assertThat(discoveryResult.getProperties().get(CONFIG_ID), is(65537));
117         assertThat(discoveryResult.getRepresentationProperty(), is(CONFIG_ID));
118     }
119
120     @Test
121     public void validDiscoveryResultWhiteLightWS() {
122         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}}";
123         JsonObject data = new JsonParser().parse(json).getAsJsonObject();
124
125         discovery.onUpdate("65537", data);
126
127         assertNotNull(discoveryResult);
128         assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
129         assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0220:1:65537")));
130         assertThat(discoveryResult.getThingTypeUID(), is(THING_TYPE_COLOR_TEMP_LIGHT));
131         assertThat(discoveryResult.getBridgeUID(), is(GATEWAY_THING_UID));
132         assertThat(discoveryResult.getProperties().get(CONFIG_ID), is(65537));
133         assertThat(discoveryResult.getRepresentationProperty(), is(CONFIG_ID));
134     }
135
136     @Test
137     public void validDiscoveryResultWhiteLightWSWithIncompleteJson() {
138         // We do not always receive a COLOR = "5706" attribute, even the light supports it - but the gateway does not
139         // seem to have this information, if the bulb is unreachable.
140         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}}";
141         JsonObject data = new JsonParser().parse(json).getAsJsonObject();
142
143         discovery.onUpdate("65537", data);
144
145         assertNotNull(discoveryResult);
146         assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
147         assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0220:1:65537")));
148         assertThat(discoveryResult.getThingTypeUID(), is(THING_TYPE_COLOR_TEMP_LIGHT));
149         assertThat(discoveryResult.getBridgeUID(), is(GATEWAY_THING_UID));
150         assertThat(discoveryResult.getProperties().get(CONFIG_ID), is(65537));
151         assertThat(discoveryResult.getRepresentationProperty(), is(CONFIG_ID));
152     }
153
154     @Test
155     public void validDiscoveryResultColorLightCWS() {
156         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}]}";
157         JsonObject data = new JsonParser().parse(json).getAsJsonObject();
158
159         discovery.onUpdate("65550", data);
160
161         assertNotNull(discoveryResult);
162         assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
163         assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0210:1:65550")));
164         assertThat(discoveryResult.getThingTypeUID(), is(THING_TYPE_COLOR_LIGHT));
165         assertThat(discoveryResult.getBridgeUID(), is(GATEWAY_THING_UID));
166         assertThat(discoveryResult.getProperties().get(CONFIG_ID), is(65550));
167         assertThat(discoveryResult.getRepresentationProperty(), is(CONFIG_ID));
168     }
169
170     @Test
171     public void validDiscoveryResultAlternativeColorLightCWS() {
172         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}";
173         JsonObject data = new JsonParser().parse(json).getAsJsonObject();
174
175         discovery.onUpdate("65539", data);
176
177         assertNotNull(discoveryResult);
178         assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
179         assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0210:1:65539")));
180         assertThat(discoveryResult.getThingTypeUID(), is(THING_TYPE_COLOR_LIGHT));
181         assertThat(discoveryResult.getBridgeUID(), is(GATEWAY_THING_UID));
182         assertThat(discoveryResult.getProperties().get(CONFIG_ID), is(65539));
183         assertThat(discoveryResult.getRepresentationProperty(), is(CONFIG_ID));
184     }
185
186     @Test
187     public void validDiscoveryResultRemoteControl() {
188         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}]}";
189         JsonObject data = new JsonParser().parse(json).getAsJsonObject();
190
191         discovery.onUpdate("65536", data);
192
193         assertNotNull(discoveryResult);
194         assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
195         assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0830:1:65536")));
196         assertThat(discoveryResult.getThingTypeUID(), is(THING_TYPE_REMOTE_CONTROL));
197         assertThat(discoveryResult.getBridgeUID(), is(GATEWAY_THING_UID));
198         assertThat(discoveryResult.getProperties().get(CONFIG_ID), is(65536));
199         assertThat(discoveryResult.getRepresentationProperty(), is(CONFIG_ID));
200     }
201
202     @Test
203     public void validDiscoveryResultWirelessDimmer() {
204         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}]}";
205         JsonObject data = new JsonParser().parse(json).getAsJsonObject();
206
207         discovery.onUpdate("65536", data);
208
209         assertNotNull(discoveryResult);
210         assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
211         assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0820:1:65536")));
212         assertThat(discoveryResult.getThingTypeUID(), is(THING_TYPE_DIMMER));
213         assertThat(discoveryResult.getBridgeUID(), is(GATEWAY_THING_UID));
214         assertThat(discoveryResult.getProperties().get(CONFIG_ID), is(65536));
215         assertThat(discoveryResult.getRepresentationProperty(), is(CONFIG_ID));
216     }
217
218     @Test
219     public void validDiscoveryResultMotionSensor() {
220         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}]}";
221         JsonObject data = new JsonParser().parse(json).getAsJsonObject();
222
223         discovery.onUpdate("65538", data);
224
225         assertNotNull(discoveryResult);
226         assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
227         assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0107:1:65538")));
228         assertThat(discoveryResult.getThingTypeUID(), is(THING_TYPE_MOTION_SENSOR));
229         assertThat(discoveryResult.getBridgeUID(), is(GATEWAY_THING_UID));
230         assertThat(discoveryResult.getProperties().get(CONFIG_ID), is(65538));
231         assertThat(discoveryResult.getRepresentationProperty(), is(CONFIG_ID));
232     }
233 }