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 java.util.Map.entry;
17 import java.math.BigDecimal;
18 import java.time.Duration;
19 import java.time.ZonedDateTime;
20 import java.util.LinkedHashMap;
22 import java.util.Map.Entry;
24 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMFanTimerMode;
25 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMThermostatEcoMode;
26 import org.openhab.binding.nest.internal.sdm.dto.SDMTraits.SDMThermostatMode;
29 * The {@link SDMCommands} provides classes used for mapping all SDM REST API device command requests and responses.
31 * @author Wouter Born - Initial contribution
33 * @see https://developers.google.com/nest/device-access/reference/rest/v1/enterprises.devices/executeCommand
35 public class SDMCommands {
38 * Command request parent.
40 public abstract static class SDMCommandRequest<T extends SDMCommandResponse> {
41 private final String command;
42 private final Map<String, Object> params = new LinkedHashMap<>();
45 private SDMCommandRequest(String command, Entry<String, Object>... params) {
46 this.command = command;
47 for (Entry<String, Object> param : params) {
48 this.params.put(param.getKey(), param.getValue());
52 public String getCommand() {
56 public Map<String, Object> getParams() {
60 @SuppressWarnings("unchecked")
61 public Class<T> getResponseClass() {
62 return (Class<T>) SDMCommandResponse.class;
67 * Command response parent. This class is also used for responses without additional data.
69 public static class SDMCommandResponse {
72 // CameraEventImage trait commands
75 * Generates a download URL for the image related to a camera event.
77 public static class SDMGenerateCameraImageRequest extends SDMCommandRequest<SDMGenerateCameraImageResponse> {
80 * Event images expire 30 seconds after the event is published. Make sure to download the image prior to
83 public static final Duration EVENT_IMAGE_VALIDITY = Duration.ofSeconds(30);
86 * @param eventId ID of the camera event to request a related image for.
88 public SDMGenerateCameraImageRequest(String eventId) {
89 super("sdm.devices.commands.CameraEventImage.GenerateImage", entry("eventId", eventId));
93 public Class<SDMGenerateCameraImageResponse> getResponseClass() {
94 return SDMGenerateCameraImageResponse.class;
98 public static class SDMGenerateCameraImageResults {
100 * The URL to download the camera image from.
105 * Token to use in the HTTP Authorization header when downloading the camera image.
110 public static class SDMGenerateCameraImageResponse extends SDMCommandResponse {
111 public SDMGenerateCameraImageResults results;
114 // CameraLiveStream trait commands
117 * Request a token to access a camera RTSP live stream URL.
119 public static class SDMGenerateCameraRtspStreamRequest
120 extends SDMCommandRequest<SDMGenerateCameraRtspStreamResponse> {
121 public SDMGenerateCameraRtspStreamRequest() {
122 super("sdm.devices.commands.CameraLiveStream.GenerateRtspStream");
126 public Class<SDMGenerateCameraRtspStreamResponse> getResponseClass() {
127 return SDMGenerateCameraRtspStreamResponse.class;
132 * Camera RTSP live stream URLs.
134 public static class SDMCameraRtspStreamUrls {
135 public String rtspUrl;
138 public static class SDMGenerateCameraRtspStreamResults {
140 * Camera RTSP live stream URLs.
142 public SDMCameraRtspStreamUrls streamUrls;
145 * Token to use to extend the {@link #streamToken} for an RTSP live stream.
147 public String streamExtensionToken;
150 * Token to use to access an RTSP live stream.
152 public String streamToken;
155 * Time at which both {@link #streamExtensionToken} and {@link #streamToken} expire.
157 public ZonedDateTime expiresAt;
160 public static class SDMGenerateCameraRtspStreamResponse extends SDMCommandResponse {
161 public SDMGenerateCameraRtspStreamResults results;
165 * Request a new RTSP live stream URL access token to replace a valid RTSP access token before it expires. This is
166 * also used to replace a valid RTSP token from a previous ExtendRtspStream command request.
168 public static class SDMExtendCameraRtspStreamRequest extends SDMCommandRequest<SDMExtendCameraRtspStreamResponse> {
170 * @param streamExtensionToken Token to use to request an extension to the RTSP streaming token.
172 public SDMExtendCameraRtspStreamRequest(String streamExtensionToken) {
173 super("sdm.devices.commands.CameraLiveStream.ExtendRtspStream",
174 entry("streamExtensionToken", streamExtensionToken));
178 public Class<SDMExtendCameraRtspStreamResponse> getResponseClass() {
179 return SDMExtendCameraRtspStreamResponse.class;
183 public static class SDMExtendCameraRtspStreamResults {
185 * Token to use to view an existing RTSP live stream and to request an extension to the streaming token.
187 public String streamExtensionToken;
190 * New token to use to access an existing RTSP live stream.
192 public String streamToken;
195 * Time at which both {@link #streamExtensionToken} and {@link #streamToken} expire.
197 public ZonedDateTime expiresAt;
200 public static class SDMExtendCameraRtspStreamResponse extends SDMCommandResponse {
201 public SDMExtendCameraRtspStreamResults results;
205 * Invalidates a valid RTSP access token and stops the RTSP live stream tied to that access token.
207 public static class SDMStopCameraRtspStreamRequest extends SDMCommandRequest<SDMCommandResponse> {
209 * @param streamExtensionToken Token to use to invalidate an existing RTSP live stream.
211 public SDMStopCameraRtspStreamRequest(String streamExtensionToken) {
212 super("sdm.devices.commands.CameraLiveStream.StopRtspStream",
213 entry("streamExtensionToken", streamExtensionToken));
217 // Fan trait commands
220 * Change the fan timer.
222 public static class SDMSetFanTimerRequest extends SDMCommandRequest<SDMCommandResponse> {
223 public SDMSetFanTimerRequest(SDMFanTimerMode timerMode) {
224 super("sdm.devices.commands.Fan.SetTimer", entry("timerMode", timerMode.name()));
228 * @param duration Specifies the length of time in seconds that the timer is set to run.
229 * Range: "1s" to "43200s"
232 public SDMSetFanTimerRequest(SDMFanTimerMode timerMode, Duration duration) {
233 super("sdm.devices.commands.Fan.SetTimer", entry("timerMode", timerMode.name()),
234 entry("duration", String.valueOf(duration.toSeconds()) + "s"));
238 // ThermostatEco trait commands
241 * Change the thermostat Eco mode.
243 * To change the thermostat mode to HEAT, COOL, or HEATCOOL, use the {@link SDMSetThermostatModeRequest}.
246 * This command impacts other traits, based on the current status of, or changes to, the Eco mode:
248 * <li>If Eco mode is OFF, the thermostat mode will default to the last standard mode (HEAT, COOL, HEATCOOL, or OFF)
249 * that was active.</li>
250 * <li>If Eco mode is MANUAL_ECO:
252 * <li>Commands for the ThermostatTemperatureSetpoint trait are rejected.</li>
253 * <li>Temperature setpoints are not returned by the ThermostatTemperatureSetpoint trait.</li>
258 * Some thermostat models do not support changing the Eco mode when the thermostat mode is OFF, according to the
259 * ThermostatMode trait. The thermostat mode must be changed to HEAT, COOL, or HEATCOOL prior to changing the Eco
262 public static class SDMSetThermostatEcoModeRequest extends SDMCommandRequest<SDMCommandResponse> {
263 public SDMSetThermostatEcoModeRequest(SDMThermostatEcoMode mode) {
264 super("sdm.devices.commands.ThermostatEco.SetMode", entry("mode", mode.name()));
268 // ThermostatMode trait commands
271 * Change the thermostat mode.
273 public static class SDMSetThermostatModeRequest extends SDMCommandRequest<SDMCommandResponse> {
274 public SDMSetThermostatModeRequest(SDMThermostatMode mode) {
275 super("sdm.devices.commands.ThermostatMode.SetMode", entry("mode", mode.name()));
279 // ThermostatTemperatureSetpoint trait commands
282 * Sets the target temperature when the thermostat is in COOL mode.
284 public static class SDMSetThermostatCoolSetpointRequest extends SDMCommandRequest<SDMCommandResponse> {
286 * @param temperature the target temperature in degrees Celsius
288 public SDMSetThermostatCoolSetpointRequest(BigDecimal temperature) {
289 super("sdm.devices.commands.ThermostatTemperatureSetpoint.SetCool", entry("coolCelsius", temperature));
294 * Sets the target temperature when the thermostat is in HEAT mode.
296 public static class SDMSetThermostatHeatSetpointRequest extends SDMCommandRequest<SDMCommandResponse> {
298 * @param temperature the target temperature in degrees Celsius
300 public SDMSetThermostatHeatSetpointRequest(BigDecimal temperature) {
301 super("sdm.devices.commands.ThermostatTemperatureSetpoint.SetHeat", entry("heatCelsius", temperature));
306 * Sets the minimum and maximum temperatures when the thermostat is in HEATCOOL mode.
308 public static class SDMSetThermostatRangeSetpointRequest extends SDMCommandRequest<SDMCommandResponse> {
310 * @param minTemperature the minimum target temperature in degrees Celsius
311 * @param maxTemperature the maximum target temperature in degrees Celsius
313 public SDMSetThermostatRangeSetpointRequest(BigDecimal minTemperature, BigDecimal maxTemperature) {
314 super("sdm.devices.commands.ThermostatTemperatureSetpoint.SetRange", entry("heatCelsius", minTemperature),
315 entry("coolCelsius", maxTemperature));