1 //Create a class for thermostat device
3 import Timer.SimulatedTimer
5 public class Thermostat {
8 private String displayName
9 private int temperature
10 private int currentTemperature
11 private int currentCoolingSetpoint
12 private int currentHeatingSetpoint
13 private int coolingSetpoint
14 private int latestCoolingSetPoint
15 private int thermostatSetpoint
16 private int latestThermostatSetPoint
17 private int heatingSetpoint
18 private int latestHeatingSetPoint
19 private List coolingSetpointRange
20 private List thermostatSetpointRange
21 private List heatingSetpointRange
22 private List supportedThermostatFanModes
23 private List supportedThermostatModes
24 private String thermostatOperatingState
25 private String thermostatFanMode
26 private String thermostatMode
27 private String currentThermostatMode
28 private String climateName
29 private String thermostatLatestMode
30 private String thermostatLatestOperatingState
31 private String thermostatLatestFanMode
36 Thermostat(Closure sendEvent, String id, String label, String displayName, int temperature, int currentCoolingSetpoint, int currentHeatingSetpoint, int coolingSetpoint,
37 int thermostatSetpoint, int heatingSetpoint, List coolingSetpointRange, List thermostatSetpointRange, List heatingSetpointRange,
38 List supportedThermostatFanModes, List supportedThermostatModes, String thermostatOperatingState, String thermostatFanMode, String thermostatMode,
39 String climateName, String thermostatLatestMode, String thermostatLatestOperatingState, String thermostatLatestFanMode, int latestCoolingSetPoint,
40 int latestThermostatSetPoint, int latestHeatingSetPoint) {
43 this.sendEvent = sendEvent
44 this.temperature = temperature
45 this.currentTemperature = currentTemperature
46 this.currentCoolingSetpoint = currentCoolingSetpoint
47 this.currentHeatingSetpoint = currentHeatingSetpoint
48 this.coolingSetpoint = coolingSetpoint
49 this.thermostatSetpoint = thermostatSetpoint
50 this.heatingSetpoint = heatingSetpoint
51 this.coolingSetpointRange = coolingSetpointRange
52 this.thermostatSetpointRange = thermostatSetpointRange
53 this.heatingSetpointRange = heatingSetpointRange
54 this.supportedThermostatFanModes = supportedThermostatFanModes
55 this.supportedThermostatModes = supportedThermostatModes
56 this.thermostatOperatingState = thermostatOperatingState
57 this.thermostatFanMode = thermostatFanMode
58 this.thermostatMode = thermostatMode
59 this.currentThermostatMode = thermostatMode
60 this.climateName = climateName
61 this.thermostatLatestMode = thermostatLatestMode
62 this.thermostatLatestOperatingState = thermostatLatestOperatingState
63 this.thermostatLatestFanMode = thermostatLatestFanMode
64 this.latestHeatingSetPoint = latestHeatingSetPoint
65 this.latestThermostatSetPoint = latestThermostatSetPoint
66 this.latestCoolingSetPoint = latestCoolingSetPoint
71 def setCoolingSetpoint(int coolingSetpoint) {
72 if (this.coolingSetpoint != coolingSetpoint) {
73 this.latestCoolingSetPoint = coolingSetpoint
74 this.coolingSetpoint = coolingSetpoint
75 this.currentCoolingSetpoint = coolingSetpoint
76 println("Cooling set point for the thermostat with id:$id is changed to $coolingSetpoint!")
77 sendEvent([name: "coolingSetpoint", value: "$coolingSetpoint", deviceId: this.id, descriptionText: "",
78 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
82 def setCoolingSetpoint(String coolingSetpoint) {
83 setCoolingSetpoint(coolingSetpoint.toInteger())
86 def setHeatingSetpoint(int heatingSetpoint) {
87 if (this.heatingSetpoint != heatingSetpoint) {
88 this.latestHeatingSetPoint = heatingSetpoint
89 this.heatingSetpoint = heatingSetpoint
90 this.currentHeatingSetpoint = heatingSetpoint
91 println("Heating set point for the thermostat with id:$id is changed to $heatingSetpoint!")
92 sendEvent([name: "heatingSetpoint", value: "$heatingSetpoint", deviceId: this.id, descriptionText: "",
93 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
97 def setHeatingSetpoint(String heatingSetpoint) {
98 setHeatingSetpoint(heatingSetpoint.toInteger())
102 //Not implemented yet
105 def setThermostatFanMode(String thermostatFanMode) {
106 if (this.thermostatFanMode != thermostatFanMode) {
107 this.thermostatLatestFanMode = thermostatFanMode
108 this.thermostatFanMode = thermostatFanMode
109 println("Fan mode of the thermostat with id:$id is changed to $thermostatFanMode!")
110 sendEvent([name: "thermostatFanMode", value: "$thermostatFanMode", deviceId: this.id, descriptionText: "",
111 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
115 def setThermostatMode(String thermostatMode) {
116 if (this.thermostatMode != thermostatMode) {
117 this.thermostatLatestMode =thermostatMode
118 this.thermostatMode = thermostatMode
119 this.currentThermostatMode = thermostatMode
120 println("Mode of the thermostat with id:$id is changed to $thermostatMode!")
121 sendEvent([name: "thermostatMode", value: "$thermostatMode", deviceId: this.id, descriptionText: "",
122 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
127 if (this.thermostatMode != "cool") {
128 this.thermostatLatestMode = "cool"
129 this.thermostatMode = "cool"
130 this.currentThermostatMode = "cool"
131 println("Mode of the thermostat with id:$id is changed to cool!")
132 sendEvent([name: "thermostatMode", value: "cool", deviceId: this.id, descriptionText: "",
133 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
138 if (this.thermostatMode != "heat") {
139 this.thermostatLatestMode = "heat"
140 this.thermostatMode = "heat"
141 this.currentThermostatMode = "heat"
142 println("Mode of the thermostat with id:$id is changed to heat!")
143 sendEvent([name: "thermostatMode", value: "heat", deviceId: this.id, descriptionText: "",
144 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
149 if (this.thermostatMode != "auto") {
150 this.thermostatLatestMode = "auto"
151 this.thermostatMode = "auto"
152 this.currentThermostatMode = "auto"
153 println("Mode of the thermostat with id:$id is changed to auto!")
154 sendEvent([name: "thermostatMode", value: "auto", deviceId: this.id, descriptionText: "",
155 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
159 def emergencyHeat() {
160 if (this.thermostatMode != "emergencyHeat") {
161 this.thermostatLatestMode = "emergencyHeat"
162 this.thermostatMode = "emergencyHeat"
163 this.currentThermostatMode = "emergencyHeat"
164 println("Mode of the thermostat with id:$id is changed to emergencyHeat!")
165 sendEvent([name: "thermostatMode", value: "emergencyHeat", deviceId: this.id, descriptionText: "",
166 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
171 if (this.thermostatMode != "off") {
172 this.thermostatLatestMode = "off"
173 this.thermostatMode = "off"
174 this.currentThermostatMode = "off"
175 println("Mode of the thermostat with id:$id is changed to off!")
176 sendEvent([name: "thermostatMode", value: "off", deviceId: this.id, descriptionText: "",
177 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
181 def setClimate(String info, String givenClimateName) {
182 if (this.climateName != givenClimateName) {
183 this.climateName = givenClimateName
184 println("Climate name of the thermostat with id:$id is changed to $givenClimateName!")
188 def setHold(String info1, int coolingSetpoint, int heatingSetpoint, String info2, String info3) {
189 if ((this.heatingSetpoint != heatingSetpoint) || (this.coolingSetpoint != coolingSetpoint)) {
190 this.coolingSetpoint = coolingSetpoint
191 this.currentCoolingSetpoint = currentCoolingSetpoint
192 println("Cooling set point for the thermostat with id:$id is changed to $coolingSetpoint!")
193 this.heatingSetpoint = heatingSetpoint
194 this.currentHeatingSetpoint = currentHeatingSetpoint
195 println("Heating set point for the thermostat with id:$id is changed to $heatingSetpoint!")
196 sendEvent([name: "heatingSetpoint", value: "$heatingSetpoint", deviceId: this.id, descriptionText: "",
197 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
198 sendEvent([name: "coolingSetpoint", value: "$coolingSetpoint", deviceId: this.id, descriptionText: "",
199 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
204 def setValue(String value, String name) {
205 if ((name == "temperature") && (value.toInteger() != this.temperature)) {
206 println("the temperature is $value!")
207 this.temperature = value.toInteger()
208 this.currentTemperature = value.toInteger()
209 } else if ((name == "heatingSetpoint") && (value.toInteger() != this.heatingSetpoint)) {
210 println("the heating set point of the thermostat with id:$id is $value!")
211 this.latestHeatingSetPoint = value.toInteger()
212 this.heatingSetpoint = value.toInteger()
213 } else if ((name == "coolingSetpoint") && (value.toInteger() != this.coolingSetpoint)) {
214 println("the cooling set point of the thermostat with id:$id is $value!")
215 this.latestCoolingSetPoint = value.toInteger()
216 this.coolingSetpoint = value.toInteger()
217 } else if ((name == "thermostatSetpoint") && (value.toInteger() != this.thermostatSetpoint)) {
218 println("the set point of the thermostat with id:$id is $value!")
219 this.latestThermostatSetPoint = value.toInteger()
220 this.thermostatSetpoint = value.toInteger()
221 } else if ((name == "thermostatMode") && (value != this.thermostatMode)) {
222 println("the mode of the thermostat with id:$id is $value!")
223 this.thermostatLatestMode = value
224 this.thermostatMode = value
225 } else if ((name == "thermostatFanMode") && (value != this.thermostatFanMode)) {
226 println("the fan mode of the thermostat with id:$id is $value!")
227 this.thermostatLatestFanMode = value
228 this.thermostatFanMode = value
229 } else if ((name == "thermostatOperatingState") && (value != this.thermostatOperatingState)) {
230 println("the operating state of the thermostat with id:$id is $value!")
231 this.thermostatLatestOperatingState = value
232 this.thermostatOperatingState = value
236 def currentValue(String deviceFeature) {
237 if (deviceFeature == "thermostatMode")
238 return thermostatMode
239 else if (deviceFeature == "thermostatOperatingState")
240 return thermostatOperatingState
241 else if (deviceFeature == "thermostatFanMode")
242 return thermostatFanMode
243 else if (deviceFeature == "heatingSetpoint")
244 return heatingSetpoint
245 else if (deviceFeature == "coolingSetpoint")
246 return coolingSetpoint
247 else if (deviceFeature == "thermostatSetpoint")
248 return thermostatSetpoint
251 def latestValue(String deviceFeature) {
252 if (deviceFeature == "thermostatMode")
253 return thermostatLatestMode
254 else if (deviceFeature == "thermostatOperatingState")
255 return thermostatLatestOperatingState
256 else if (deviceFeature == "thermostatFanMode")
257 return thermostatLatestFanMode
258 else if (deviceFeature == "heatingSetpoint")
259 return latestHeatingSetPoint
260 else if (deviceFeature == "coolingSetpoint")
261 return latestCoolingSetPoint
262 else if (deviceFeature == "thermostatSetpoint")
263 return latestThermostatSetPoint