Changes in classes: new concept for latest value + all types of events generated...
[smartthings-infrastructure.git] / Thermostat / Thermostat.groovy
1 //Create a class for thermostat device
2 package Thermostat
3 import Timer.SimulatedTimer
4
5 public class Thermostat {
6         private String id
7         private String label
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
32         def sendEvent
33         def timers
34
35
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) {
41                 this.id = id
42                 this.label = label
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
67         }
68
69
70         //By Apps
71         def setCoolingSetpoint(int coolingSetpoint) {
72                 if (this.coolingSetpoint != coolingSetpoint) {
73                         this.latestCoolingSetPoint = coolingSetpoint
74                         this.coolingSetpoint = coolingSetpoint
75                         this.currentCoolingSetpoint = currentCoolingSetpoint
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"}'])
79                 }
80         }
81
82         def setCoolingSetpoint(String coolingSetpoint) {
83                 setCoolingSetpoint(coolingSetpoint.toInteger())
84         }
85
86         def setHeatingSetpoint(int heatingSetpoint) {
87                 if (this.heatingSetpoint != heatingSetpoint) {
88                         this.latestHeatingSetPoint = heatingSetpoint
89                         this.heatingSetpoint = heatingSetpoint
90                         this.currentHeatingSetpoint = currentHeatingSetpoint
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"}'])
94                 }
95         }
96
97         def setHeatingSetpoint(String heatingSetpoint) {
98                 setHeatingSetpoint(heatingSetpoint.toInteger())
99         }
100
101         def setSchedule() {
102                 //Not implemented yet
103         }
104
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"}'])
112                 }
113         }
114
115         def setThermostatMode(String thermostatMode) {
116                 if (this.thermostatMode != thermostatMode) {
117                         this.thermostatLatestMode =thermostatMode
118                         this.thermostatMode = thermostatMode
119                         this.currentThermostatMode = currentThermostatMode
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"}'])
123                 }
124         }
125
126         def cool() {
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: "cool", value: "cool", deviceId: this.id, descriptionText: "",
133                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
134                 }
135         }
136
137         def heat() {
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: "heat", value: "heat", deviceId: this.id, descriptionText: "",
144                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
145                 }
146         }
147
148         def auto() {
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: "auto", value: "auto", deviceId: this.id, descriptionText: "",
155                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
156                 }
157         }
158
159         def off() {
160                 if (this.thermostatMode != "off") {
161                         this.thermostatLatestMode = "off"
162                         this.thermostatMode = "off"
163                         this.currentThermostatMode = "off"
164                         println("Mode of the thermostat with id:$id is changed to off!")
165                         sendEvent([name: "off", value: "off", deviceId: this.id, descriptionText: "",
166                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
167                 }
168         }
169
170         def setClimate(String info, String givenClimateName) {
171                 if (this.climateName != givenClimateName) {
172                         this.climateName = givenClimateName
173                         println("Climate name of the thermostat with id:$id is changed to $givenClimateName!")
174                 }
175         }
176
177         def setHold(String info1, int coolingSetpoint, int heatingSetpoint, String info2, String info3) {
178                 if ((this.heatingSetpoint != heatingSetpoint) || (this.coolingSetpoint != coolingSetpoint)) {
179                         this.coolingSetpoint = coolingSetpoint
180                         this.currentCoolingSetpoint = currentCoolingSetpoint
181                         println("Cooling set point for the thermostat with id:$id is changed to $coolingSetpoint!")
182                         this.heatingSetpoint = heatingSetpoint
183                         this.currentHeatingSetpoint = currentHeatingSetpoint
184                         println("Heating set point for the thermostat with id:$id is changed to $heatingSetpoint!")
185                         sendEvent([name: "heatingSetpoint", value: "$heatingSetpoint", deviceId: this.id, descriptionText: "",
186                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
187                         sendEvent([name: "coolingSetpoint", value: "$coolingSetpoint", deviceId: this.id, descriptionText: "",
188                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
189                 }
190         }
191
192         //By Model Checker
193         def setValue(String value, String name) {
194                 if ((name == "temperature") && (value.toInteger() != this.temperature)) {
195                         println("the temperature is $value!")
196                         this.temperature = value.toInteger()
197                         this.currentTemperature = value.toInteger()
198                 } else if ((name == "heatingSetpoint") && (value.toInteger() != this.heatingSetpoint)) {
199                         println("the heating set point of the thermostat with id:$id is $value!")
200                         this.latestHeatingSetPoint = value.toInteger()
201                         this.heatingSetpoint = value.toInteger()
202                 } else if ((name == "coolingSetpoint") && (value.toInteger() != this.coolingSetpoint)) {
203                         println("the cooling set point of the thermostat with id:$id is $value!")
204                         this.latestCoolingSetPoint = value.toInteger()
205                         this.coolingSetpoint = value.toInteger()
206                 } else if ((name == "thermostatSetpoint") && (value.toInteger() != this.thermostatSetpoint)) {
207                         println("the set point of the thermostat with id:$id is $value!")
208                         this.latestThermostatSetPoint = value.toInteger()
209                         this.thermostatSetpoint = value.toInteger()
210                 } else if ((name == "thermostatMode") && (value != this.thermostatMode)) {
211                         println("the mode of the thermostat with id:$id is $value!")
212                         this.thermostatLatestMode = value
213                         this.thermostatMode = value
214                 } else if ((name == "thermostatFanMode") && (value != this.thermostatFanMode)) {
215                         println("the fan mode of the thermostat with id:$id is $value!")
216                         this.thermostatLatestFanMode = value
217                         this.thermostatFanMode = value
218                 } else if ((name == "thermostatOperatingState") && (value != this.thermostatOperatingState)) {
219                         println("the operating state of the thermostat with id:$id is $value!")
220                         this.thermostatLatestOperatingState = value
221                         this.thermostatOperatingState = value
222                 }
223         }
224
225         def currentValue(String deviceFeature) {
226                 if (deviceFeature == "thermostatMode")
227                         return thermostatMode
228                 else if (deviceFeature == "thermostatOperatingState")
229                         return thermostatOperatingState
230                 else if (deviceFeature == "thermostatFanMode")
231                         return thermostatFanMode
232                 else if (deviceFeature == "heatingSetpoint")
233                         return heatingSetpoint
234                 else if (deviceFeature == "coolingSetpoint")
235                         return coolingSetpoint
236                 else if (deviceFeature == "thermostatSetpoint")
237                         return thermostatSetpoint
238         }
239
240         def latestValue(String deviceFeature) {
241                 if (deviceFeature == "thermostatMode")
242                         return thermostatLatestMode
243                 else if (deviceFeature == "thermostatOperatingState")
244                         return thermostatLatestOperatingState
245                 else if (deviceFeature == "thermostatFanMode")
246                         return thermostatLatestFanMode
247                 else if (deviceFeature == "heatingSetpoint")
248                         return latestHeatingSetPoint
249                 else if (deviceFeature == "coolingSetpoint")
250                         return latestCoolingSetPoint
251                 else if (deviceFeature == "thermostatSetpoint")
252                         return latestThermostatSetPoint
253         }
254
255 }