Update WindowOrDoorOpen.groovy
[smartapps.git] / official / sonos-remote-control.groovy
1 /**
2  *  Copyright 2015 SmartThings
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  *  in compliance with the License. You may obtain a copy of the License at:
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11  *  for the specific language governing permissions and limitations under the License.
12  *
13  *  Sonos Remote Control
14  *
15  *  Author: Matt Nohr
16  *  Date: 2014-04-14
17  */
18  
19 /**
20  * Buttons:
21  * 1  2
22  * 3  4
23  *
24  * Pushed:
25  * 1: Play/Pause
26  * 2: Volume Up
27  * 3: Next Track
28  * 4: Volume Down
29  *
30  * Held:
31  * 1:
32  * 2: Volume Up (2x)
33  * 3: Previous Track
34  * 4: Volume Down (2x)
35  */
36
37 definition(
38     name: "Sonos Remote Control",
39     namespace: "smartthings",
40     author: "SmartThings",
41     description: "Control your Sonos system with an Aeon Minimote",
42     category: "SmartThings Internal",
43     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
44     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png"
45 )
46
47 preferences {
48         section("Select your devices") {
49                 input "buttonDevice", "capability.button", title: "Minimote", multiple: false, required: true
50         input "sonos", "capability.musicPlayer", title: "Sonos", multiple: false, required: true
51         }
52     section("Options") {
53         input "volumeOffset", "number", title: "Adjust Volume by this amount", required: false, description: "optional - 5% default"
54     }
55 }
56
57 def installed() {
58         log.debug "Installed with settings: ${settings}"
59
60         initialize()
61 }
62
63 def updated() {
64         log.debug "Updated with settings: ${settings}"
65
66         unsubscribe()
67         initialize()
68 }
69
70 def initialize() {
71         subscribe(buttonDevice, "button", buttonEvent)
72 }
73
74
75 def buttonEvent(evt){
76         def buttonNumber = evt.data
77         def value = evt.value
78     log.debug "buttonEvent: $evt.name = $evt.value ($evt.data)"
79         log.debug "button: $buttonNumber, value: $value"
80     
81     def recentEvents = buttonDevice.eventsSince(new Date(now() - 2000)).findAll{it.value == evt.value && it.data == evt.data}
82     log.debug "Found ${recentEvents.size()?:0} events in past 2 seconds"
83     
84     if(recentEvents.size <= 1){
85         handleButton(extractButtonNumber(buttonNumber), value)
86     } else {
87         log.debug "Found recent button press events for $buttonNumber with value $value"
88     }
89 }
90
91 def extractButtonNumber(data) {
92         def buttonNumber
93     //TODO must be a better way to do this. Data is like {buttonNumber:1}
94     switch(data) {
95         case ~/.*1.*/:
96             buttonNumber = 1
97             break
98         case ~/.*2.*/:
99             buttonNumber = 2
100             break
101         case ~/.*3.*/:
102             buttonNumber = 3
103             break
104         case ~/.*4.*/:
105             buttonNumber = 4
106             break
107     }
108     return buttonNumber
109 }
110
111 def handleButton(buttonNumber, value) {
112         switch([number: buttonNumber, value: value]) {
113         case{it.number == 1 && it.value == 'pushed'}:
114             log.debug "Button 1 pushed - Play/Pause"
115             togglePlayPause()
116             break
117         case{it.number == 2 && it.value == 'pushed'}:
118             log.debug "Button 2 pushed - Volume Up"
119             adjustVolume(true, false)
120             break
121         case{it.number == 3 && it.value == 'pushed'}:
122             log.debug "Button 3 pushed - Next Track"
123             sonos.nextTrack()
124             break
125         case{it.number == 4 && it.value == 'pushed'}:
126             log.debug "Button 4 pushed - Volume Down"
127                         adjustVolume(false, false)
128             break
129         case{it.number == 2 && it.value == 'held'}:
130             log.debug "Button 2 held - Volume Up 2x"
131             adjustVolume(true, true)
132             break
133         case{it.number == 3 && it.value == 'held'}:
134                 log.debug "Button 3 held - Previous Track"
135             sonos.previousTrack()
136                 break  
137         case{it.number == 4 && it.value == 'held'}:
138             log.debug "Button 4 held - Volume Down 2x"
139             adjustVolume(false, true)
140             break
141         default:
142             log.debug "Unhandled command: $buttonNumber $value"
143            
144     }
145 }
146
147 def togglePlayPause() {
148         def currentStatus = sonos.currentValue("status")
149     if (currentStatus == "playing") {
150         options ? sonos.pause(options) : sonos.pause()
151     }
152     else {
153         options ? sonos.play(options) : sonos.play()
154     }
155 }
156
157 def adjustVolume(boolean up, boolean doubleAmount) {
158         def changeAmount = (volumeOffset ?: 5) * (doubleAmount ? 2 : 1)
159     def currentVolume = sonos.currentValue("level")
160     
161     if(up) {
162         sonos.setLevel(currentVolume + changeAmount)
163     } else {
164             sonos.setLevel(currentVolume - changeAmount)
165     }
166 }