SONOS: Controlling the LED’s as you want, when you want :-)

During night hours, it could be great just to say: “When the sun goes down, turn off the LED’s on my Sonos devices”, and when the sun rises, turn them on again 🙂

From the Sonos APP, you can do it manually, but thats not we want!

Luckily I found this scripts from Simon Dettling (https://msitproblog.com/2016/01/04/hacking-sonos-using-windows-powershell/), where he made a Sonos Controller from Powershell. I just wanted a one-command-file and not a menu option for controlling music aso. but just for saying “LED OUT” or “LED-ON”.

So I used wireshark, and turned the LED on and on from my Sonos APP, and I found this:

<s:Envelope xmlns:s=”http://schemas.xmlsoap.org/soap/envelope/” s:encodingStyle=”http://schemas.xmlsoap.org/soap/encoding/”><s:Body><u:SetLEDState xmlns:u=”urn:schemas-upnp-org:service:DeviceProperties:1″><DesiredLEDState>Off</DesiredLEDState></u:SetLEDState></s:Body></s:Envelope>

So I took the script, and modified all the parts except the SOAP-creating-sending process, and it gave me this:

####################################################################################################################
#
#  Name: PSSonosControlerPreview.ps1
#  Author: @SimonDettling (www.msitproblog.com)
#  Version: 0.1
#  Disclaimer: This Script is not extensively tested and is a Preview of what's coming. Use at your own risk!
#
#  Modified by Martin Frederiksen - martinsblog.dk 6/2-2018 for just controlling LED
#
# Original script: https://gallery.technet.microsoft.com/SONOS-PowerShell-500c9878
###################################################################################################################

# Enter the IP Adress of your Sonos Component - remember to give them a static ip throgh dhcp server!
$sonosIP = "192.168.1.239"

# Port that is used for communication (Default = 1400)
$port = 1400

    # Assign values from Hash Table - change Off to on and make another script from a copy of this one.
    $uri = "http://${sonosIP}:$port$("/DeviceProperties/Control")"
    $soapAction = "urn:schemas-upnp-org:service:DeviceProperties:1#SetLEDState"
    $soapMessage = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetLEDState xmlns:u="urn:schemas-upnp-org:service:DeviceProperties:1"><DesiredLEDState>Off</DesiredLEDState></u:SetLEDState></s:Body></s:Envelope>'

       # Create SOAP Request
    $soapRequest = [System.Net.WebRequest]::Create($uri)

    # Set Headers
    $soapRequest.Accept = 'gzip'
    $soapRequest.Method = 'POST'
    $soapRequest.ContentType = 'text/xml; charset="utf-8"'
    $soapRequest.KeepAlive = $false
    $soapRequest.Headers.Add("SOAPACTION", $soapAction)

    # Sending SOAP Request
    $requestStream = $soapRequest.GetRequestStream()
    $soapMessage = [xml] $soapMessage
    $soapMessage.Save($requestStream)
    $requestStream.Close()

    # Sending Complete, Get Response
    $response = $soapRequest.GetResponse()
    $responseStream = $response.GetResponseStream()
    $soapReader = [System.IO.StreamReader]($responseStream)
    $returnXml = [Xml] $soapReader.ReadToEnd() 
    $responseStream.Close()


 

So just alter the IP in the scripts below, make sure your Sonos MAC are locked in DHCP server so they always get the same IP.

Add it to a scheduled task in your Home Automation Controller or just windows, and you are good to go 🙂

I’m obviously not a programmer, as this could be remade with just one script file and use UPNP to find the SONOS devices without static IP, and then just, by it’s name, tell it to be controlled.

You can also download them here:

SONOS-LED-CONTROL.ZIP

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close