Update step-notifier.groovy
[smartapps.git] / third-party / VirtualButtons.groovy
1 /**
2  *      Virtual Buttons for Multi-Button Controllers (ex. Minimote or ZWN-SC7)
3  *
4  *      Author: obycode
5  *      Date Created: 2015-05-13
6  *
7  */
8 definition(
9     name: "Virtual Buttons",
10     namespace: "com.obycode",
11     author: "obycode",
12     description: "Create virtual single button devices for each button of your multi-button device (ex. Minimote or ZWN-SC7)",
13     category: "Convenience",
14     iconUrl: "http://cdn.device-icons.smartthings.com/unknown/zwave/remote-controller.png",
15     iconX2Url: "http://cdn.device-icons.smartthings.com/unknown/zwave/remote-controller@2x.png"
16 )
17
18 preferences {
19   section ("Select your button controller: ") {
20     input "buttonDevice", "capability.button", title: "Which?", multiple: false, required: true
21   }
22 }
23
24 def installed() {
25   initialize()
26 }
27
28 def updated() {
29 }
30
31 def initialize() {
32   def numButtons = buttonDevice.currentValue("numButtons").toInteger()
33   log.info "Creating $numButtons virtual buttons"
34   // Create the virtual buttons
35   (1..numButtons).each {
36     def d = addChildDevice("com.obycode", "Virtual Button", buttonDevice.id + ":" + it.toString(), null, [label:buttonDevice.displayName + " " + it.toString(), name:"Virtual Button", completedSetup: true])
37   }
38
39   // Subscribe to the button events
40   subscribe(buttonDevice, "button", buttonEvent)
41 }
42
43 def uninstalled() {
44   unsubscribe()
45   def delete = getChildDevices()
46   delete.each {
47     deleteChildDevice(it.deviceNetworkId)
48   }
49 }
50
51 def buttonEvent(evt) {
52   log.debug "buttonEvent: $evt.name $evt.value ($evt.data)"
53   def buttonNumber = evt.jsonData.buttonNumber
54
55   def buttonId = buttonDevice.id + ":" + buttonNumber
56   def children = getChildDevices()
57   def childButton = children.find{ d -> d.deviceNetworkId == buttonId }
58
59   switch (evt.value) {
60     case "pushed":
61       log.debug "pushing the virtual button"
62       childButton.push()
63       break
64     case "held":
65       log.debug "holding the virtual button"
66       childButton.hold()
67       break
68     case "default":
69       log.debug "releasing the virtual button"
70       childButton.release()
71       break
72     default:
73       log.debug "Unknown event: $evt.value"
74   }
75 }