Last active
August 29, 2015 14:04
-
-
Save pyro2927/3a0cdeffdc60ef1d030b to your computer and use it in GitHub Desktop.
SmartThings Spot Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Spot | |
* | |
* SmartThings device type attempting to integrate with https://github.com/minton/Spot | |
* | |
* Copyright 2014 Joseph Pintozzi | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | |
* in compliance with the License. You may obtain a copy of the License at: | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed | |
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License | |
* for the specific language governing permissions and limitations under the License. | |
* | |
*/ | |
preferences { | |
input("ipaddress", "text", title: "IP Address", description: "192.168.1.1", require: true) | |
input("port", "text", title: "Port", description: "5051", require: true) | |
} | |
metadata { | |
definition (name: "Spot", namespace: "pyro2927", author: "Joseph Pintozzi") { | |
capability "Polling" | |
capability "Music Player" | |
} | |
simulator { | |
// TODO: define status and reply messages here | |
} | |
tiles { | |
standardTile("back", "device.back", inactiveLabel: false, decoration: "flat") { | |
state "default", action:"Music Player.previousTrack", icon:"st.sonos.previous-btn" | |
} | |
standardTile("state", "device.state", inactiveLabel: false, decoration: "flat") { | |
state "paused", action:"Music Player.play", icon: "st.sonos.play-btn" | |
state "playing", action:"Music Player.pause", icon: "st.sonos.pause-btn" | |
} | |
standardTile("next", "device.next", inactiveLabel: false, decoration: "flat") { | |
state "default", action:"Music Player.nextTrack", icon:"st.sonos.next-btn" | |
} | |
valueTile("volume", "device.volume", inactiveLabel: false, decoration: "flat") { | |
state "default", label:'Volume at ${currentValue}%', backgroundColor:"#ffffff" | |
} | |
standardTile("refresh", "device.power", inactiveLabel: false, decoration: "flat") { | |
state "default", action:"polling.poll", icon:"st.secondary.refresh" | |
} | |
//volume slider | |
controlTile("levelSliderControl", "device.level", "slider", height: 1, width: 3, inactiveLabel: false) { | |
state "level", action:"Music Player.setLevel" | |
} | |
main "state" | |
details(["back", "state", "next", "levelSliderControl", "volume", "refresh"]) | |
} | |
} | |
// parse events into attributes | |
def parse(String description) { | |
log.debug "Parsing '${description}'" | |
// TODO: handle 'status' attribute | |
// TODO: handle 'level' attribute | |
// TODO: handle 'trackDescription' attribute | |
// TODO: handle 'trackData' attribute | |
// TODO: handle 'mute' attribute | |
} | |
// handle commands | |
def poll() { | |
log.debug "Executing 'poll'" | |
try { | |
api('getVolume', [], { resp -> | |
log.debug "Volume fetched" | |
log.debug resp.data | |
sendEvent(name: 'volume', value: resp.data) | |
}) | |
} catch (Exception e){ | |
log.debug "Exception: " + e | |
} | |
} | |
def play() { | |
log.debug "Executing 'play'" | |
api('play', [], { | |
sendEvent(name: 'state', value: 'playing') | |
}) | |
} | |
def pause() { | |
log.debug "Executing 'pause'" | |
api('pause', [], { | |
sendEvent(name: 'state', value: 'paused') | |
}) | |
} | |
def stop() { | |
log.debug "Executing 'stop'" | |
pause() | |
} | |
def nextTrack() { | |
log.debug "Executing 'nextTrack'" | |
api('next', [], { | |
}) | |
} | |
def playTrack() { | |
log.debug "Executing 'playTrack'" | |
// TODO: handle 'playTrack' command | |
} | |
def setLevel(percent) { | |
log.debug "Executing 'setLevel' to ${percent}" | |
api('setVolume', ['volume': '${percent}'], { | |
poll() | |
}) | |
} | |
def playText() { | |
log.debug "Executing 'playText'" | |
// TODO: handle 'playText' command | |
} | |
def mute() { | |
log.debug "Executing 'mute'" | |
api('mute', [], { | |
sendEvent(name: 'state', value: 'paused') | |
}) | |
} | |
def previousTrack() { | |
log.debug "Executing 'previousTrack'" | |
// TODO: handle 'previousTrack' command | |
api('back', [], { | |
}) | |
} | |
def unmute() { | |
log.debug "Executing 'unmute'" | |
// TODO: handle 'unmute' command | |
setLevel(50) | |
} | |
def setTrack() { | |
log.debug "Executing 'setTrack'" | |
// TODO: handle 'setTrack' command | |
} | |
def resumeTrack() { | |
log.debug "Executing 'resumeTrack'" | |
// TODO: handle 'resumeTrack' command | |
} | |
def restoreTrack() { | |
log.debug "Executing 'restoreTrack'" | |
// TODO: handle 'restoreTrack' command | |
} | |
// Methods stolen/modified from https://github.com/Dianoga | |
def api(method, args = [], success = {}) { | |
def methods = [ | |
'playing': [uri: "/playing", type: 'get'], | |
'play': [uri: "/play", type: 'put'], | |
'pause': [uri: "/pause", type: 'put'], | |
'next': [uri: "/next", type: 'put'], | |
'back': [uri: "/back", type: 'put'], | |
'mute': [uri: "/mute", type: 'put'], | |
'getVolume': [uri: "/volume", type: 'get'], | |
'setVolume': [uri: "/volume", type: 'put'] | |
] | |
def request = methods.getAt(method) | |
doRequest(request.uri, args, request.type, success) | |
} | |
def doRequest(uri, args, type, success) { | |
log.debug "Calling $type : $uri : $args" | |
def params = [ | |
uri: "http://${settings.ipaddress}:${settings.port}${uri}", | |
headers: [ | |
'Accept': "application/json" | |
], | |
body: args | |
] | |
if(type == 'post') { | |
httpPostJson(params, success) | |
} else if (type == 'get') { | |
httpGet(params, success) | |
} else if (type == 'put') { | |
httpPutJson(params, success) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment