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.
20 private songOptions() {
22 // Make sure current selection is in the set
24 def options = new LinkedHashSet()
25 options << "STOP PLAYING"
26 if (state.selectedSong?.station) {
27 options << state.selectedSong.station
29 else if (state.selectedSong?.description) {
30 // TODO - Remove eventually? 'description' for backward compatibility
31 options << state.selectedSong.description
34 // Query for recent tracks
35 def states = sonos.statesSince("trackData", new Date(0), [max:30])
36 def dataMaps = states.collect{it.jsonValue}
37 options.addAll(dataMaps.collect{it.station})
39 log.trace "${options.size()} songs in list"
40 options.take(20) as List
43 private saveSelectedSongs() {
45 def songs = sonos.statesSince("trackData", new Date(0), [max:30]).collect{it.jsonValue}
46 log.info "Searching ${songs.size()} records"
48 if (!state.selectedSongs) {
49 state.selectedSongs = [:]
52 settings.each {name, thisSong ->
53 if (thisSong == "STOP PLAYING") {
54 state.selectedSongs."$name" = "PAUSE"
56 if (name.startsWith("mode_")) {
57 log.info "Looking for $thisSong"
59 def data = songs.find {s -> s.station == thisSong}
60 log.info "Found ${data?.station}"
62 state.selectedSongs."$name" = data
63 log.debug "Selected song = $data.station"
65 else if (song == state.selectedSongs."$name"?.station) {
66 log.debug "Selected existing entry '$thisSong', which is no longer in the last 20 list"
69 log.warn "Selected song '$thisSong' not found"
80 name: "Sonos Music Modes",
81 namespace: "smartthings",
82 author: "SmartThings",
83 description: "Plays a different selected song or station for each mode.",
84 category: "SmartThings Internal",
85 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Partner/sonos.png",
86 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Partner/sonos@2x.png"
90 page(name: "mainPage", title: "Play a message on your Sonos when something happens", nextPage: "chooseTrack", uninstall: true)
91 page(name: "chooseTrack", title: "Select a song", install: true)
92 page(name: "timeIntervalInput", title: "Only during a certain time") {
94 input "starting", "time", title: "Starting", required: false
95 input "ending", "time", title: "Ending", required: false
101 dynamicPage(name: "mainPage") {
104 input "sonos", "capability.musicPlayer", title: "Sonos player", required: true
106 section("More options", hideable: true, hidden: true) {
107 input "volume", "number", title: "Set the volume", description: "0-100%", required: false
108 input "frequency", "decimal", title: "Minimum time between actions (defaults to every event)", description: "Minutes", required: false
109 href "timeIntervalInput", title: "Only during a certain time", description: timeLabel ?: "Tap to set", state: timeLabel ? "complete" : "incomplete"
110 input "days", "enum", title: "Only on certain days of the week", multiple: true, required: false,
111 options: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
112 if (settings.modes) {
113 input "modes", "mode", title: "Only when mode is", multiple: true, required: false
115 input "oncePerDay", "bool", title: "Only once per day", required: false, defaultValue: false
121 dynamicPage(name: "chooseTrack") {
122 section("Play a different song for each mode in which you want music") {
123 def options = songOptions()
124 location.modes.each {mode ->
125 input "mode_$mode.name", "enum", title: mode.name, options: options, required: false
128 section([mobileOnly:true]) {
129 label title: "Assign a name", required: false
130 mode title: "Set for specific mode(s)", required: false
136 log.debug "Installed with settings: ${settings}"
141 log.debug "Updated with settings: ${settings}"
146 def subscribeToEvents() {
147 log.trace "subscribeToEvents()"
150 subscribe(location, modeChangeHandler)
153 def modeChangeHandler(evt) {
154 log.trace "modeChangeHandler($evt.name: $evt.value)"
157 def lastTime = state[frequencyKey(evt)]
158 if (lastTime == null || now() - lastTime >= frequency * 60000) {
168 private takeAction(evt) {
170 def name = "mode_$evt.value".toString()
171 def selectedSong = state.selectedSongs."$name"
173 if (selectedSong == "PAUSE") {
177 log.info "Playing '$selectedSong"
179 if (volume != null) {
182 sonos.setLevel(volume)
186 sonos.playTrack(selectedSong)
189 if (frequency || oncePerDay) {
190 state[frequencyKey(evt)] = now()
192 log.trace "Exiting takeAction()"
195 private frequencyKey(evt) {
196 "lastActionTimeStamp"
199 private dayString(Date date) {
200 def df = new java.text.SimpleDateFormat("yyyy-MM-dd")
201 if (location.timeZone) {
202 df.setTimeZone(location.timeZone)
205 df.setTimeZone(TimeZone.getTimeZone("America/New_York"))
210 private oncePerDayOk(Long lastTime) {
211 def result = lastTime ? dayString(new Date()) != dayString(new Date(lastTime)) : true
212 log.trace "oncePerDayOk = $result"
216 // TODO - centralize somehow
218 modeOk && daysOk && timeOk
221 private getModeOk() {
222 def result = !modes || modes.contains(location.mode)
223 log.trace "modeOk = $result"
227 private getDaysOk() {
230 def df = new java.text.SimpleDateFormat("EEEE")
231 if (location.timeZone) {
232 df.setTimeZone(location.timeZone)
235 df.setTimeZone(TimeZone.getTimeZone("America/New_York"))
237 def day = df.format(new Date())
238 result = days.contains(day)
240 log.trace "daysOk = $result"
244 private getTimeOk() {
246 if (starting && ending) {
248 def start = timeToday(starting, location?.timeZone).time
249 def stop = timeToday(ending, location?.timeZone).time
250 result = start < stop ? currTime >= start && currTime <= stop : currTime <= stop || currTime >= start
252 log.trace "timeOk = $result"
256 private hhmm(time, fmt = "h:mm a")
258 def t = timeToday(time, location.timeZone)
259 def f = new java.text.SimpleDateFormat(fmt)
260 f.setTimeZone(location.timeZone ?: timeZone(time))
264 private timeIntervalLabel()
266 (starting && ending) ? hhmm(starting) + "-" + hhmm(ending, "h:mm a z") : ""
268 // TODO - End Centralize