1 //Create a class for valve
3 import SmartThing.SmartThing
5 public class Valve 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 string values
11 StringBuilder currentValve = new StringBuilder()
12 // Maps from features to values
13 HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
15 Valve(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentValve) {
16 deviceValuesMap = deviceValueSmartThing
18 labelSmartThing = label
19 displayNameSmartThing = displayName
20 sendEventSmartThings = sendEvent
25 this.displayName = displayName
26 this.currentValve = currentValve
28 deviceValuesMap.put("valve", currentValve)
31 // Methods to set values
33 action(currentValve, "open", "contact")
36 def open(LinkedHashMap metaData) {
41 action(currentValve, "closed", "contact")
44 def close(LinkedHashMap metaData) {
48 def action(StringBuilder variable, String newValue, String feature) {
49 if (!variable.toString().equals(newValue)) {
50 String tmpID = id.toString()
51 variable.replace(0, variable.length(), newValue)
52 println("$feature of the valve with id:$tmpID is $newValue!")
53 sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
54 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
58 // Methods to return values
59 def getCurrentValve() {
60 return currentValve.toString()