]> git.basschouten.com Git - openhab-addons.git/blob
b6b444859f2dad6d4d1a0fcd2dfceff070dbe3fd
[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.nest.internal.sdm.dto;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
18 import static org.hamcrest.core.Is.is;
19 import static org.openhab.binding.nest.internal.sdm.dto.SDMDataUtil.fromJson;
20
21 import java.io.IOException;
22 import java.math.BigDecimal;
23 import java.time.ZonedDateTime;
24 import java.util.List;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.junit.jupiter.api.Test;
28 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMCameraImageTrait;
29 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMCameraLiveStreamTrait;
30 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMConnectivityStatus;
31 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMConnectivityTrait;
32 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMDeviceInfoTrait;
33 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMDeviceSettingsTrait;
34 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMFanTimerMode;
35 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMFanTrait;
36 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMHumidityTrait;
37 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMHvacStatus;
38 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMResolution;
39 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMTemperatureScale;
40 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMTemperatureTrait;
41 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMThermostatEcoMode;
42 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMThermostatEcoTrait;
43 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMThermostatHvacTrait;
44 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMThermostatMode;
45 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMThermostatModeTrait;
46 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMThermostatTemperatureSetpointTrait;
47
48 /**
49  * Tests deserialization of {@link org.openhab.binding.nest.internal.sdm.dto.SDMDevice}s from JSON.
50  *
51  * @author Wouter Born - Initial contribution
52  */
53 @NonNullByDefault
54 public class SDMDeviceTest {
55
56     @Test
57     public void deserializeThermostatDevice() throws IOException {
58         SDMDevice device = getThermostatDevice();
59         assertThat(device, is(notNullValue()));
60
61         assertThat(device.name.name, is("enterprises/project-id/devices/thermostat-device-id"));
62         assertThat(device.type, is(SDMDeviceType.THERMOSTAT));
63
64         SDMTraits traits = device.traits;
65         assertThat(traits, is(notNullValue()));
66         assertThat(traits.traitList(), hasSize(10));
67
68         SDMDeviceInfoTrait deviceInfo = traits.deviceInfo;
69         assertThat(deviceInfo, is(notNullValue()));
70         assertThat(deviceInfo.customName, is(""));
71
72         SDMHumidityTrait humidity = traits.humidity;
73         assertThat(humidity, is(notNullValue()));
74         assertThat(humidity.ambientHumidityPercent, is(new BigDecimal(26)));
75
76         SDMConnectivityTrait connectivity = traits.connectivity;
77         assertThat(connectivity, is(notNullValue()));
78         assertThat(connectivity.status, is(SDMConnectivityStatus.ONLINE));
79
80         SDMFanTrait fan = traits.fan;
81         assertThat(fan, is(notNullValue()));
82         assertThat(fan.timerMode, is(SDMFanTimerMode.ON));
83         assertThat(fan.timerTimeout, is(ZonedDateTime.parse("2019-05-10T03:22:54Z")));
84
85         SDMThermostatModeTrait thermostatMode = traits.thermostatMode;
86         assertThat(thermostatMode, is(notNullValue()));
87         assertThat(thermostatMode.mode, is(SDMThermostatMode.HEAT));
88         assertThat(thermostatMode.availableModes, is(List.of(SDMThermostatMode.HEAT, SDMThermostatMode.OFF)));
89
90         SDMThermostatEcoTrait thermostatEco = traits.thermostatEco;
91         assertThat(thermostatEco, is(notNullValue()));
92         assertThat(thermostatEco.availableModes,
93                 is(List.of(SDMThermostatEcoMode.OFF, SDMThermostatEcoMode.MANUAL_ECO)));
94         assertThat(thermostatEco.mode, is(SDMThermostatEcoMode.OFF));
95         assertThat(thermostatEco.heatCelsius, is(new BigDecimal("15.34473")));
96         assertThat(thermostatEco.coolCelsius, is(new BigDecimal("24.44443")));
97
98         SDMThermostatHvacTrait thermostatHvac = traits.thermostatHvac;
99         assertThat(thermostatHvac, is(notNullValue()));
100         assertThat(thermostatHvac.status, is(SDMHvacStatus.OFF));
101
102         SDMDeviceSettingsTrait deviceSettings = traits.deviceSettings;
103         assertThat(deviceSettings, is(notNullValue()));
104         assertThat(deviceSettings.temperatureScale, is(SDMTemperatureScale.CELSIUS));
105
106         SDMThermostatTemperatureSetpointTrait thermostatTemperatureSetpoint = traits.thermostatTemperatureSetpoint;
107         assertThat(thermostatTemperatureSetpoint, is(notNullValue()));
108         assertThat(thermostatTemperatureSetpoint.heatCelsius, is(new BigDecimal("14.92249")));
109         assertThat(thermostatTemperatureSetpoint.coolCelsius, is(nullValue()));
110
111         SDMTemperatureTrait temperature = traits.temperature;
112         assertThat(temperature, is(notNullValue()));
113         assertThat(temperature.ambientTemperatureCelsius, is(new BigDecimal("19.73")));
114
115         List<SDMParentRelation> parentRelations = device.parentRelations;
116         assertThat(parentRelations, is(notNullValue()));
117         assertThat(parentRelations, hasSize(1));
118
119         assertThat(parentRelations.get(0).parent.name,
120                 is("enterprises/project-id/structures/structure-id/rooms/thermostat-room-id"));
121         assertThat(parentRelations.get(0).displayName, is("Thermostat Room Name"));
122     }
123
124     protected SDMDevice getThermostatDevice() throws IOException {
125         return fromJson("thermostat-device-response.json", SDMDevice.class);
126     }
127
128     @Test
129     public void deserializeCameraDevice() throws IOException {
130         SDMDevice device = getCameraDevice();
131         assertThat(device, is(notNullValue()));
132
133         assertThat(device.name.name, is("enterprises/project-id/devices/camera-device-id"));
134         assertThat(device.type, is(SDMDeviceType.CAMERA));
135
136         SDMTraits traits = device.traits;
137         assertThat(traits, is(notNullValue()));
138         assertThat(traits.traitList(), hasSize(7));
139
140         SDMDeviceInfoTrait deviceInfo = traits.deviceInfo;
141         assertThat(deviceInfo, is(notNullValue()));
142         assertThat(deviceInfo.customName, is(""));
143
144         SDMConnectivityTrait connectivity = traits.connectivity;
145         assertThat(connectivity, is(nullValue()));
146
147         SDMCameraLiveStreamTrait cameraLiveStream = traits.cameraLiveStream;
148         assertThat(cameraLiveStream, is(notNullValue()));
149
150         SDMResolution maxVideoResolution = cameraLiveStream.maxVideoResolution;
151         assertThat(maxVideoResolution, is(notNullValue()));
152         assertThat(maxVideoResolution.width, is(640));
153         assertThat(maxVideoResolution.height, is(480));
154
155         assertThat(cameraLiveStream.videoCodecs, is(List.of("H264")));
156         assertThat(cameraLiveStream.audioCodecs, is(List.of("AAC")));
157
158         SDMCameraImageTrait cameraImage = traits.cameraImage;
159         assertThat(cameraImage, is(notNullValue()));
160
161         SDMResolution maxImageResolution = cameraImage.maxImageResolution;
162         assertThat(maxImageResolution, is(notNullValue()));
163         assertThat(maxImageResolution.width, is(1920));
164         assertThat(maxImageResolution.height, is(1200));
165
166         assertThat(traits.cameraPerson, is(notNullValue()));
167         assertThat(traits.cameraSound, is(notNullValue()));
168         assertThat(traits.cameraMotion, is(notNullValue()));
169         assertThat(traits.cameraEventImage, is(notNullValue()));
170         assertThat(traits.doorbellChime, is(nullValue()));
171
172         List<SDMParentRelation> parentRelations = device.parentRelations;
173         assertThat(parentRelations, is(notNullValue()));
174         assertThat(parentRelations, hasSize(1));
175
176         assertThat(parentRelations.get(0).parent.name,
177                 is("enterprises/project-id/structures/structure-id/rooms/camera-room-id"));
178         assertThat(parentRelations.get(0).displayName, is("Camera Room Name"));
179     }
180
181     protected SDMDevice getCameraDevice() throws IOException {
182         return fromJson("camera-device-response.json", SDMDevice.class);
183     }
184
185     @Test
186     public void deserializeDisplayDevice() throws IOException {
187         SDMDevice device = getDisplayDevice();
188         assertThat(device, is(notNullValue()));
189
190         assertThat(device.name.name, is("enterprises/project-id/devices/display-device-id"));
191         assertThat(device.type, is(SDMDeviceType.DISPLAY));
192
193         SDMTraits traits = device.traits;
194         assertThat(traits, is(notNullValue()));
195         assertThat(traits.traitList(), hasSize(7));
196
197         SDMDeviceInfoTrait deviceInfo = traits.deviceInfo;
198         assertThat(deviceInfo, is(notNullValue()));
199         assertThat(deviceInfo.customName, is(""));
200
201         SDMConnectivityTrait connectivity = traits.connectivity;
202         assertThat(connectivity, is(nullValue()));
203
204         SDMCameraLiveStreamTrait cameraLiveStream = traits.cameraLiveStream;
205         assertThat(cameraLiveStream, is(notNullValue()));
206
207         SDMResolution maxVideoResolution = cameraLiveStream.maxVideoResolution;
208         assertThat(maxVideoResolution, is(notNullValue()));
209         assertThat(maxVideoResolution.width, is(640));
210         assertThat(maxVideoResolution.height, is(480));
211
212         assertThat(cameraLiveStream.videoCodecs, is(List.of("H264")));
213         assertThat(cameraLiveStream.audioCodecs, is(List.of("AAC")));
214
215         SDMCameraImageTrait cameraImage = traits.cameraImage;
216         assertThat(cameraImage, is(notNullValue()));
217
218         SDMResolution maxImageResolution = cameraImage.maxImageResolution;
219         assertThat(maxImageResolution, is(notNullValue()));
220         assertThat(maxImageResolution.width, is(1920));
221         assertThat(maxImageResolution.height, is(1200));
222
223         assertThat(traits.cameraPerson, is(notNullValue()));
224         assertThat(traits.cameraSound, is(notNullValue()));
225         assertThat(traits.cameraMotion, is(notNullValue()));
226         assertThat(traits.cameraEventImage, is(notNullValue()));
227         assertThat(traits.doorbellChime, is(nullValue()));
228
229         List<SDMParentRelation> parentRelations = device.parentRelations;
230         assertThat(parentRelations, is(notNullValue()));
231         assertThat(parentRelations, hasSize(1));
232
233         assertThat(parentRelations.get(0).parent.name,
234                 is("enterprises/project-id/structures/structure-id/rooms/display-room-id"));
235         assertThat(parentRelations.get(0).displayName, is("Display Room Name"));
236     }
237
238     protected SDMDevice getDisplayDevice() throws IOException {
239         return fromJson("display-device-response.json", SDMDevice.class);
240     }
241
242     @Test
243     public void deserializeDoorbellDevice() throws IOException {
244         SDMDevice device = getDoorbellDevice();
245         assertThat(device, is(notNullValue()));
246
247         assertThat(device.name.name, is("enterprises/project-id/devices/doorbell-device-id"));
248         assertThat(device.type, is(SDMDeviceType.DOORBELL));
249
250         SDMTraits traits = device.traits;
251         assertThat(traits, is(notNullValue()));
252         assertThat(traits.traitList(), hasSize(8));
253
254         SDMDeviceInfoTrait deviceInfo = traits.deviceInfo;
255         assertThat(deviceInfo, is(notNullValue()));
256         assertThat(deviceInfo.customName, is(""));
257
258         SDMConnectivityTrait connectivity = traits.connectivity;
259         assertThat(connectivity, is(nullValue()));
260
261         SDMCameraLiveStreamTrait cameraLiveStream = traits.cameraLiveStream;
262         assertThat(cameraLiveStream, is(notNullValue()));
263
264         SDMResolution maxVideoResolution = cameraLiveStream.maxVideoResolution;
265         assertThat(maxVideoResolution, is(notNullValue()));
266         assertThat(maxVideoResolution.width, is(640));
267         assertThat(maxVideoResolution.height, is(480));
268
269         assertThat(cameraLiveStream.videoCodecs, is(List.of("H264")));
270         assertThat(cameraLiveStream.audioCodecs, is(List.of("AAC")));
271
272         SDMCameraImageTrait cameraImage = traits.cameraImage;
273         assertThat(cameraImage, is(notNullValue()));
274
275         SDMResolution maxImageResolution = cameraImage.maxImageResolution;
276         assertThat(maxImageResolution, is(notNullValue()));
277         assertThat(maxImageResolution.width, is(1920));
278         assertThat(maxImageResolution.height, is(1200));
279
280         assertThat(traits.cameraPerson, is(notNullValue()));
281         assertThat(traits.cameraSound, is(notNullValue()));
282         assertThat(traits.cameraMotion, is(notNullValue()));
283         assertThat(traits.cameraEventImage, is(notNullValue()));
284         assertThat(traits.doorbellChime, is(notNullValue()));
285
286         List<SDMParentRelation> parentRelations = device.parentRelations;
287         assertThat(parentRelations, is(notNullValue()));
288         assertThat(parentRelations, hasSize(1));
289
290         assertThat(parentRelations.get(0).parent.name,
291                 is("enterprises/project-id/structures/structure-id/rooms/doorbell-room-id"));
292         assertThat(parentRelations.get(0).displayName, is("Doorbell Room Name"));
293     }
294
295     protected SDMDevice getDoorbellDevice() throws IOException {
296         return fromJson("doorbell-device-response.json", SDMDevice.class);
297     }
298 }