//Create a class for switch device package Switch import SmartThing.SmartThing public class Switch extends SmartThing { // id, label, and display name of the device StringBuilder id = new StringBuilder() StringBuilder label = new StringBuilder() StringBuilder displayName = new StringBuilder() // Features with string values StringBuilder currentSwitch = new StringBuilder() // Maps from features to values HashMap deviceValuesMap = new HashMap() // Possible values for eventsSince method List possibleValues = new ArrayList(); Switch(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentSwitch) { deviceValuesMap = deviceValueSmartThing idSmartThing = id labelSmartThing = label displayNameSmartThing = displayName sendEventSmartThings = sendEvent possibleValuesSmartThings = possibleValues // Initialization this.id = id this.label = label this.displayName = displayName this.currentSwitch = currentSwitch possibleValues.add("on") possibleValues.add("off") deviceValuesMap.put("switch", currentSwitch) } // Methods to set values def on() { action(currentSwitch, "on", "switch") } def on(LinkedHashMap metaData) { on() } def off() { action(currentSwitch, "off", "switch") } def off(LinkedHashMap metaData) { off() } def action(StringBuilder variable, String newValue, String feature) { if (!variable.toString().equals(newValue)) { String tmpID = id.toString() variable.replace(0, variable.length(), newValue) println("$feature of the light with id:$tmpID is changed to $newValue!") sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) } } // Methods to return values def getCurrentSwitch() { return currentSwitch.toString() } }