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.ecobee.internal.function;
15 import java.util.Date;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.ecobee.internal.dto.thermostat.EventDTO;
20 import org.openhab.binding.ecobee.internal.enums.HoldType;
23 * The set hold function sets the thermostat into a hold with the specified temperature.
24 * Creates a hold for the specified duration. Note that an event is created regardless
25 * of whether the program is in the same state as the requested state. There is also
26 * support for creating a hold by passing a holdClimateRef request parameter/value pair
27 * to this function (See Event). When an existing and valid Thermostat.Climate#climateRef
28 * value is passed to this function, the coolHoldTemp, heatHoldTemp and fan mode from
29 * that Thermostat.Climate are used in the creation of the hold event. The values from
30 * that Climate will take precedence over any coolHoldTemp, heatHoldTemp, and fan
31 * mode parameters passed into this function separately. To resume from a hold and
32 * return to the program, use the ResumeProgramFunction.
34 * @author John Cocula - Initial contribution
35 * @author Mark Hilbush - Adapt for OH2/3
38 public final class SetHoldFunction extends AbstractFunction {
40 public SetHoldFunction(@Nullable EventDTO event, @Nullable HoldType holdType, @Nullable Integer holdHours,
41 @Nullable Date startDateTime, @Nullable Date endDateTime) {
45 throw new IllegalArgumentException("event must not be null");
47 if (holdType == HoldType.HOLD_HOURS && holdHours == null) {
48 throw new IllegalArgumentException("holdHours must be specified when holdType='holdHours'");
49 } else if (holdType == HoldType.DATE_TIME && endDateTime == null) {
50 throw new IllegalArgumentException("endDateTime must be specified when holdType='dateTime'");
52 if (event.holdClimateRef == null) {
53 if (Boolean.TRUE.equals(event.isTemperatureAbsolute) && Boolean.TRUE.equals(event.isTemperatureRelative)) {
54 throw new IllegalArgumentException("cannot set both absolute and relative temperatures");
56 if (Boolean.TRUE.equals(event.isTemperatureAbsolute)
57 && (event.coolHoldTemp == null || event.heatHoldTemp == null)) {
58 throw new IllegalArgumentException(
59 "coolHoldTemp and heatHoldTemp must be specified when 'isTemperatureAbsolute' is true");
61 if (Boolean.TRUE.equals(event.isTemperatureRelative)
62 && (event.coolRelativeTemp == null || event.heatRelativeTemp == null)) {
63 throw new IllegalArgumentException(
64 "coolRelativeTemp and heatRelativeTemp must be specified when 'isTemperatureRelative' is true");
67 // Make parameters from the input event
68 if (event.isOccupied != null) {
69 params.put("isOccupied", event.isOccupied);
71 if (event.isCoolOff != null) {
72 params.put("isCoolOff", event.isCoolOff);
74 if (event.isHeatOff != null) {
75 params.put("isHeatOff", event.isHeatOff);
77 if (event.coolHoldTemp != null) {
78 params.put("coolHoldTemp", event.coolHoldTemp);
80 if (event.heatHoldTemp != null) {
81 params.put("heatHoldTemp", event.heatHoldTemp);
83 if (event.fan != null) {
84 params.put("fan", event.fan);
86 if (event.vent != null) {
87 params.put("vent", event.vent);
89 if (event.ventilatorMinOnTime != null) {
90 params.put("ventilatorMinOnTime", event.ventilatorMinOnTime);
92 if (event.isOptional != null) {
93 params.put("isOptional", event.isOptional);
95 if (event.isTemperatureRelative != null) {
96 params.put("isTemperatureRelative", event.isTemperatureRelative);
98 if (event.coolRelativeTemp != null) {
99 params.put("coolRelativeTemp", event.coolRelativeTemp);
101 if (event.heatRelativeTemp != null) {
102 params.put("heatRelativeTemp", event.heatRelativeTemp);
104 if (event.isTemperatureAbsolute != null) {
105 params.put("isTemperatureAbsolute", event.isTemperatureAbsolute);
107 if (event.fanMinOnTime != null) {
108 params.put("fanMinOnTime", event.fanMinOnTime);
110 if (event.holdClimateRef != null) {
111 params.put("holdClimateRef", event.holdClimateRef);
114 // Make parameters from the holdType and hold options
115 if (holdType != null) {
116 params.put("holdType", holdType);
118 if (holdHours != null) {
119 params.put("holdHours", holdHours);
121 if (startDateTime != null) {
122 params.put("startDate", YMD.format(startDateTime));
123 params.put("startTime", HMS.format(startDateTime));
125 if (endDateTime != null) {
126 params.put("endDate", YMD.format(endDateTime));
127 params.put("endTime", HMS.format(endDateTime));