eed02d26ea67be02a85245d2f8cf15dd036c3284
[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 def buttonEvent(evt){
75         def buttonNumber = evt.data
76         def value = evt.value
77     log.debug "buttonEvent: $evt.name = $evt.value ($evt.data)"
78         log.debug "button: $buttonNumber, value: $value"
79     
80     def recentEvents = buttonDevice.eventsSince(new Date(now() - 2000)).findAll{it.value == evt.value && it.data == evt.data}
81     log.debug "Found ${recentEvents.size()?:0} events in past 2 seconds"
82     
83     if(recentEvents.size <= 1){
84         handleButton(extractButtonNumber(buttonNumber), value)
85     } else {
86         log.debug "Found recent button press events for $buttonNumber with value $value"
87     }
88 }
89
90 def extractButtonNumber(data) {
91         def buttonNumber
92     //TODO must be a better way to do this. Data is like {buttonNumber:1}
93     switch(data) {
94         case ~/.*1.*/:
95             buttonNumber = 1
96             break
97         case ~/.*2.*/:
98             buttonNumber = 2
99             break
100         case ~/.*3.*/:
101             buttonNumber = 3
102             break
103         case ~/.*4.*/:
104             buttonNumber = 4
105             break
106     }
107     return buttonNumber
108 }
109
110 def handleButton(buttonNumber, value) {
111         switch([number: buttonNumber, value: value]) {
112         case{it.number == 1 && it.value == 'pushed'}:
113             log.debug "Button 1 pushed - Play/Pause"
114             togglePlayPause()
115             break
116         case{it.number == 2 && it.value == 'pushed'}:
117             log.debug "Button 2 pushed - Volume Up"
118             adjustVolume(true, false)
119             break
120         case{it.number == 3 && it.value == 'pushed'}:
121             log.debug "Button 3 pushed - Next Track"
122             sonos.nextTrack()
123             break
124         case{it.number == 4 && it.value == 'pushed'}:
125             log.debug "Button 4 pushed - Volume Down"
126                         adjustVolume(false, false)
127             break
128         case{it.number == 2 && it.value == 'held'}:
129             log.debug "Button 2 held - Volume Up 2x"
130             adjustVolume(true, true)
131             break
132         case{it.number == 3 && it.value == 'held'}:
133                 log.debug "Button 3 held - Previous Track"
134             sonos.previousTrack()
135                 break  
136         case{it.number == 4 && it.value == 'held'}:
137             log.debug "Button 4 held - Volume Down 2x"
138             adjustVolume(false, true)
139             break
140         default:
141             log.debug "Unhandled command: $buttonNumber $value"
142            
143     }
144 }
145
146 def togglePlayPause() {
147         def currentStatus = sonos.currentValue("status")
148     if (currentStatus == "playing") {
149         options ? sonos.pause(options) : sonos.pause()
150     }
151     else {
152         options ? sonos.play(options) : sonos.play()
153     }
154 }
155
156 def adjustVolume(boolean up, boolean doubleAmount) {
157         def changeAmount = (volumeOffset ?: 5) * (doubleAmount ? 2 : 1)
158     def currentVolume = sonos.currentValue("level")
159     
160     if(up) {
161         sonos.setLevel(currentVolume + changeAmount)
162     } else {
163             sonos.setLevel(currentVolume - changeAmount)
164     }
165 }