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 <a href="https://developers.google.com/nest/device-access/reference/rest/v1/enterprises.devices/executeCommand">
34 * https://developers.google.com/nest/device-access/reference/rest/v1/enterprises.devices/executeCommand</a>
36 public class SDMCommands {
39 * Command request parent.
41 public abstract static class SDMCommandRequest<T extends SDMCommandResponse> {
42 private final String command;
43 private final Map<String, Object> params = new LinkedHashMap<>();
46 private SDMCommandRequest(String command, Entry<String, Object>... params) {
47 this.command = command;
48 for (Entry<String, Object> param : params) {
49 this.params.put(param.getKey(), param.getValue());
53 public String getCommand() {
57 public Map<String, Object> getParams() {
61 @SuppressWarnings("unchecked")
62 public Class<T> getResponseClass() {
63 return (Class<T>) SDMCommandResponse.class;
68 * Command response parent. This class is also used for responses without additional data.
70 public static class SDMCommandResponse {
73 // CameraEventImage trait commands
76 * Generates a download URL for the image related to a camera event.
78 public static class SDMGenerateCameraImageRequest extends SDMCommandRequest<SDMGenerateCameraImageResponse> {
81 * Event images expire 30 seconds after the event is published. Make sure to download the image prior to
84 public static final Duration EVENT_IMAGE_VALIDITY = Duration.ofSeconds(30);
87 * @param eventId ID of the camera event to request a related image for.
89 public SDMGenerateCameraImageRequest(String eventId) {
90 super("sdm.devices.commands.CameraEventImage.GenerateImage", entry("eventId", eventId));
94 public Class<SDMGenerateCameraImageResponse> getResponseClass() {
95 return SDMGenerateCameraImageResponse.class;
99 public static class SDMGenerateCameraImageResults {
101 * The URL to download the camera image from.
106 * Token to use in the HTTP Authorization header when downloading the camera image.
111 public static class SDMGenerateCameraImageResponse extends SDMCommandResponse {
112 public SDMGenerateCameraImageResults results;
115 // CameraLiveStream trait commands
118 * Request a token to access a camera RTSP live stream URL.
120 public static class SDMGenerateCameraRtspStreamRequest
121 extends SDMCommandRequest<SDMGenerateCameraRtspStreamResponse> {
122 public SDMGenerateCameraRtspStreamRequest() {
123 super("sdm.devices.commands.CameraLiveStream.GenerateRtspStream");
127 public Class<SDMGenerateCameraRtspStreamResponse> getResponseClass() {
128 return SDMGenerateCameraRtspStreamResponse.class;
133 * Camera RTSP live stream URLs.
135 public static class SDMCameraRtspStreamUrls {
136 public String rtspUrl;
139 public static class SDMGenerateCameraRtspStreamResults {
141 * Camera RTSP live stream URLs.
143 public SDMCameraRtspStreamUrls streamUrls;
146 * Token to use to extend the {@link #streamToken} for an RTSP live stream.
148 public String streamExtensionToken;
151 * Token to use to access an RTSP live stream.
153 public String streamToken;
156 * Time at which both {@link #streamExtensionToken} and {@link #streamToken} expire.
158 public ZonedDateTime expiresAt;
161 public static class SDMGenerateCameraRtspStreamResponse extends SDMCommandResponse {
162 public SDMGenerateCameraRtspStreamResults results;
166 * Request a new RTSP live stream URL access token to replace a valid RTSP access token before it expires. This is
167 * also used to replace a valid RTSP token from a previous ExtendRtspStream command request.
169 public static class SDMExtendCameraRtspStreamRequest extends SDMCommandRequest<SDMExtendCameraRtspStreamResponse> {
171 * @param streamExtensionToken Token to use to request an extension to the RTSP streaming token.
173 public SDMExtendCameraRtspStreamRequest(String streamExtensionToken) {
174 super("sdm.devices.commands.CameraLiveStream.ExtendRtspStream",
175 entry("streamExtensionToken", streamExtensionToken));
179 public Class<SDMExtendCameraRtspStreamResponse> getResponseClass() {
180 return SDMExtendCameraRtspStreamResponse.class;
184 public static class SDMExtendCameraRtspStreamResults {
186 * Token to use to view an existing RTSP live stream and to request an extension to the streaming token.
188 public String streamExtensionToken;
191 * New token to use to access an existing RTSP live stream.
193 public String streamToken;
196 * Time at which both {@link #streamExtensionToken} and {@link #streamToken} expire.
198 public ZonedDateTime expiresAt;
201 public static class SDMExtendCameraRtspStreamResponse extends SDMCommandResponse {
202 public SDMExtendCameraRtspStreamResults results;
206 * Invalidates a valid RTSP access token and stops the RTSP live stream tied to that access token.
208 public static class SDMStopCameraRtspStreamRequest extends SDMCommandRequest<SDMCommandResponse> {
210 * @param streamExtensionToken Token to use to invalidate an existing RTSP live stream.
212 public SDMStopCameraRtspStreamRequest(String streamExtensionToken) {
213 super("sdm.devices.commands.CameraLiveStream.StopRtspStream",
214 entry("streamExtensionToken", streamExtensionToken));
218 // Fan trait commands
221 * Change the fan timer.
223 public static class SDMSetFanTimerRequest extends SDMCommandRequest<SDMCommandResponse> {
224 public SDMSetFanTimerRequest(SDMFanTimerMode timerMode) {
225 super("sdm.devices.commands.Fan.SetTimer", entry("timerMode", timerMode.name()));
229 * @param duration Specifies the length of time in seconds that the timer is set to run.
230 * Range: "1s" to "43200s"
233 public SDMSetFanTimerRequest(SDMFanTimerMode timerMode, Duration duration) {
234 super("sdm.devices.commands.Fan.SetTimer", entry("timerMode", timerMode.name()),
235 entry("duration", duration.toSeconds() + "s"));
239 // ThermostatEco trait commands
242 * Change the thermostat Eco mode.
244 * To change the thermostat mode to HEAT, COOL, or HEATCOOL, use the {@link SDMSetThermostatModeRequest}.
247 * This command impacts other traits, based on the current status of, or changes to, the Eco mode:
249 * <li>If Eco mode is OFF, the thermostat mode will default to the last standard mode (HEAT, COOL, HEATCOOL, or OFF)
250 * that was active.</li>
251 * <li>If Eco mode is MANUAL_ECO:
253 * <li>Commands for the ThermostatTemperatureSetpoint trait are rejected.</li>
254 * <li>Temperature setpoints are not returned by the ThermostatTemperatureSetpoint trait.</li>
259 * Some thermostat models do not support changing the Eco mode when the thermostat mode is OFF, according to the
260 * ThermostatMode trait. The thermostat mode must be changed to HEAT, COOL, or HEATCOOL prior to changing the Eco
263 public static class SDMSetThermostatEcoModeRequest extends SDMCommandRequest<SDMCommandResponse> {
264 public SDMSetThermostatEcoModeRequest(SDMThermostatEcoMode mode) {
265 super("sdm.devices.commands.ThermostatEco.SetMode", entry("mode", mode.name()));
269 // ThermostatMode trait commands
272 * Change the thermostat mode.
274 public static class SDMSetThermostatModeRequest extends SDMCommandRequest<SDMCommandResponse> {
275 public SDMSetThermostatModeRequest(SDMThermostatMode mode) {
276 super("sdm.devices.commands.ThermostatMode.SetMode", entry("mode", mode.name()));
280 // ThermostatTemperatureSetpoint trait commands
283 * Sets the target temperature when the thermostat is in COOL mode.
285 public static class SDMSetThermostatCoolSetpointRequest extends SDMCommandRequest<SDMCommandResponse> {
287 * @param temperature the target temperature in degrees Celsius
289 public SDMSetThermostatCoolSetpointRequest(BigDecimal temperature) {
290 super("sdm.devices.commands.ThermostatTemperatureSetpoint.SetCool", entry("coolCelsius", temperature));
295 * Sets the target temperature when the thermostat is in HEAT mode.
297 public static class SDMSetThermostatHeatSetpointRequest extends SDMCommandRequest<SDMCommandResponse> {
299 * @param temperature the target temperature in degrees Celsius
301 public SDMSetThermostatHeatSetpointRequest(BigDecimal temperature) {
302 super("sdm.devices.commands.ThermostatTemperatureSetpoint.SetHeat", entry("heatCelsius", temperature));
307 * Sets the minimum and maximum temperatures when the thermostat is in HEATCOOL mode.
309 public static class SDMSetThermostatRangeSetpointRequest extends SDMCommandRequest<SDMCommandResponse> {
311 * @param minTemperature the minimum target temperature in degrees Celsius
312 * @param maxTemperature the maximum target temperature in degrees Celsius
314 public SDMSetThermostatRangeSetpointRequest(BigDecimal minTemperature, BigDecimal maxTemperature) {
315 super("sdm.devices.commands.ThermostatTemperatureSetpoint.SetRange", entry("heatCelsius", minTemperature),
316 entry("coolCelsius", maxTemperature));