1 //Create a class for switch device
3 import SmartThing.SmartThing
5 public class Switch 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 currentSwitch = new StringBuilder()
12 // Maps from features to values
13 HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
14 // Possible values for eventsSince method
15 List<StringBuilder> possibleValues = new ArrayList<StringBuilder>();
17 Switch(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentSwitch) {
18 deviceValuesMap = deviceValueSmartThing
20 labelSmartThing = label
21 displayNameSmartThing = displayName
22 sendEventSmartThings = sendEvent
23 possibleValuesSmartThings = possibleValues
28 this.displayName = displayName
29 this.currentSwitch = currentSwitch
30 possibleValues.add("on")
31 possibleValues.add("off")
33 deviceValuesMap.put("switch", currentSwitch)
36 // Methods to set values
38 action(currentSwitch, "on", "switch")
41 def on(LinkedHashMap metaData) {
46 action(currentSwitch, "off", "switch")
49 def off(LinkedHashMap metaData) {
53 def action(StringBuilder variable, String newValue, String feature) {
54 if (!variable.toString().equals(newValue)) {
55 String tmpID = id.toString()
56 variable.replace(0, variable.length(), newValue)
57 println("$feature of the light with id:$tmpID is changed to $newValue!")
58 sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
59 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
63 // Methods to return values
64 def getCurrentSwitch() {
65 return currentSwitch.toString()