[_CODE_] Sonos integration!

Controlling a Sonos device from your Vera (using code from [tt]http://forum.micasaverde.com/index.php?topic=2001.msg48090#msg48090[/tt] by Gilles with contributions by mcvflorin and futzle and research from [tt]http://travelmarx.blogspot.com/2010/06/exploring-sonos-via-upnp.html[/tt]):

function SOAP_request( server_url, action, servicetype, arguments )


 function execute_request( command ) -- a function to excute a shell command

  local file = io.popen ( command )
  local data = file:read( "*a" )
  file:close()

  if (data == "")
   then
    data = nil
   end

  return data

 end -- function execute_request( command ) -- a function to excute a shell command


 local req = '<?xml version="1.0" encoding="utf-8"?>' ..
               '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' ..
                  '<s:Body>' ..
                    '<u:' .. action .. ' xmlns:u="' .. servicetype .. '">' ..
                      arguments .. 
                    '</u:' .. action .. '>' .. 
                  '</s:Body>' ..
               '</s:Envelope>'

  local file_req = io.open( "req.lua", "w" )
  file_req:write( req )
  file_req:close()

  return execute_request( 'curl -s -X POST -H "SOAPAction: ' .. servicetype .. '#' .. action .. '" -d@req.lua ' .. server_url )

 end -- function SOAP_request( server_url, action, servicetype, arguments )


 local answer = SOAP_request( 'http://192.168.178.38:1400/MediaRenderer/AVTransport/Control',
                              'GetTransportInfo'                                            ,
                              'urn:schemas-upnp-org:service:AVTransport:1'                  ,
                              '<InstanceID>0</InstanceID>'                                    )
                                  

 luup.variable_set( 'urn:upnp-ap15e-com:serviceId:WAI1', 'Location', tostring( answer ) , 36 )

IMHO it’s a shame that Vera as a device that claims to be an UPnP<->Z-Wave bridge doesn’t work as advertised ([tt]http://bugs.micasaverde.com/view.php?id=1473[/tt]). Vera even gets her own UPnP description wrong ([tt]http://bugs.micasaverde.com/view.php?id=1487[/tt])!

It could be as simple as

local answer = luup.call_action( 'urn:upnp-org:service:AVTransport', 'GetTransportInfo', { InstanceID = '0' }, device_id )

Use Whitebear Media Server ([tt]http://www.whitebear.ch/mediaserver[/tt]) for integration of Squeezebox devices via UPnP.

Rewriting the code for LuaSOAP ([tt]http://www.keplerproject.org/luasoap/[/tt]) is left as an exercise to the gentle reader. :slight_smile:

So, does it work?

I don’t own a Sonos, but I’m interested in one or four.

How does the above code work with a sonos?

The Lua SOAP code does work with a ZonePlayer 120.

The [tt]luup.call_action[/tt] code does not work due to bug #1473.

@Intrepid

I’ve returned the Sonos ZonePlayer to the dealer, so I won’t be able to do further testing and coding for Sonos devices.

Continued at [tt]http://forum.micasaverde.com/index.php/topic,8505.0.html[/tt].