2 * Copyright 2015 SmartThings
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
13 * Sonos Remote Control
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"
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
53 input "volumeOffset", "number", title: "Adjust Volume by this amount", required: false, description: "optional - 5% default"
58 log.debug "Installed with settings: ${settings}"
64 log.debug "Updated with settings: ${settings}"
71 subscribe(buttonDevice, "button", buttonEvent)
76 def buttonNumber = evt.data
78 log.debug "buttonEvent: $evt.name = $evt.value ($evt.data)"
79 log.debug "button: $buttonNumber, value: $value"
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"
84 if(recentEvents.size <= 1){
85 handleButton(extractButtonNumber(buttonNumber), value)
87 log.debug "Found recent button press events for $buttonNumber with value $value"
91 def extractButtonNumber(data) {
93 //TODO must be a better way to do this. Data is like {buttonNumber:1}
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"
117 case{it.number == 2 && it.value == 'pushed'}:
118 log.debug "Button 2 pushed - Volume Up"
119 adjustVolume(true, false)
121 case{it.number == 3 && it.value == 'pushed'}:
122 log.debug "Button 3 pushed - Next Track"
125 case{it.number == 4 && it.value == 'pushed'}:
126 log.debug "Button 4 pushed - Volume Down"
127 adjustVolume(false, false)
129 case{it.number == 2 && it.value == 'held'}:
130 log.debug "Button 2 held - Volume Up 2x"
131 adjustVolume(true, true)
133 case{it.number == 3 && it.value == 'held'}:
134 log.debug "Button 3 held - Previous Track"
135 sonos.previousTrack()
137 case{it.number == 4 && it.value == 'held'}:
138 log.debug "Button 4 held - Volume Down 2x"
139 adjustVolume(false, true)
142 log.debug "Unhandled command: $buttonNumber $value"
147 def togglePlayPause() {
148 def currentStatus = sonos.currentValue("status")
149 if (currentStatus == "playing") {
150 options ? sonos.pause(options) : sonos.pause()
153 options ? sonos.play(options) : sonos.play()
157 def adjustVolume(boolean up, boolean doubleAmount) {
158 def changeAmount = (volumeOffset ?: 5) * (doubleAmount ? 2 : 1)
159 def currentVolume = sonos.currentValue("level")
162 sonos.setLevel(currentVolume + changeAmount)
164 sonos.setLevel(currentVolume - changeAmount)