Infrastructure that works for all the locks' group!
[smartthings-infrastructure.git] / Switch / Switch.groovy
index b9c0a819df5620f31bbf40e18075e5db1d809c15..d01d27be361b5268d7b8dcc1e7a21ec44e8cd748 100644 (file)
@@ -33,42 +33,50 @@ public class Switch {
        }
 
        def on() {
-               println("the switch with id:$id is on!")
-               this.switchLatestValue = this.switchState
-               this.switchState = "on"
-               this.currentSwitch = "on"
-               sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
-                   displayed: true, linkText: "", isStateChange: false, unit: "", data: [value: "on"]])
-       }
-
-       def on(LinkedHashMap metaData) {
-               def task = timers.runAfter(metaData["delay"]) {
+               if (this.switchState != "on") {
                        println("the switch with id:$id is on!")
                        this.switchLatestValue = this.switchState
                        this.switchState = "on"
                        this.currentSwitch = "on"
                        sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
-                           displayed: true, linkText: "", isStateChange: false, unit: "", data: [value: "on"]])
+                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
                }
        }
 
-       def off() {
-               println("the switch with id:$id is off!")
-               this.switchLatestValue = this.switchState
-               this.switchState = "off"
-               this.currentSwitch = "off"
-               sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
-                   displayed: true, linkText: "", isStateChange: false, unit: "", data: [value: "off"]])
+       def on(LinkedHashMap metaData) {
+               if (this.switchState != "on") {
+                       def task = timers.runAfter(metaData["delay"]) {
+                               println("the switch with id:$id is on!")
+                               this.switchLatestValue = this.switchState
+                               this.switchState = "on"
+                               this.currentSwitch = "on"
+                               sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
+                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       }
+               }
        }
 
-       def off(LinkedHashMap metaData) {
-               def task = timers.runAfter(metaData["delay"]) {
+       def off() {
+               if (this.switchState != "off") {
                        println("the switch with id:$id is off!")
                        this.switchLatestValue = this.switchState
                        this.switchState = "off"
                        this.currentSwitch = "off"
                        sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
-                           displayed: true, linkText: "", isStateChange: false, unit: "", data: [value: "off"]])
+                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
+       }
+
+       def off(LinkedHashMap metaData) {
+               if (this.switchState != "off") {
+                       def task = timers.runAfter(metaData["delay"]) {
+                               println("the switch with id:$id is off!")
+                               this.switchLatestValue = this.switchState
+                               this.switchState = "off"
+                               this.currentSwitch = "off"
+                               sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
+                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       }
                }
        }