//Create a class for relay switch device package RelaySwitch import SmartThing.SmartThing public class RelaySwitch 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() RelaySwitch(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentSwitch) { deviceValuesMap = deviceValueSmartThing idSmartThing = id labelSmartThing = label displayNameSmartThing = displayName sendEventSmartThings = sendEvent // Initialization this.id = id this.label = label this.displayName = displayName this.currentSwitch = currentSwitch 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 relay switch 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() } }