Infrastructure now works for: locks, alarms, thermostats, and non-hue lights.
[smartthings-infrastructure.git] / SwitchLevel / SwitchLevel.groovy
index a09a4505799233ef00b8f0c0108442526bcf9170..24bd7bfa0f3b03aba3b026067cdfaa1171641297 100644 (file)
@@ -6,13 +6,16 @@ public class SwitchLevel {
        private String id
        private String label
        private String displayName
+       private String switchState
+       private String currentSwitch
        private int level
        private int rate
+       private String switchLatestValue
        def sendEvent   
        def timers
        
 
-       SwitchLevel(Closure sendEvent, String id, String label, String displayName, int level) {
+       SwitchLevel(Closure sendEvent, String id, String label, String displayName, int level, String switchState, String switchLatestValue) {
                this.sendEvent = sendEvent
                this.timers = new SimulatedTimer()
                this.id = id
@@ -20,6 +23,9 @@ public class SwitchLevel {
                this.displayName = displayName
                this.level = level
                this.rate = level
+               this.switchState = switchState
+               this.currentSwitch = switchState
+               this.switchLatestValue = switchLatestValue
        }
 
        //By Apps
@@ -28,15 +34,95 @@ public class SwitchLevel {
                        println("the switch with id:$id is setted to level $level!")
                        this.level = level
                        this.rate = level
-                       sendEvent([name: "level", value: "50", deviceId: this.id, descriptionText: "",
+                       sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
                }
        }
 
+       def on() {
+               if (this.switchState != "on") {
+                       println("the switch with id:$id is on!")
+                       this.switchLatestValue = "on"
+                       this.switchState = "on"
+                       this.currentSwitch = "on"
+                       sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
+                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
+                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
+       }
+
+       def on(LinkedHashMap metaData) {
+               if (this.switchState != "on") {
+                       def task = timers.runAfter(metaData["delay"]) {
+                               println("the switch with id:$id is on!")
+                               this.switchLatestValue = "on"
+                               this.switchState = "on"
+                               this.currentSwitch = "on"
+                               sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
+                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                               sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
+                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       }
+               }
+       }
+
+       def off() {
+               if (this.switchState != "off") {
+                       println("the switch with id:$id is off!")
+                       this.switchLatestValue = "off"
+                       this.switchState = "off"
+                       this.currentSwitch = "off"
+                       sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
+                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
+                           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 = "off"
+                               this.switchState = "off"
+                               this.currentSwitch = "off"
+                               sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
+                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                               sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
+                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       }
+               }
+       }
+
        //By Model Checker
-       def setValue(String value) {
-               println("the switch with id:$id is setted to level $value!")
-               this.level = value.toInteger()
-               this.rate = value.toInteger()
+       def setValue(String value, String name) {
+               if (name == "switch") {
+                       println("the switch with id:$id is $value!")
+                       this.switchLatestValue = value
+                       this.switchState = value
+                       this.currentSwitch = value
+               } else if (name == "level") {
+                       println("the switch with id:$id is setted to level $value!")
+                       this.level = value.toInteger()
+                       this.rate = value.toInteger()
+               }
+       }
+
+
+       def currentValue(String deviceFeature) {
+               if (deviceFeature == "level") {
+                       return level
+               } else if (deviceFeature == "switch") {
+                       return switchState
+               }
+       }
+
+       def latestValue(String deviceFeature) {
+               if (deviceFeature == "level") {
+                       return level
+               } else if (deviceFeature == "switch") {
+                       return switchState
+               }
        }
 }