Bluesound Driver

I want to share Bluesound Driver. You are free to modify it. I made this Helped by @tomw . Thank you so much for your help.

/*

*/

metadata
{
    definition(name: "Bluesound Node2i", namespace: "putudhony88", author: "tomw", importUrl: "")
    {
        capability "AudioVolume"
        capability "Initialize"
        capability "MediaInputSource"
        capability "Refresh"
        capability "MusicPlayer"

        command "executeCommand", ["command"]
        command "repeat"
        command "shuffle"
        command "bluetooth"
        command "hdmi"
        command "spotify"
        command "analog"
        
        command "preset1"
        command "preset2"
        command "preset3"
        command "preset4"
        command "preset5"
        command "preset6"
        
        command "addZoneSlave", ["slaveIP"]
        command "removeZoneSlave", ["slaveIP"]
        
        attribute "trackArt", "string"
        attribute "trackAlbum", "string"
        attribute "trackArtist", "string"
        attribute "trackTitle", "string"
        
        
    }
    

}

preferences
{
    section
    {
        input "IP_address", "text", title: "IP address of Bluesound unit", required: true
        input "refreshInterval", "number", title: "Polling refresh interval in seconds", defaultValue: 60
        input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: true
    }
}

def logDebug(msg)
{
    if (logEnable)
    {
        log.debug(msg)
    }
}

def installed()
{
    sendEvent(name: "supportedInputs", value: '{"HDMI", "Bluetooth", "Spotify"}')
    initialize()
}

def initialize()
{
    refresh()
}

def refresh()
{
    unschedule(refresh)
    
    def status = httpGetExec("Status")
    
    if(status)
    {
        events = [[:]]
        events += [name: "volume", value: status.volume.toInteger()]
        events += [name: "mute", value: (status.mute.toInteger() == 1) ? "muted" : "unmuted"]
        events += [name: "status", value: status.state]
        events += [name: "trackData", value: status.streamUrl]
        events += [name: "trackDescription", value: "${status.title1} - ${status.artist} - ${status.album}"]
//      events += [name: "trackArt", value:"<img src=\"${status.currentImage}\">"]
        events += [name: "trackArt", value: status.image]
        events += [name: "trackArtist", value: status.artist]
        events += [name: "trackAlbum", value: status.album]
        events += [name: "trackTitle", value: status.title1]

        
        switch(status.inputId)
        {
            case "input4":
                events += [name: "MediaInputSource", value: "HDMI"]
                break
            case "Spotify":
                events += [name: "MediaInputSource", value: "Spotify"]
                break
            case "input2":
                events += [name: "MediaInputSource", value: "Bluetooth"]
                break
            case "input0":
                events += [name: "MediaInputSource", value: "Analog"]
                break
        }
        
        
        events.each
        {
            sendEvent(it)
        }        
    }
       
    runIn(refreshInterval ?: 60, refresh)
}

def updated()
{
    installed()
}

def uninstalled()
{
    unschedule()
}

def play()
{
    executeCommand("Play")
}

def pause()
{
    executeCommand("Pause")
}

def stop()
{
    executeCommand("Stop")
}

def nextTrack()
{
    executeCommand("Skip")
}

def previousTrack()
{
    executeCommand("Back")
}

def mute()
{
    executeCommand("Volume?mute=1")
}

def unmute()
{
    executeCommand("Volume?mute=0")
}

def setVolume(volumelevel)
{
    logDebug("setting volume to: $volumelevel")
    
    vol = volumelevel.toInteger()    
    vol = (vol < 0) ? 0 : (vol > 100) ? 100 : vol
    
    executeCommand("Volume?level=${vol.toString()}&tell_slaves=off")
}

def volumeDown()
{
    executeCommand("Volume?db=-1")
}

def volumeUp()
{
    executeCommand("Volume?db=1")
}

def playTrack(trackuri)
{
    executeCommand("Play:${trackuri}")
}

def restoreTrack(trackuri)
{
    playTrack(trackuri)
}

def resumeTrack(trackuri)
{
    playTrack(trackuri)
}

def repeat()
{
    executeCommand("Repeat?state=1")  
}

def shuffle()
{
    executeCommand("Shuffle?state=1")  
}

def bluetooth()
{
    executeCommand("Play?url=Capture%3Abluez%3Abluetooth")  
}

def analog()
{
    executeCommand("Play?inputType=spdif&index=1")  
}


def hdmi()
{
    executeCommand("Play?url=Capture%3Ahw%3A2%2C0%2F1%2F25%2F2%2Finput4")  
}

def spotify()
{
    executeCommand("Play?url=Spotify%3Aplay")  
}

def addZoneSlave(slaveIP)
{
    executeCommand("AddSlave?slave=${slaveIP.toString()}&port=11000") 
}

def removeZoneSlave(slaveIP)
{
    executeCommand("RemoveSlave?slave=${slaveIP.toString()}&port=11000") 
}

def setInputSource(inputName)
{
    switch(inputName)
    {
        case "HDMI":
            hdmi ()
            break
        case "Spotify":
            spotify()
            break
        case "Bluetooth":
            bluetooth()
            break
    }
    sendEvent(name: "mediaInputSource", value: status.service)
}

def preset1()
{
    executeCommand("Preset?id=1")
}

def preset2()
{
    executeCommand("Preset?id=2")
}

def preset3()
{
    executeCommand("Preset?id=3")
}

def preset4()
{
    executeCommand("Preset?id=4")
}

def preset5()
{
    executeCommand("Preset?id=5")
}

def preset6()
{
    executeCommand("Preset?id=6")
}

def scaleBluesoundToPercent(volumeLevel)
{
    def percVol = ((volumeLevel.toInteger() + 90) * 100/10).toInteger()
    percVol = (percVol >= 0) ? ((percVol <=100) ? percVol : 100) : 0
    return percVol
}

def scalePercentToBluesound(volumeLevel)
{
    def neetsVol = (volumeLevel.toInteger() * 10/100).toInteger() - 90
    neetsVol = (neetsVol >= -90) ? ((neetsVol <= 0) ? neetsVol : 0) : -90
    return neetsVol
}

def executeCommand(command, doRefresh = true)
{
    res = httpGetExec(command)
    
    if((null != res) && doRefresh)
    {
        runInMillis(100, refresh)
    }
    
    return res
}

def getBaseURI()
{
    return "http://" + IP_address + ":11000/"
}

def httpGetExec(suffix)
{
    logDebug("httpGetExec(${suffix})")

    try
    {
	    def result
        def getString = getBaseURI() + suffix
        httpGet(getString.replaceAll(' ', '%20'))
        { resp ->
            if (resp.data)
            {
                logDebug("resp.data = ${resp.data}")
                result = resp.data
            }
        }		
	        return result
    }
    catch (Exception e)
    {
        logDebug("httpGetExec() failed: ${e.message}")
    }

}

1 Like