2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nest.internal.sdm.dto;
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;
21 import java.io.IOException;
22 import java.math.BigDecimal;
23 import java.time.ZonedDateTime;
24 import java.util.List;
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;
49 * Tests deserialization of {@link org.openhab.binding.nest.internal.sdm.dto.SDMDevice}s from JSON.
51 * @author Wouter Born - Initial contribution
54 public class SDMDeviceTest {
57 public void deserializeThermostatDevice() throws IOException {
58 SDMDevice device = getThermostatDevice();
59 assertThat(device, is(notNullValue()));
61 assertThat(device.name.name, is("enterprises/project-id/devices/thermostat-device-id"));
62 assertThat(device.type, is(SDMDeviceType.THERMOSTAT));
64 SDMTraits traits = device.traits;
65 assertThat(traits, is(notNullValue()));
66 assertThat(traits.traitList(), hasSize(10));
68 SDMDeviceInfoTrait deviceInfo = traits.deviceInfo;
69 assertThat(deviceInfo, is(notNullValue()));
70 assertThat(deviceInfo.customName, is(""));
72 SDMHumidityTrait humidity = traits.humidity;
73 assertThat(humidity, is(notNullValue()));
74 assertThat(humidity.ambientHumidityPercent, is(new BigDecimal(26)));
76 SDMConnectivityTrait connectivity = traits.connectivity;
77 assertThat(connectivity, is(notNullValue()));
78 assertThat(connectivity.status, is(SDMConnectivityStatus.ONLINE));
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")));
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)));
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")));
98 SDMThermostatHvacTrait thermostatHvac = traits.thermostatHvac;
99 assertThat(thermostatHvac, is(notNullValue()));
100 assertThat(thermostatHvac.status, is(SDMHvacStatus.OFF));
102 SDMDeviceSettingsTrait deviceSettings = traits.deviceSettings;
103 assertThat(deviceSettings, is(notNullValue()));
104 assertThat(deviceSettings.temperatureScale, is(SDMTemperatureScale.CELSIUS));
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()));
111 SDMTemperatureTrait temperature = traits.temperature;
112 assertThat(temperature, is(notNullValue()));
113 assertThat(temperature.ambientTemperatureCelsius, is(new BigDecimal("19.73")));
115 List<SDMParentRelation> parentRelations = device.parentRelations;
116 assertThat(parentRelations, is(notNullValue()));
117 assertThat(parentRelations, hasSize(1));
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"));
124 protected SDMDevice getThermostatDevice() throws IOException {
125 return fromJson("thermostat-device-response.json", SDMDevice.class);
129 public void deserializeCameraDevice() throws IOException {
130 SDMDevice device = getCameraDevice();
131 assertThat(device, is(notNullValue()));
133 assertThat(device.name.name, is("enterprises/project-id/devices/camera-device-id"));
134 assertThat(device.type, is(SDMDeviceType.CAMERA));
136 SDMTraits traits = device.traits;
137 assertThat(traits, is(notNullValue()));
138 assertThat(traits.traitList(), hasSize(7));
140 SDMDeviceInfoTrait deviceInfo = traits.deviceInfo;
141 assertThat(deviceInfo, is(notNullValue()));
142 assertThat(deviceInfo.customName, is(""));
144 SDMConnectivityTrait connectivity = traits.connectivity;
145 assertThat(connectivity, is(nullValue()));
147 SDMCameraLiveStreamTrait cameraLiveStream = traits.cameraLiveStream;
148 assertThat(cameraLiveStream, is(notNullValue()));
150 SDMResolution maxVideoResolution = cameraLiveStream.maxVideoResolution;
151 assertThat(maxVideoResolution, is(notNullValue()));
152 assertThat(maxVideoResolution.width, is(640));
153 assertThat(maxVideoResolution.height, is(480));
155 assertThat(cameraLiveStream.videoCodecs, is(List.of("H264")));
156 assertThat(cameraLiveStream.audioCodecs, is(List.of("AAC")));
158 SDMCameraImageTrait cameraImage = traits.cameraImage;
159 assertThat(cameraImage, is(notNullValue()));
161 SDMResolution maxImageResolution = cameraImage.maxImageResolution;
162 assertThat(maxImageResolution, is(notNullValue()));
163 assertThat(maxImageResolution.width, is(1920));
164 assertThat(maxImageResolution.height, is(1200));
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()));
172 List<SDMParentRelation> parentRelations = device.parentRelations;
173 assertThat(parentRelations, is(notNullValue()));
174 assertThat(parentRelations, hasSize(1));
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"));
181 protected SDMDevice getCameraDevice() throws IOException {
182 return fromJson("camera-device-response.json", SDMDevice.class);
186 public void deserializeDisplayDevice() throws IOException {
187 SDMDevice device = getDisplayDevice();
188 assertThat(device, is(notNullValue()));
190 assertThat(device.name.name, is("enterprises/project-id/devices/display-device-id"));
191 assertThat(device.type, is(SDMDeviceType.DISPLAY));
193 SDMTraits traits = device.traits;
194 assertThat(traits, is(notNullValue()));
195 assertThat(traits.traitList(), hasSize(7));
197 SDMDeviceInfoTrait deviceInfo = traits.deviceInfo;
198 assertThat(deviceInfo, is(notNullValue()));
199 assertThat(deviceInfo.customName, is(""));
201 SDMConnectivityTrait connectivity = traits.connectivity;
202 assertThat(connectivity, is(nullValue()));
204 SDMCameraLiveStreamTrait cameraLiveStream = traits.cameraLiveStream;
205 assertThat(cameraLiveStream, is(notNullValue()));
207 SDMResolution maxVideoResolution = cameraLiveStream.maxVideoResolution;
208 assertThat(maxVideoResolution, is(notNullValue()));
209 assertThat(maxVideoResolution.width, is(640));
210 assertThat(maxVideoResolution.height, is(480));
212 assertThat(cameraLiveStream.videoCodecs, is(List.of("H264")));
213 assertThat(cameraLiveStream.audioCodecs, is(List.of("AAC")));
215 SDMCameraImageTrait cameraImage = traits.cameraImage;
216 assertThat(cameraImage, is(notNullValue()));
218 SDMResolution maxImageResolution = cameraImage.maxImageResolution;
219 assertThat(maxImageResolution, is(notNullValue()));
220 assertThat(maxImageResolution.width, is(1920));
221 assertThat(maxImageResolution.height, is(1200));
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()));
229 List<SDMParentRelation> parentRelations = device.parentRelations;
230 assertThat(parentRelations, is(notNullValue()));
231 assertThat(parentRelations, hasSize(1));
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"));
238 protected SDMDevice getDisplayDevice() throws IOException {
239 return fromJson("display-device-response.json", SDMDevice.class);
243 public void deserializeDoorbellDevice() throws IOException {
244 SDMDevice device = getDoorbellDevice();
245 assertThat(device, is(notNullValue()));
247 assertThat(device.name.name, is("enterprises/project-id/devices/doorbell-device-id"));
248 assertThat(device.type, is(SDMDeviceType.DOORBELL));
250 SDMTraits traits = device.traits;
251 assertThat(traits, is(notNullValue()));
252 assertThat(traits.traitList(), hasSize(8));
254 SDMDeviceInfoTrait deviceInfo = traits.deviceInfo;
255 assertThat(deviceInfo, is(notNullValue()));
256 assertThat(deviceInfo.customName, is(""));
258 SDMConnectivityTrait connectivity = traits.connectivity;
259 assertThat(connectivity, is(nullValue()));
261 SDMCameraLiveStreamTrait cameraLiveStream = traits.cameraLiveStream;
262 assertThat(cameraLiveStream, is(notNullValue()));
264 SDMResolution maxVideoResolution = cameraLiveStream.maxVideoResolution;
265 assertThat(maxVideoResolution, is(notNullValue()));
266 assertThat(maxVideoResolution.width, is(640));
267 assertThat(maxVideoResolution.height, is(480));
269 assertThat(cameraLiveStream.videoCodecs, is(List.of("H264")));
270 assertThat(cameraLiveStream.audioCodecs, is(List.of("AAC")));
272 SDMCameraImageTrait cameraImage = traits.cameraImage;
273 assertThat(cameraImage, is(notNullValue()));
275 SDMResolution maxImageResolution = cameraImage.maxImageResolution;
276 assertThat(maxImageResolution, is(notNullValue()));
277 assertThat(maxImageResolution.width, is(1920));
278 assertThat(maxImageResolution.height, is(1200));
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()));
286 List<SDMParentRelation> parentRelations = device.parentRelations;
287 assertThat(parentRelations, is(notNullValue()));
288 assertThat(parentRelations, hasSize(1));
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"));
295 protected SDMDevice getDoorbellDevice() throws IOException {
296 return fromJson("doorbell-device-response.json", SDMDevice.class);