1 //Create a class for thermostat device
3 import SmartThing.SmartThing
5 class Thermostat extends SmartThing {
6 // id, label, and display name of the device
7 StringBuilder id = new StringBuilder()
8 StringBuilder label = new StringBuilder()
9 StringBuilder displayName = new StringBuilder()
10 // Features with numberical values
11 MutableInteger currentTemperature = new MutableInteger()
12 MutableInteger currentCoolingSetpoint = new MutableInteger()
13 MutableInteger currentHeatingSetpoint = new MutableInteger()
14 MutableInteger currentThermostatSetPoint = new MutableInteger()
15 // Features with string values
16 StringBuilder currentThermostatOperatingState = new StringBuilder()
17 StringBuilder currentThermostatFanMode = new StringBuilder()
18 StringBuilder currentThermostatMode = new StringBuilder()
19 StringBuilder currentClimateName = new StringBuilder()
20 // Maps from features to values
21 HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
22 HashMap<String, MutableInteger> deviceIntValuesMap = new HashMap<String, MutableInteger>()
24 Thermostat(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, MutableInteger currentTemperature, MutableInteger currentCoolingSetpoint,
25 MutableInteger currentHeatingSetpoint, MutableInteger currentThermostatSetPoint, StringBuilder currentThermostatOperatingState, StringBuilder currentThermostatFanMode,
26 StringBuilder currentThermostatMode, StringBuilder currentClimateName) {
27 deviceValuesMap = deviceValueSmartThing
28 deviceIntValuesMap = deviceIntValueSmartThing
30 labelSmartThing = label
31 displayNameSmartThing = displayName
32 sendEventSmartThings = sendEvent
37 this.displayName = displayName
38 this.currentTemperature = currentTemperature
39 this.currentCoolingSetpoint = currentCoolingSetpoint
40 this.currentHeatingSetpoint = currentHeatingSetpoint
41 this.currentThermostatSetPoint = currentThermostatSetPoint
42 this.currentThermostatOperatingState = currentThermostatOperatingState
43 this.currentThermostatFanMode = currentThermostatFanMode
44 this.currentThermostatMode = currentThermostatMode
45 this.currentClimateName = currentClimateName
47 deviceValuesMap.put("temperature", currentTemperature)
48 deviceValuesMap.put("thermostatOperatingState", currentThermostatOperatingState)
49 deviceValuesMap.put("thermostatFanMode", currentThermostatFanMode)
50 deviceValuesMap.put("thermostatMode", currentThermostatMode)
51 deviceIntValuesMap.put("coolingSetpoint", currentCoolingSetpoint)
52 deviceIntValuesMap.put("heatingSetpoint", currentHeatingSetpoint)
53 deviceIntValuesMap.put("thermostatSetpoint", currentThermostatSetPoint)
56 // Methods to set values
57 def setCoolingSetpoint(int newValue) {
58 action(this.currentCoolingSetpoint, newValue, "coolingSetpoint")
61 def setCoolingSetpoint(String newValue) {
62 setCoolingSetpoint(newValue.toInteger())
65 def setHeatingSetpoint(int newValue) {
66 action(this.currentHeatingSetpoint, newValue, "heatingSetpoint")
69 def setHeatingSetpoint(String newValue) {
70 setHeatingSetpoint(newValue.toInteger())
73 def setThermostatFanMode(String newValue) {
74 action(this.currentThermostatFanMode, newValue, "thermostatFanMode")
77 def setThermostatMode(String newValue) {
78 action(this.currentThermostatMode, newValue, "thermostatMode")
82 action(this.currentThermostatMode, "cool", "thermostatMode")
86 action(this.currentThermostatMode, "heat", "thermostatMode")
90 action(this.currentThermostatMode, "auto", "thermostatMode")
94 action(this.currentThermostatMode, "emergencyHeat", "thermostatMode")
98 action(this.currentThermostatMode, "off", "thermostatMode")
101 def setClimate(String info, String newValue) {
102 action(currentClimateName, newValue, "climateName")
105 def setHold(String info1, int coolingSetpoint, int heatingSetpoint, String info2, String info3) {
106 setHeatingSetpoint(heatingSetpoint)
107 setCoolingSetpoint(coolingSetpoint)
110 def action(StringBuilder variable, String newValue, String feature) {
111 if (!variable.toString().equals(newValue)) {
112 String tmpID = id.toString()
113 variable.replace(0, variable.length(), newValue)
114 println("$feature of the thermostat with id:$tmpID is changed to $newValue!")
115 sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
116 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
120 def action(MutableInteger variable, int newValue, String feature) {
121 if (!variable.getValue().equals(newValue)) {
122 String tmpID = id.toString()
123 variable.setValue(newValue)
124 println("$feature for the thermostat with id:$tmpID is changed to $newValue!")
125 sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
126 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
130 // Methods to return values
131 def getCurrentTemperature() {
132 return currentTemperature.getValue()
135 def getCurrentCoolingSetpoint() {
136 return currentCoolingSetpoint.getValue()
139 def getCurrentHeatingSetpoint() {
140 return currentHeatingSetpoint.getValue()
143 def getCurrentThermostatSetPoint() {
144 return currentThermostatSetPoint.getValue()
147 def getCurrentThermostatOperatingState() {
148 return currentThermostatOperatingState.toString()
151 def getCurrentThermostatFanMode() {
152 return currentThermostatFanMode.toString()
155 def getCurrentThermostatMode() {
156 return currentThermostatMode.toString()
159 def getCurrentClimateName() {
160 return currentClimateName.toString()