General structure - SonosCollaboration/SonosProtocol GitHub Wiki
Sonos use the standard UPnP protocol, but the commands that can be used are not documented. This page describes the general anatomy of a Sonos command in details and ignoring UPnP. To send a command to a Sonos device we need to send it a HTTP request. In the Sonos command there are 3 concepts that we will use: The action, the endpoint and the body.
The endpoint is a part of the URL that we will be sending the command to. An example of a complete URL could look like this:
http://192.168.0.110:1400/MediaRenderer/AVTransport/Control
It consist of the IP of the controller, the port to be used (1400) and the endpoint, which is everything after the port number. There are at least 4 different endpoints and these will accept different commands. The endpoints are:
Name | Endpoint |
---|---|
Transport Endpoint | /MediaRenderer/AVTransport/Control |
Rendering Endpoint | /MediaRenderer/RenderingControl/Control |
Device Endpoint | /DeviceProperties/Control |
Content Directory Endpoint | /MediaServer/ContentDirectory/Control |
Each of the function specific articles will contain the endpoint used for that function.
The action is one of the HTTP headers. All the headers that need to be sent alongside with a command are:
Key | Value |
---|---|
Content-Type | text/xml |
SOAPACTION | action |
where action
is a text string that corresponds to the command. An example action could be:
"urn:schemas-upnp-org:service:AVTransport:1#Play"
where the "" are a part of the value.
Each of the function specific articles will contain the actions used for the individual function.
The body is the main part of the command, it is the content of the HTTP request. The Sonos (UPnP) bodies are XML and could look like this:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:Play xmlns:u="urn:schemas-upnp-org:service:AVTransport:1">
<InstanceID>0</InstanceID>
<Speed>1</Speed>
</u:Play>
</s:Body>
</s:Envelope>
There is an Envelope
and a Body
tag, which is common to all Sonos commands. Inside the Body
tags are the tags for the actual command, which in this case is a play command.
Example bodies for the requests will be listed in each of the function specific articles.
Below are the only code examples to be found on this wiki. They are generally avoided in order to keep this documentation project programming language and project independent, but are listed here to get people started with an implementation. The code example are listed alphabetically by programming language name.
""" This file send a play command to a Sonos device """
import requests
IP = '192.168.0.112'
ACTION = '"urn:schemas-upnp-org:service:AVTransport:1#Play"'
ENDPOINT = '/MediaRenderer/AVTransport/Control'
BODY = '''
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:Play xmlns:u="urn:schemas-upnp-org:service:AVTransport:1">
<InstanceID>0</InstanceID>
<Speed>1</Speed>
</u:Play>
</s:Body>
</s:Envelope>
'''
HEADERS = {
'Content-Type': 'text/xml',
'SOAPACTION': ACTION
}
URL = 'http://{ip}:1400{endpoint}'.format(ip=IP, endpoint=ENDPOINT)
REQ = requests.post(URL, data=BODY, headers=HEADERS)
print 'Response:'
print REQ.content
This example is also available in the repository as play.py.