1 //Create a class for thermostat device
3 import SmartThing.SmartThing
5 //Importing mutable integer class
6 import MutableInteger.MutableInteger
8 class Thermostat extends SmartThing {
9 // id, label, and display name of the device
10 StringBuilder id = new StringBuilder()
11 StringBuilder label = new StringBuilder()
12 StringBuilder displayName = new StringBuilder()
13 // Features with numberical values
14 MutableInteger currentTemperature = new MutableInteger()
15 MutableInteger currentCoolingSetpoint = new MutableInteger()
16 MutableInteger currentHeatingSetpoint = new MutableInteger()
17 MutableInteger currentThermostatSetPoint = new MutableInteger()
18 // Features with string values
19 StringBuilder currentThermostatOperatingState = new StringBuilder()
20 StringBuilder currentThermostatFanMode = new StringBuilder()
21 StringBuilder currentThermostatMode = new StringBuilder()
22 StringBuilder currentClimateName = new StringBuilder()
23 // Maps from features to values
24 HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
25 HashMap<String, MutableInteger> deviceIntValuesMap = new HashMap<String, MutableInteger>()
27 Thermostat(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, MutableInteger currentTemperature, MutableInteger currentCoolingSetpoint,
28 MutableInteger currentHeatingSetpoint, MutableInteger currentThermostatSetPoint, StringBuilder currentThermostatOperatingState, StringBuilder currentThermostatFanMode,
29 StringBuilder currentThermostatMode, StringBuilder currentClimateName) {
30 deviceValuesMap = deviceValueSmartThing
31 deviceIntValuesMap = deviceIntValueSmartThing
33 labelSmartThing = label
34 displayNameSmartThing = displayName
35 sendEventSmartThings = sendEvent
40 this.displayName = displayName
41 this.currentTemperature = currentTemperature
42 this.currentCoolingSetpoint = currentCoolingSetpoint
43 this.currentHeatingSetpoint = currentHeatingSetpoint
44 this.currentThermostatSetPoint = currentThermostatSetPoint
45 this.currentThermostatOperatingState = currentThermostatOperatingState
46 this.currentThermostatFanMode = currentThermostatFanMode
47 this.currentThermostatMode = currentThermostatMode
48 this.currentClimateName = currentClimateName
50 deviceValuesMap.put("temperature", currentTemperature)
51 deviceValuesMap.put("thermostatOperatingState", currentThermostatOperatingState)
52 deviceValuesMap.put("thermostatFanMode", currentThermostatFanMode)
53 deviceValuesMap.put("thermostatMode", currentThermostatMode)
54 deviceIntValuesMap.put("coolingSetpoint", currentCoolingSetpoint)
55 deviceIntValuesMap.put("heatingSetpoint", currentHeatingSetpoint)
56 deviceIntValuesMap.put("thermostatSetpoint", currentThermostatSetPoint)
59 // Methods to set values
60 def setCoolingSetpoint(int newValue) {
61 action(this.currentCoolingSetpoint, newValue, "coolingSetpoint")
64 def setCoolingSetpoint(String newValue) {
65 setCoolingSetpoint(newValue.toInteger())
68 def setHeatingSetpoint(int newValue) {
69 action(this.currentHeatingSetpoint, newValue, "heatingSetpoint")
72 def setHeatingSetpoint(String newValue) {
73 setHeatingSetpoint(newValue.toInteger())
76 def setThermostatFanMode(String newValue) {
77 action(this.currentThermostatFanMode, newValue, "thermostatFanMode")
80 def setThermostatMode(String newValue) {
81 action(this.currentThermostatMode, newValue, "thermostatMode")
85 action(this.currentThermostatMode, "cool", "thermostatMode")
89 action(this.currentThermostatMode, "heat", "thermostatMode")
93 action(this.currentThermostatMode, "auto", "thermostatMode")
97 action(this.currentThermostatMode, "emergencyHeat", "thermostatMode")
101 action(this.currentThermostatMode, "off", "thermostatMode")
104 def setClimate(String info, String newValue) {
105 action(currentClimateName, newValue, "climateName")
108 def setHold(String info1, int coolingSetpoint, int heatingSetpoint, String info2, String info3) {
109 setHeatingSetpoint(heatingSetpoint)
110 setCoolingSetpoint(coolingSetpoint)
113 def action(StringBuilder variable, String newValue, String feature) {
114 if (!variable.toString().equals(newValue)) {
115 String tmpID = id.toString()
116 variable.replace(0, variable.length(), newValue)
117 println("$feature of the thermostat with id:$tmpID is changed to $newValue!")
118 sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
119 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
123 def action(MutableInteger variable, int newValue, String feature) {
124 if (!variable.getValue().equals(newValue)) {
125 String tmpID = id.toString()
126 variable.setValue(newValue)
127 println("$feature for the thermostat with id:$tmpID is changed to $newValue!")
128 sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
129 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
133 // Methods to return values
134 def getCurrentTemperature() {
135 return currentTemperature.getValue()
138 def getCurrentCoolingSetpoint() {
139 return currentCoolingSetpoint.getValue()
142 def getCurrentHeatingSetpoint() {
143 return currentHeatingSetpoint.getValue()
146 def getCurrentThermostatSetPoint() {
147 return currentThermostatSetPoint.getValue()
150 def getCurrentThermostatOperatingState() {
151 return currentThermostatOperatingState.toString()
154 def getCurrentThermostatFanMode() {
155 return currentThermostatFanMode.toString()
158 def getCurrentThermostatMode() {
159 return currentThermostatMode.toString()
162 def getCurrentClimateName() {
163 return currentClimateName.toString()