TapHome

Sonos

Packet Parser → HTTP
Inviato da
Ultimo aggiornamento: 03. 2026
Sonos

Sonos è un sistema audio wireless premium progettato per la riproduzione multi-room. TapHome comunica con gli altoparlanti Sonos sulla rete locale tramite UPnP/SOAP sulla porta 1400 — non è necessaria alcuna connessione cloud né autenticazione. Il template è universale e funziona con tutti i modelli di altoparlanti Sonos (Era 100, Era 300, One, Five, Beam, Arc, Ray, Move, Roam, Sub e altri).

Il template offre il controllo completo della riproduzione (play, pausa, stop, salto), gestione del volume e della disattivazione audio, selezione della modalità di riproduzione, timer di spegnimento e fino a 10 pulsanti preimpostati per playlist e stazioni radio. Un set di 14 attributi di servizio espone le informazioni sul dispositivo e il brano attualmente in riproduzione.

Configurazione

L’altoparlante Sonos si connette tramite Wi-Fi. Durante l’importazione del template TapHome, inserisci l’indirizzo IP dell’altoparlante (segnaposto predefinito 192.168.0.1). L’altoparlante deve trovarsi sulla stessa rete della TapHome CCU.

Gli altoparlanti Sonos supportano il discovery mDNS con il pattern hostname Sonos-{MAC}.local (indirizzo MAC senza due punti). L’utilizzo di un hostname previene problemi di connettività quando l’indirizzo IP dell’altoparlante cambia dopo un rinnovo DHCP.

Apri l’app TapHome e usa l’IP Scanner (Impostazioni → Rete → Scansione). Lo scanner rileverà i dispositivi sulla tua rete e mostrerà sia l’indirizzo IP che l’hostname mDNS (es. Sonos-B8E9375A1C02.local). Usa l’hostname al posto dell’indirizzo IP per una connessione più affidabile.

Funzionalità del dispositivo

Controllo del volume

Il template espone il volume come dispositivo dimmer. Il volume viene letto tramite l’azione SOAP RenderingControl GetVolume (intervallo 0–100) e diviso per 100 per produrre il valore dimmer 0,0–1,0. La scrittura di un nuovo volume invia SetVolume con il livello desiderato. Il volume viene interrogato ogni 2,5 secondi.

Due PushButton aggiuntivi — Volume Up e Volume Down — consentono la regolazione relativa del volume. Questi sono gestiti dalla logica lato CCU anziché da azioni SOAP dirette, permettendo l’integrazione con interruttori a parete fisici o smart rule.

Disattivazione audio

La disattivazione audio è esposta come interruttore. Lo stato viene letto tramite GetMute e scritto tramite SetMute sul canale Master. Quando la disattivazione è attiva, l’uscita audio viene silenziata senza modificare il livello del volume.

Controllo della riproduzione

Sei PushButton controllano il trasporto della riproduzione:

  • Play — invia AVTransport Play (Speed=1)
  • Pause — invia AVTransport Pause
  • Stop — invia AVTransport Stop
  • Next — invia AVTransport Next per passare al brano successivo
  • Previous — invia AVTransport Previous per tornare al brano precedente
  • Play/Pause — alterna tra play e pausa in base allo stato di trasporto corrente (gestito dalla logica lato CCU)
Stato della riproduzione

Un sensore binario in sola lettura indica se l’altoparlante sta attualmente riproducendo. Il template legge GetTransportInfo e mappa gli stati di trasporto: PLAYING e TRANSITIONING vengono mappati a 1 (in riproduzione), mentre STOPPED, NO_MEDIA_PRESENT e PAUSED_PLAYBACK vengono mappati a 0 (non in riproduzione). Interrogazione ogni 2,5 secondi.

Modalità di riproduzione

La modalità di riproduzione è un interruttore multi-valore con sei modalità:

ValoreModalitàDescrizione
0NormalRiproduzione sequenziale
1Repeat AllRipetizione dell’intera coda
2Repeat OneRipetizione del brano corrente
3Shuffle No RepeatRiproduzione casuale senza ripetizione
4ShuffleRiproduzione casuale con ripetizione
5Shuffle Repeat OneRiproduzione casuale, ripetizione del brano corrente

I valori 6–9 sono slot di riempimento non utilizzati nella configurazione dell’interruttore multi-valore e non hanno alcuna funzione.

Timer di spegnimento

Due PushButton configurano i timer di spegnimento tramite l’azione SOAP AVTransport ConfigureSleepTimer:

  • Sleep in 1 hour — imposta il timer a 01:00:00
  • Sleep in 15 minutes — imposta il timer a 00:15:00

Allo scadere del timer, l’altoparlante interrompe automaticamente la riproduzione.

Preset playlist e radio

Il template fornisce 10 PushButton preimpostati — 5 per playlist e 5 per stazioni radio. Ogni pulsante ha una variabile dispositivo che l’utente deve configurare con il nome della playlist o della stazione desiderata. Lo script gestisce automaticamente la ricerca, la gestione della coda e la riproduzione.

Playlist (pulsanti 1–5): Ogni pulsante playlist utilizza una variabile dispositivo playlist_name. Lo script cerca nelle playlist Sonos (Code salvate) tramite il contenitore ContentDirectory SQ:, con corrispondenza esatta del dc:title (sensibile alle maiuscole). Quando viene trovata una corrispondenza, lo script svuota la coda corrente, accoda la playlist e avvia la riproduzione.

Per configurare un pulsante playlist:

  1. Apri l’app TapHome → vai al dispositivo playlist → Variabili dispositivo
  2. Imposta playlist_name sul nome esatto di una playlist Sonos
  3. Per creare una playlist Sonos: app Sonos → accoda brani → Salva coda come playlist

Sono supportate solo le playlist Sonos (Code salvate). Le playlist di Spotify, Apple Music o altri servizi di streaming non possono essere utilizzate direttamente. Per utilizzare brani di questi servizi, aggiungili prima a una playlist Sonos.

Stazioni radio (pulsanti 1–5): Ogni pulsante radio utilizza una variabile dispositivo radio_name. Lo script cerca nei Sonos Favorites tramite il contenitore ContentDirectory FV:2, con corrispondenza esatta del dc:title (sensibile alle maiuscole) e filtraggio per il protocollo radio streaming (x-sonosapi-stream).

Per configurare un pulsante radio:

  1. Apri l’app TapHome → vai al dispositivo radio → Variabili dispositivo
  2. Imposta radio_name sul nome esatto della stazione come appare nei Sonos Favorites
  3. Per aggiungere una stazione ai Favorites: app Sonos → sfoglia TuneIn o iHeartRadio → tocca l’icona cuore/stella

Il nome deve corrispondere esattamente (sensibile alle maiuscole). Apri l’app Sonos, vai su My Sonos → Favorites e copia il nome della stazione carattere per carattere. Sono supportate solo le stazioni radio in streaming — i contenuti audio on-demand dai Favorites non funzioneranno.

Diagnostica di servizio

Il template espone 14 attributi di servizio dall’XML di descrizione del dispositivo (/xml/device_description.xml):

  • Info dispositivo — nome modello, URL modello, numero di serie, indirizzo MAC, hostname, UDN (Unique Device Name)
  • Software — versione software, generazione software, revisione hardware, versione API, versione API minima
  • Configurazione Sonos — nome stanza, nome visualizzato
  • In riproduzione — brano corrente (titolo, artista, album estratti dai metadati DIDL-Lite tramite GetPositionInfo)

L’attributo del brano corrente decodifica i TrackMetaData codificati in XML e restituisce una stringa multi-riga con titolo, artista e informazioni sull’album. Se i metadati non sono disponibili (es. ingresso line-in), restituisce “Not supported”.

Funzionalità aggiuntive

L’API UPnP di Sonos espone 16 servizi con funzionalità oltre l’implementazione corrente del template. Il servizio RenderingControl supporta regolazioni di bassi, alti e loudness, nonché la modalità notturna sui modelli soundbar. Il servizio AVTransport offre controllo crossfade, ricerca per tempo o numero di brano e gestione completa della coda (aggiunta, rimozione, riordinamento dei brani). Sono disponibili anche gestione dei gruppi multi-room, pianificazione delle sveglie e impostazioni dell’equalizzatore per configurazioni surround/subwoofer. I modelli portatili (Move, Roam) mostrano lo stato della batteria. Queste funzionalità possono essere aggiunte in un futuro aggiornamento del template.

Risoluzione dei problemi

L’altoparlante non risponde
  1. Verifica che l’altoparlante Sonos sia acceso e connesso al Wi-Fi
  2. Prova a usare l’hostname mDNS (Sonos-{MAC}.local) al posto dell’indirizzo IP — l’IP potrebbe essere cambiato dopo un rinnovo DHCP
  3. Apri http://{speaker-ip}:1400/xml/device_description.xml in un browser — se risponde con XML, l’altoparlante è raggiungibile
  4. Verifica che la TapHome CCU e l’altoparlante Sonos siano sulla stessa rete / VLAN
I comandi di riproduzione falliscono con errore 800

I gruppi multi-room Sonos instradano tutti i comandi di riproduzione attraverso il coordinatore del gruppo. Se il template punta a un altoparlante che non è il coordinatore, i comandi di trasporto (Play, Pause, Next, Previous) falliranno. Assicurati che l’indirizzo IP in TapHome punti al coordinatore del gruppo, oppure rimuovi l’altoparlante dal gruppo per renderlo un dispositivo indipendente.

Le modifiche del volume non si riflettono
  1. Conferma che il template stia interrogando — il volume viene letto ogni 2,5 secondi tramite GetVolume
  2. Se un altro controller (app Sonos, Apple AirPlay) ha modificato il volume, TapHome rileverà il nuovo valore al prossimo ciclo di interrogazione
  3. Verifica che l’altoparlante non sia in un gruppo dove il volume è controllato dal coordinatore del gruppo

Gli altoparlanti Sonos accettano comandi UPnP solo da dispositivi sulla stessa sottorete. Se la TapHome CCU si trova su una VLAN diversa, assicurati che il traffico multicast/UPnP sia instradato tra le VLAN, oppure posiziona entrambi i dispositivi sullo stesso segmento di rete.

Dispositivi disponibili

Sonos Modulo
Attributi di servizio
Nome modello
URL modello
Versione SW
Generazione SW
Revisione HW
Numero di serie
Indirizzo MAC
Nome host
UDN
Versione API
Versione API minima
Nome stanza
Nome visualizzato
Brano corrente
Variabili personalizzate
deviceDescription (string)

Sonos

Attributi di servizio
Model name
VAR response := SENDHTTPREQUEST("/xml/device_description.xml");
deviceDescription := response.Content;

PARSEXML(deviceDescription, "//root:modelName[1]");
Model url
PARSEXML(deviceDescription, "//root:modelURL[1]");
SW version
PARSEXML(deviceDescription, "//root:softwareVersion[1]");
SW generation
PARSEXML(deviceDescription, "//root:swGen[1]");
HW revision
PARSEXML(deviceDescription, "//root:hardwareVersion[1]");
Serial number
PARSEXML(deviceDescription, "//root:serialNum[1]");
Mac address
PARSEXML(deviceDescription, "//root:MACAddress[1]");
Host name
VAR macAddress := PARSEXML(deviceDescription, "//root:MACAddress[1]");
"Sonos-" + REPLACE(macAddress, ":", "") + ".local"
UDN
PARSEXML(deviceDescription, "//root:UDN[1]");
API version
PARSEXML(deviceDescription, "//root:apiVersion[1]");
Min API version
PARSEXML(deviceDescription, "//root:minApiVersion[1]");
Room name
PARSEXML(deviceDescription, "//root:roomName[1]");
Display name
PARSEXML(deviceDescription, "//root:displayName[1]");
Current track
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#GetPositionInfo\"";
VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:GetPositionInfo xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:GetPositionInfo></s:Body></s:Envelope>";

VAR response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);

IF response.IsSuccess
    VAR responseXml := response.Content;
    VAR metaData := DECODE(PARSETEXT(responseXml, "<TrackMetaData>", "</TrackMetaData>"), "xml");
    
    IF(metaData = "NOT_IMPLEMENTED")
        RETURN("Not supported");
    END

    IF(LENGTH(metaData) = 0)
        RETURN("");
    END

    VAR title := PARSEXML(metaData, "//dc:title");
    VAR artist := PARSEXML(metaData, "//dc:creator");
    VAR album := PARSEXML(metaData, "//upnp:album");
    
    VAR result := "";
    
    IF(LENGTH(title) > 0)
        result := "Title: " + title + "\n";
    END
    
    IF(LENGTH(artist) > 0)
         result := result + "Artist: " + artist + "\n";
    END
    
    IF(LENGTH(album) > 0)
        result := result + "Album: " + album;
    END

    RETURN(result);
ELSE
    RETURN("Error");
END
Volume Dimmer
numeric Unità: %

Volume

Lettura livello
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:RenderingControl:1#GetVolume\"";
VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:GetVolume xmlns:u=\"urn:schemas-upnp-org:service:RenderingControl:1\"><InstanceID>0</InstanceID><Channel>Master</Channel></u:GetVolume></s:Body></s:Envelope>";

VAR response := SENDHTTPREQUEST("MediaRenderer/RenderingControl/Control", "POST", body, header);

IF response.IsSuccess
    VAR responseXml := response.Content;
    RETURN(PARSEXML(responseXml, "//CurrentVolume") / 100.0);
ELSE
    ADDERROR(response.Content);
    RETURN(NaN);
END
Scrittura livello
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:RenderingControl:1#SetVolume\"";
VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:SetVolume xmlns:u=\"urn:schemas-upnp-org:service:RenderingControl:1\"><InstanceID>0</InstanceID><Channel>Master</Channel><DesiredVolume>" + ROUND(Le*100) + "</DesiredVolume></u:SetVolume></s:Body></s:Envelope>";

SENDHTTPREQUEST("MediaRenderer/RenderingControl/Control", "POST", body, header);
Brano successivo Pulsante
action

Brano successivo

Scrittura stato pulsante
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Next\"";
VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Next xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:Next></s:Body></s:Envelope>";

SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
Pausa Pulsante
action

Pausa

Scrittura stato pulsante
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Pause\"";
VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Pause xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:Pause></s:Body></s:Envelope>";

SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
Play Pulsante
action

Play

Scrittura stato pulsante
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Play\"";
VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><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>";

SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
Brano precedente Pulsante
action

Brano precedente

Scrittura stato pulsante
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Previous\"";
VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Previous xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:Previous></s:Body></s:Envelope>";

SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
Stop Pulsante
action

Stop

Scrittura stato pulsante
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Stop\"";
VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Stop xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:Stop></s:Body></s:Envelope>";

SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
Volume su Pulsante
action

Volume su

Scrittura stato pulsante
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:RenderingControl:1#SetRelativeVolume\"";

VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:SetRelativeVolume xmlns:u=\"urn:schemas-upnp-org:service:RenderingControl:1\"><InstanceID>0</InstanceID><Channel>Master</Channel><Adjustment>5</Adjustment></u:SetRelativeVolume></s:Body></s:Envelope>";

SENDHTTPREQUEST("MediaRenderer/RenderingControl/Control", "POST", body, header);
Volume giù Pulsante
action

Volume giù

Scrittura stato pulsante
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:RenderingControl:1#SetRelativeVolume\"";

VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:SetRelativeVolume xmlns:u=\"urn:schemas-upnp-org:service:RenderingControl:1\"><InstanceID>0</InstanceID><Channel>Master</Channel><Adjustment>-5</Adjustment></u:SetRelativeVolume></s:Body></s:Envelope>";

SENDHTTPREQUEST("MediaRenderer/RenderingControl/Control", "POST", body, header);
Muto Interruttore
boolean
Valori / Stati: ON · OFF

Muto

Lettura stato interruttore
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:RenderingControl:1#GetMute\"";
VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:GetMute xmlns:u=\"urn:schemas-upnp-org:service:RenderingControl:1\"><InstanceID>0</InstanceID><Channel>Master</Channel></u:GetMute></s:Body></s:Envelope>";

VAR response := SENDHTTPREQUEST("MediaRenderer/RenderingControl/Control", "POST", body, header);

IF response.IsSuccess
    VAR responseXml := response.Content;
    RETURN(PARSEXML(responseXml, "//CurrentMute"));
ELSE
    ADDERROR(response.Content);
    RETURN(NaN);
END
Scrittura stato interruttore
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:RenderingControl:1#SetMute\"";
VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:SetMute xmlns:u=\"urn:schemas-upnp-org:service:RenderingControl:1\"><InstanceID>0</InstanceID><Channel>Master</Channel><DesiredMute>" + St + "</DesiredMute></u:SetMute></s:Body></s:Envelope>";

SENDHTTPREQUEST("MediaRenderer/RenderingControl/Control", "POST", body, header);
Modalità di riproduzione Interruttore multivalore
enum mapped
Valori / Stati: Normal · Repeat all · Repeat one · Shuffle no repeat · Shuffle · Shuffle repeat one

Modalità di riproduzione

Lettura stato interruttore
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#GetTransportSettings\"";
VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:GetTransportSettings xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:GetTransportSettings></s:Body></s:Envelope>";

VAR response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);

IF response.IsSuccess
    VAR responseXml := response.Content;
    VAR mode := PARSEXML(responseXml, "//PlayMode");
    RETURN(SWITCH(mode , "NORMAL", 0, "REPEAT_ALL", 1, "REPEAT_ONE", 2, "SHUFFLE_NOREPEAT", 3, "SHUFFLE", 4, "SHUFFLE_REPEAT_ONE", 5, -1));
ELSE
    ADDERROR(response.Content);
    RETURN(NaN);
END
Scrittura stato interruttore
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#SetPlayMode\"";

VAR mode := SWITCH(Mu, 0, "NORMAL", 1, "REPEAT_ALL", 2, "REPEAT_ONE", 3, "SHUFFLE_NOREPEAT", 4, "SHUFFLE", 5, "SHUFFLE_REPEAT_ONE", "NORMAL");

VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:SetPlayMode xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><NewPlayMode>" + mode +"</NewPlayMode></u:SetPlayMode></s:Body></s:Envelope>";

VAR response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
Timer spegnimento (1 ora) Pulsante
action

Timer spegnimento (1 ora)

Scrittura stato pulsante
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#ConfigureSleepTimer\"";

VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:ConfigureSleepTimer xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><NewSleepTimerDuration>01:00:00</NewSleepTimerDuration></u:ConfigureSleepTimer></s:Body></s:Envelope>";

SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
Timer spegnimento (15 min) Pulsante
action

Timer spegnimento (15 min)

Scrittura stato pulsante
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#ConfigureSleepTimer\"";

VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:ConfigureSleepTimer xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><NewSleepTimerDuration>00:15:00</NewSleepTimerDuration></u:ConfigureSleepTimer></s:Body></s:Envelope>";

SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
Stato riproduzione Contatto reed Solo lettura
boolean mapped

Stato riproduzione

Lettura
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#GetTransportInfo\"";
VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:GetTransportInfo xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:GetTransportInfo></s:Body></s:Envelope>";

VAR response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);

IF response.IsSuccess
    VAR responseXml := response.Content;
    VAR state := PARSEXML(responseXml, "//CurrentTransportState");
    RETURN(SWITCH(state , "PLAYING", 1, "TRANSITIONING", 1, "STOPPED", 0, "NO_MEDIA_PRESENT", 0, "PAUSED_PLAYBACK", 0, 0));
ELSE
    ADDERROR(response.Content);
    RETURN(NaN);
END
Play/Pausa Pulsante
action

Play/Pausa

Scrittura stato pulsante
VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#GetTransportInfo\"";
VAR body := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:GetTransportInfo xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:GetTransportInfo></s:Body></s:Envelope>";

VAR response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);

IF response.IsSuccess
    VAR responseXml := response.Content;
    VAR state := PARSEXML(responseXml, "//CurrentTransportState");
    VAR isPlaying := SWITCH(state , "PLAYING", 1, "TRANSITIONING", 1, "STOPPED", 0, "NO_MEDIA_PRESENT", 0, "PAUSED_PLAYBACK", 0, 0);
    
    IF(isPlaying)
        VAR headerPause := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Pause\"";
        VAR bodyPause := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Pause xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:Pause></s:Body></s:Envelope>";

        SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", bodyPause, headerPause);
    ELSE
        VAR headerPlay := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Play\"";
        VAR bodyPlay := "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><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>";

        SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", bodyPlay, headerPlay);
    END
ELSE
    ADDERROR(response.Content);
    RETURN(NaN);
END
Playlist 1 Pulsante
action
Variabile: playlist_nameExact name of a Sonos playlist (Saved Queue). Case-sensitive.

Playlist 1

Scrittura stato pulsante
######################
# check playlist name is not empty
IF(ISNULL(playlist_name) OR LENGTH(playlist_name) = 0)
   ADDERROR("Please set playlist name in device variables");
   RETURN(-1);
END

######################
# get player uuid
VAR response := SENDHTTPREQUEST("xml/device_description.xml");
VAR content := response.Content;
VAR playerUuid := PARSETEXT(content, "<UDN>uuid:", "</UDN>");

IF(ISNULL(playerUuid))
    ADDERROR("Failed to get player UUID");
    RETURN(-2);
END
######################

######################
# fetch playlists
VAR playlistUri := NULL;
VAR numberOfPlaylists := -1;
VAR readPlaylists := 0;

DO
    VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"";
    VAR body := "<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Browse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\"><ObjectID>SQ:</ObjectID><BrowseFlag>BrowseDirectChildren</BrowseFlag><Filter></Filter><StartingIndex>" + readPlaylists + "</StartingIndex><RequestedCount>10</RequestedCount><SortCriteria></SortCriteria></u:Browse></s:Body></s:Envelope>";

    response := SENDHTTPREQUEST("MediaServer/ContentDirectory/Control", "POST", body, header);
    content := response.Content;
    
    IF(numberOfPlaylists = -1)
        numberOfPlaylists := TODOUBLE(PARSETEXT(content, "<TotalMatches>", "</TotalMatches>"));
    END
    
    VAR returnedPlaylists := TODOUBLE(PARSETEXT(content, "<NumberReturned>", "</NumberReturned>"));
    readPlaylists := readPlaylists + returnedPlaylists;
    
    VAR playlistsXml := DECODE(PARSETEXT(content, "<Result>", "</Result>"), "xml");
    playlistUri := PARSEXML(playlistsXml, "//DIDL-Lite:container[dc:title='" + playlist_name + "']/DIDL-Lite:res");
    
    IF(!ISNULL(playlistUri) AND LENGTH(playlistUri) > 0)
        BREAK;
    END
LOOP WHILE readPlaylists < numberOfPlaylists
    
######################
IF(ISNULL(playlistUri) OR LENGTH(playlistUri) = 0)
    ADDERROR("Unable to find playlist with name '" + playlist_name + "'. Only Sonos playlists are supported.");
    RETURN(-3);
END

######################
# clear play queue
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#RemoveAllTracksFromQueue\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:RemoveAllTracksFromQueue xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:RemoveAllTracksFromQueue></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to RemoveAllTracksFromQueue (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-4);
END
######################

######################
# add playlist to queue
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#AddURIToQueue\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:AddURIToQueue xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><EnqueuedURI>" + playlistUri + "</EnqueuedURI><EnqueuedURIMetaData></EnqueuedURIMetaData><DesiredFirstTrackNumberEnqueued>0</DesiredFirstTrackNumberEnqueued><EnqueueAsNext>0</EnqueueAsNext></u:AddURIToQueue></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to AddURIToQueue (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-5);
END
######################

######################
# set play uri
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:SetAVTransportURI xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><CurrentURI>x-rincon-queue:" + playerUuid + "#0</CurrentURI><CurrentURIMetaData></CurrentURIMetaData></u:SetAVTransportURI></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to SetAVTransportURI (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-6);
END
######################

######################
# play
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Play\"";
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>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to Play (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-7);
END
######################
Playlist 2 Pulsante
action
Variabile: playlist_nameExact name of a Sonos playlist (Saved Queue). Case-sensitive.

Playlist 2

Scrittura stato pulsante
######################
# check playlist name is not empty
IF(ISNULL(playlist_name) OR LENGTH(playlist_name) = 0)
   ADDERROR("Please set playlist name in device variables");
   RETURN(-1);
END

######################
# get player uuid
VAR response := SENDHTTPREQUEST("xml/device_description.xml");
VAR content := response.Content;
VAR playerUuid := PARSETEXT(content, "<UDN>uuid:", "</UDN>");

IF(ISNULL(playerUuid))
    ADDERROR("Failed to get player UUID");
    RETURN(-2);
END
######################

######################
# fetch playlists
VAR playlistUri := NULL;
VAR numberOfPlaylists := -1;
VAR readPlaylists := 0;

DO
    VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"";
    VAR body := "<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Browse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\"><ObjectID>SQ:</ObjectID><BrowseFlag>BrowseDirectChildren</BrowseFlag><Filter></Filter><StartingIndex>" + readPlaylists + "</StartingIndex><RequestedCount>10</RequestedCount><SortCriteria></SortCriteria></u:Browse></s:Body></s:Envelope>";

    response := SENDHTTPREQUEST("MediaServer/ContentDirectory/Control", "POST", body, header);
    content := response.Content;
    
    IF(numberOfPlaylists = -1)
        numberOfPlaylists := TODOUBLE(PARSETEXT(content, "<TotalMatches>", "</TotalMatches>"));
    END
    
    VAR returnedPlaylists := TODOUBLE(PARSETEXT(content, "<NumberReturned>", "</NumberReturned>"));
    readPlaylists := readPlaylists + returnedPlaylists;
    
    VAR playlistsXml := DECODE(PARSETEXT(content, "<Result>", "</Result>"), "xml");
    playlistUri := PARSEXML(playlistsXml, "//DIDL-Lite:container[dc:title='" + playlist_name + "']/DIDL-Lite:res");
    
    IF(!ISNULL(playlistUri) AND LENGTH(playlistUri) > 0)
        BREAK;
    END
LOOP WHILE readPlaylists < numberOfPlaylists
    
######################
IF(ISNULL(playlistUri) OR LENGTH(playlistUri) = 0)
    ADDERROR("Unable to find playlist with name '" + playlist_name + "'. Only Sonos playlists are supported.");
    RETURN(-3);
END

######################
# clear play queue
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#RemoveAllTracksFromQueue\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:RemoveAllTracksFromQueue xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:RemoveAllTracksFromQueue></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to RemoveAllTracksFromQueue (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-4);
END
######################

######################
# add playlist to queue
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#AddURIToQueue\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:AddURIToQueue xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><EnqueuedURI>" + playlistUri + "</EnqueuedURI><EnqueuedURIMetaData></EnqueuedURIMetaData><DesiredFirstTrackNumberEnqueued>0</DesiredFirstTrackNumberEnqueued><EnqueueAsNext>0</EnqueueAsNext></u:AddURIToQueue></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to AddURIToQueue (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-5);
END
######################

######################
# set play uri
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:SetAVTransportURI xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><CurrentURI>x-rincon-queue:" + playerUuid + "#0</CurrentURI><CurrentURIMetaData></CurrentURIMetaData></u:SetAVTransportURI></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to SetAVTransportURI (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-6);
END
######################

######################
# play
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Play\"";
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>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to Play (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-7);
END
######################
Playlist 3 Pulsante
action
Variabile: playlist_nameExact name of a Sonos playlist (Saved Queue). Case-sensitive.

Playlist 3

Scrittura stato pulsante
######################
# check playlist name is not empty
IF(ISNULL(playlist_name) OR LENGTH(playlist_name) = 0)
   ADDERROR("Please set playlist name in device variables");
   RETURN(-1);
END

######################
# get player uuid
VAR response := SENDHTTPREQUEST("xml/device_description.xml");
VAR content := response.Content;
VAR playerUuid := PARSETEXT(content, "<UDN>uuid:", "</UDN>");

IF(ISNULL(playerUuid))
    ADDERROR("Failed to get player UUID");
    RETURN(-2);
END
######################

######################
# fetch playlists
VAR playlistUri := NULL;
VAR numberOfPlaylists := -1;
VAR readPlaylists := 0;

DO
    VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"";
    VAR body := "<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Browse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\"><ObjectID>SQ:</ObjectID><BrowseFlag>BrowseDirectChildren</BrowseFlag><Filter></Filter><StartingIndex>" + readPlaylists + "</StartingIndex><RequestedCount>10</RequestedCount><SortCriteria></SortCriteria></u:Browse></s:Body></s:Envelope>";

    response := SENDHTTPREQUEST("MediaServer/ContentDirectory/Control", "POST", body, header);
    content := response.Content;
    
    IF(numberOfPlaylists = -1)
        numberOfPlaylists := TODOUBLE(PARSETEXT(content, "<TotalMatches>", "</TotalMatches>"));
    END
    
    VAR returnedPlaylists := TODOUBLE(PARSETEXT(content, "<NumberReturned>", "</NumberReturned>"));
    readPlaylists := readPlaylists + returnedPlaylists;
    
    VAR playlistsXml := DECODE(PARSETEXT(content, "<Result>", "</Result>"), "xml");
    playlistUri := PARSEXML(playlistsXml, "//DIDL-Lite:container[dc:title='" + playlist_name + "']/DIDL-Lite:res");
    
    IF(!ISNULL(playlistUri) AND LENGTH(playlistUri) > 0)
        BREAK;
    END
LOOP WHILE readPlaylists < numberOfPlaylists
    
######################
IF(ISNULL(playlistUri) OR LENGTH(playlistUri) = 0)
    ADDERROR("Unable to find playlist with name '" + playlist_name + "'. Only Sonos playlists are supported.");
    RETURN(-3);
END

######################
# clear play queue
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#RemoveAllTracksFromQueue\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:RemoveAllTracksFromQueue xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:RemoveAllTracksFromQueue></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to RemoveAllTracksFromQueue (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-4);
END
######################

######################
# add playlist to queue
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#AddURIToQueue\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:AddURIToQueue xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><EnqueuedURI>" + playlistUri + "</EnqueuedURI><EnqueuedURIMetaData></EnqueuedURIMetaData><DesiredFirstTrackNumberEnqueued>0</DesiredFirstTrackNumberEnqueued><EnqueueAsNext>0</EnqueueAsNext></u:AddURIToQueue></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to AddURIToQueue (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-5);
END
######################

######################
# set play uri
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:SetAVTransportURI xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><CurrentURI>x-rincon-queue:" + playerUuid + "#0</CurrentURI><CurrentURIMetaData></CurrentURIMetaData></u:SetAVTransportURI></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to SetAVTransportURI (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-6);
END
######################

######################
# play
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Play\"";
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>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to Play (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-7);
END
######################
Playlist 4 Pulsante
action
Variabile: playlist_nameExact name of a Sonos playlist (Saved Queue). Case-sensitive.

Playlist 4

Scrittura stato pulsante
######################
# check playlist name is not empty
IF(ISNULL(playlist_name) OR LENGTH(playlist_name) = 0)
   ADDERROR("Please set playlist name in device variables");
   RETURN(-1);
END

######################
# get player uuid
VAR response := SENDHTTPREQUEST("xml/device_description.xml");
VAR content := response.Content;
VAR playerUuid := PARSETEXT(content, "<UDN>uuid:", "</UDN>");

IF(ISNULL(playerUuid))
    ADDERROR("Failed to get player UUID");
    RETURN(-2);
END
######################

######################
# fetch playlists
VAR playlistUri := NULL;
VAR numberOfPlaylists := -1;
VAR readPlaylists := 0;

DO
    VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"";
    VAR body := "<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Browse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\"><ObjectID>SQ:</ObjectID><BrowseFlag>BrowseDirectChildren</BrowseFlag><Filter></Filter><StartingIndex>" + readPlaylists + "</StartingIndex><RequestedCount>10</RequestedCount><SortCriteria></SortCriteria></u:Browse></s:Body></s:Envelope>";

    response := SENDHTTPREQUEST("MediaServer/ContentDirectory/Control", "POST", body, header);
    content := response.Content;
    
    IF(numberOfPlaylists = -1)
        numberOfPlaylists := TODOUBLE(PARSETEXT(content, "<TotalMatches>", "</TotalMatches>"));
    END
    
    VAR returnedPlaylists := TODOUBLE(PARSETEXT(content, "<NumberReturned>", "</NumberReturned>"));
    readPlaylists := readPlaylists + returnedPlaylists;
    
    VAR playlistsXml := DECODE(PARSETEXT(content, "<Result>", "</Result>"), "xml");
    playlistUri := PARSEXML(playlistsXml, "//DIDL-Lite:container[dc:title='" + playlist_name + "']/DIDL-Lite:res");
    
    IF(!ISNULL(playlistUri) AND LENGTH(playlistUri) > 0)
        BREAK;
    END
LOOP WHILE readPlaylists < numberOfPlaylists
    
######################
IF(ISNULL(playlistUri) OR LENGTH(playlistUri) = 0)
    ADDERROR("Unable to find playlist with name '" + playlist_name + "'. Only Sonos playlists are supported.");
    RETURN(-3);
END

######################
# clear play queue
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#RemoveAllTracksFromQueue\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:RemoveAllTracksFromQueue xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:RemoveAllTracksFromQueue></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to RemoveAllTracksFromQueue (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-4);
END
######################

######################
# add playlist to queue
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#AddURIToQueue\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:AddURIToQueue xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><EnqueuedURI>" + playlistUri + "</EnqueuedURI><EnqueuedURIMetaData></EnqueuedURIMetaData><DesiredFirstTrackNumberEnqueued>0</DesiredFirstTrackNumberEnqueued><EnqueueAsNext>0</EnqueueAsNext></u:AddURIToQueue></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to AddURIToQueue (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-5);
END
######################

######################
# set play uri
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:SetAVTransportURI xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><CurrentURI>x-rincon-queue:" + playerUuid + "#0</CurrentURI><CurrentURIMetaData></CurrentURIMetaData></u:SetAVTransportURI></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to SetAVTransportURI (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-6);
END
######################

######################
# play
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Play\"";
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>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to Play (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-7);
END
######################
Playlist 5 Pulsante
action
Variabile: playlist_nameExact name of a Sonos playlist (Saved Queue). Case-sensitive.

Playlist 5

Scrittura stato pulsante
######################
# check playlist name is not empty
IF(ISNULL(playlist_name) OR LENGTH(playlist_name) = 0)
   ADDERROR("Please set playlist name in device variables");
   RETURN(-1);
END

######################
# get player uuid
VAR response := SENDHTTPREQUEST("xml/device_description.xml");
VAR content := response.Content;
VAR playerUuid := PARSETEXT(content, "<UDN>uuid:", "</UDN>");

IF(ISNULL(playerUuid))
    ADDERROR("Failed to get player UUID");
    RETURN(-2);
END
######################

######################
# fetch playlists
VAR playlistUri := NULL;
VAR numberOfPlaylists := -1;
VAR readPlaylists := 0;

DO
    VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"";
    VAR body := "<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Browse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\"><ObjectID>SQ:</ObjectID><BrowseFlag>BrowseDirectChildren</BrowseFlag><Filter></Filter><StartingIndex>" + readPlaylists + "</StartingIndex><RequestedCount>10</RequestedCount><SortCriteria></SortCriteria></u:Browse></s:Body></s:Envelope>";

    response := SENDHTTPREQUEST("MediaServer/ContentDirectory/Control", "POST", body, header);
    content := response.Content;
    
    IF(numberOfPlaylists = -1)
        numberOfPlaylists := TODOUBLE(PARSETEXT(content, "<TotalMatches>", "</TotalMatches>"));
    END
    
    VAR returnedPlaylists := TODOUBLE(PARSETEXT(content, "<NumberReturned>", "</NumberReturned>"));
    readPlaylists := readPlaylists + returnedPlaylists;
    
    VAR playlistsXml := DECODE(PARSETEXT(content, "<Result>", "</Result>"), "xml");
    playlistUri := PARSEXML(playlistsXml, "//DIDL-Lite:container[dc:title='" + playlist_name + "']/DIDL-Lite:res");
    
    IF(!ISNULL(playlistUri) AND LENGTH(playlistUri) > 0)
        BREAK;
    END
LOOP WHILE readPlaylists < numberOfPlaylists
    
######################
IF(ISNULL(playlistUri) OR LENGTH(playlistUri) = 0)
    ADDERROR("Unable to find playlist with name '" + playlist_name + "'. Only Sonos playlists are supported.");
    RETURN(-3);
END

######################
# clear play queue
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#RemoveAllTracksFromQueue\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:RemoveAllTracksFromQueue xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:RemoveAllTracksFromQueue></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to RemoveAllTracksFromQueue (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-4);
END
######################

######################
# add playlist to queue
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#AddURIToQueue\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:AddURIToQueue xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><EnqueuedURI>" + playlistUri + "</EnqueuedURI><EnqueuedURIMetaData></EnqueuedURIMetaData><DesiredFirstTrackNumberEnqueued>0</DesiredFirstTrackNumberEnqueued><EnqueueAsNext>0</EnqueueAsNext></u:AddURIToQueue></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to AddURIToQueue (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-5);
END
######################

######################
# set play uri
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:SetAVTransportURI xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><CurrentURI>x-rincon-queue:" + playerUuid + "#0</CurrentURI><CurrentURIMetaData></CurrentURIMetaData></u:SetAVTransportURI></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to SetAVTransportURI (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-6);
END
######################

######################
# play
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Play\"";
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>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
    ADDERROR("Unable to Play (" + PARSEJSON(response, "Content", 1) + ")");
    RETURN(-7);
END
######################
Stazione radio 1 Pulsante
action
Variabile: radio_nameExact name of a radio station from Sonos Favorites. Case-sensitive.

Stazione radio 1

Scrittura stato pulsante
######################
# check radio name is not empty
IF(ISNULL(radio_name) OR LENGTH(radio_name) = 0)
   ADDERROR("Please set radio name in device variables");
   RETURN(-1);
END

######################
# fetch favorites
VAR radioUri := NULL;
VAR numberOfFavorites := -1;
VAR readFavorites := 0;

DO
    VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"";
    VAR body := "<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Browse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\"><ObjectID>FV:2</ObjectID><BrowseFlag>BrowseDirectChildren</BrowseFlag><Filter></Filter><StartingIndex>" + readFavorites + "</StartingIndex><RequestedCount>10</RequestedCount><SortCriteria></SortCriteria></u:Browse></s:Body></s:Envelope>";

    VAR response := SENDHTTPREQUEST("MediaServer/ContentDirectory/Control", "POST", body, header);
    VAR content := response.Content;
   
    IF(numberOfFavorites = -1)
        numberOfFavorites := TODOUBLE(PARSETEXT(content, "<TotalMatches>", "</TotalMatches>"));
    END
    
    VAR returnedFavorites := TODOUBLE(PARSETEXT(content, "<NumberReturned>", "</NumberReturned>"));
    readFavorites := readFavorites + returnedFavorites;
    
    VAR favoritesXml := DECODE(PARSETEXT(content, "<Result>", "</Result>"), "xml");
    radioUri := PARSEXML(favoritesXml, "//DIDL-Lite:item[dc:title='" + radio_name + "']/DIDL-Lite:res[@protocolInfo='x-sonosapi-stream:*:*:*']");
    
    IF(!ISNULL(radioUri) AND LENGTH(radioUri) > 0)
        BREAK;
    END
LOOP WHILE readFavorites < numberOfFavorites

IF(ISNULL(radioUri) OR LENGTH(radioUri) = 0)
    ADDERROR("Unable to find radio with name '" + radio_name + "'. Make sure it's included in your Sonos favorite radio stations");
    RETURN(-3);
END

######################
# find radio metadata
VAR radioMetaData := PARSEXML(favoritesXml, "//DIDL-Lite:item[dc:title='" + radio_name + "'and DIDL-Lite:res[@protocolInfo='x-sonosapi-stream:*:*:*']]/r:resMD");

IF(ISNULL(radioMetaData) OR LENGTH(radioMetaData) = 0)
	ADDERROR("Unable to find radio metadata");
	RETURN(-4);
END

######################
# set radio uri
VAR metaDataEncoded := ENCODE(radioMetaData, "xml");
VAR uriEncoded := ENCODE(radioUri, "xml");

header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:SetAVTransportURI xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><CurrentURI>" + uriEncoded + "</CurrentURI><CurrentURIMetaData>" + metaDataEncoded + "</CurrentURIMetaData></u:SetAVTransportURI></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
	ADDERROR("Unable to SetAVTransportURI (" + PARSEJSON(response, "Content", 1) + ")");
	RETURN(-5);
END
######################

######################
# play
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Play\"";
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>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
	ADDERROR("Unable to Play (" + PARSEJSON(response, "Content", 1) + ")");
	RETURN(-6);
END
######################
Stazione radio 2 Pulsante
action
Variabile: radio_nameExact name of a radio station from Sonos Favorites. Case-sensitive.

Stazione radio 2

Scrittura stato pulsante
######################
# check radio name is not empty
IF(ISNULL(radio_name) OR LENGTH(radio_name) = 0)
   ADDERROR("Please set radio name in device variables");
   RETURN(-1);
END

######################
# fetch favorites
VAR radioUri := NULL;
VAR numberOfFavorites := -1;
VAR readFavorites := 0;

DO
    VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"";
    VAR body := "<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Browse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\"><ObjectID>FV:2</ObjectID><BrowseFlag>BrowseDirectChildren</BrowseFlag><Filter></Filter><StartingIndex>" + readFavorites + "</StartingIndex><RequestedCount>10</RequestedCount><SortCriteria></SortCriteria></u:Browse></s:Body></s:Envelope>";

    VAR response := SENDHTTPREQUEST("MediaServer/ContentDirectory/Control", "POST", body, header);
    VAR content := response.Content;
   
    IF(numberOfFavorites = -1)
        numberOfFavorites := TODOUBLE(PARSETEXT(content, "<TotalMatches>", "</TotalMatches>"));
    END
    
    VAR returnedFavorites := TODOUBLE(PARSETEXT(content, "<NumberReturned>", "</NumberReturned>"));
    readFavorites := readFavorites + returnedFavorites;
    
    VAR favoritesXml := DECODE(PARSETEXT(content, "<Result>", "</Result>"), "xml");
    radioUri := PARSEXML(favoritesXml, "//DIDL-Lite:item[dc:title='" + radio_name + "']/DIDL-Lite:res[@protocolInfo='x-sonosapi-stream:*:*:*']");
    
    IF(!ISNULL(radioUri) AND LENGTH(radioUri) > 0)
        BREAK;
    END
LOOP WHILE readFavorites < numberOfFavorites

IF(ISNULL(radioUri) OR LENGTH(radioUri) = 0)
    ADDERROR("Unable to find radio with name '" + radio_name + "'. Make sure it's included in your Sonos favorite radio stations");
    RETURN(-3);
END

######################
# find radio metadata
VAR radioMetaData := PARSEXML(favoritesXml, "//DIDL-Lite:item[dc:title='" + radio_name + "'and DIDL-Lite:res[@protocolInfo='x-sonosapi-stream:*:*:*']]/r:resMD");

IF(ISNULL(radioMetaData) OR LENGTH(radioMetaData) = 0)
	ADDERROR("Unable to find radio metadata");
	RETURN(-4);
END

######################
# set radio uri
VAR metaDataEncoded := ENCODE(radioMetaData, "xml");
VAR uriEncoded := ENCODE(radioUri, "xml");

header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:SetAVTransportURI xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><CurrentURI>" + uriEncoded + "</CurrentURI><CurrentURIMetaData>" + metaDataEncoded + "</CurrentURIMetaData></u:SetAVTransportURI></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
	ADDERROR("Unable to SetAVTransportURI (" + PARSEJSON(response, "Content", 1) + ")");
	RETURN(-5);
END
######################

######################
# play
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Play\"";
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>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
	ADDERROR("Unable to Play (" + PARSEJSON(response, "Content", 1) + ")");
	RETURN(-6);
END
######################
Stazione radio 3 Pulsante
action
Variabile: radio_nameExact name of a radio station from Sonos Favorites. Case-sensitive.

Stazione radio 3

Scrittura stato pulsante
######################
# check radio name is not empty
IF(ISNULL(radio_name) OR LENGTH(radio_name) = 0)
   ADDERROR("Please set radio name in device variables");
   RETURN(-1);
END

######################
# fetch favorites
VAR radioUri := NULL;
VAR numberOfFavorites := -1;
VAR readFavorites := 0;

DO
    VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"";
    VAR body := "<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Browse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\"><ObjectID>FV:2</ObjectID><BrowseFlag>BrowseDirectChildren</BrowseFlag><Filter></Filter><StartingIndex>" + readFavorites + "</StartingIndex><RequestedCount>10</RequestedCount><SortCriteria></SortCriteria></u:Browse></s:Body></s:Envelope>";

    VAR response := SENDHTTPREQUEST("MediaServer/ContentDirectory/Control", "POST", body, header);
    VAR content := response.Content;
   
    IF(numberOfFavorites = -1)
        numberOfFavorites := TODOUBLE(PARSETEXT(content, "<TotalMatches>", "</TotalMatches>"));
    END
    
    VAR returnedFavorites := TODOUBLE(PARSETEXT(content, "<NumberReturned>", "</NumberReturned>"));
    readFavorites := readFavorites + returnedFavorites;
    
    VAR favoritesXml := DECODE(PARSETEXT(content, "<Result>", "</Result>"), "xml");
    radioUri := PARSEXML(favoritesXml, "//DIDL-Lite:item[dc:title='" + radio_name + "']/DIDL-Lite:res[@protocolInfo='x-sonosapi-stream:*:*:*']");
    
    IF(!ISNULL(radioUri) AND LENGTH(radioUri) > 0)
        BREAK;
    END
LOOP WHILE readFavorites < numberOfFavorites

IF(ISNULL(radioUri) OR LENGTH(radioUri) = 0)
    ADDERROR("Unable to find radio with name '" + radio_name + "'. Make sure it's included in your Sonos favorite radio stations");
    RETURN(-3);
END

######################
# find radio metadata
VAR radioMetaData := PARSEXML(favoritesXml, "//DIDL-Lite:item[dc:title='" + radio_name + "'and DIDL-Lite:res[@protocolInfo='x-sonosapi-stream:*:*:*']]/r:resMD");

IF(ISNULL(radioMetaData) OR LENGTH(radioMetaData) = 0)
	ADDERROR("Unable to find radio metadata");
	RETURN(-4);
END

######################
# set radio uri
VAR metaDataEncoded := ENCODE(radioMetaData, "xml");
VAR uriEncoded := ENCODE(radioUri, "xml");

header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:SetAVTransportURI xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><CurrentURI>" + uriEncoded + "</CurrentURI><CurrentURIMetaData>" + metaDataEncoded + "</CurrentURIMetaData></u:SetAVTransportURI></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
	ADDERROR("Unable to SetAVTransportURI (" + PARSEJSON(response, "Content", 1) + ")");
	RETURN(-5);
END
######################

######################
# play
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Play\"";
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>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
	ADDERROR("Unable to Play (" + PARSEJSON(response, "Content", 1) + ")");
	RETURN(-6);
END
######################
Stazione radio 4 Pulsante
action
Variabile: radio_nameExact name of a radio station from Sonos Favorites. Case-sensitive.

Stazione radio 4

Scrittura stato pulsante
######################
# check radio name is not empty
IF(ISNULL(radio_name) OR LENGTH(radio_name) = 0)
   ADDERROR("Please set radio name in device variables");
   RETURN(-1);
END

######################
# fetch favorites
VAR radioUri := NULL;
VAR numberOfFavorites := -1;
VAR readFavorites := 0;

DO
    VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"";
    VAR body := "<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Browse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\"><ObjectID>FV:2</ObjectID><BrowseFlag>BrowseDirectChildren</BrowseFlag><Filter></Filter><StartingIndex>" + readFavorites + "</StartingIndex><RequestedCount>10</RequestedCount><SortCriteria></SortCriteria></u:Browse></s:Body></s:Envelope>";

    VAR response := SENDHTTPREQUEST("MediaServer/ContentDirectory/Control", "POST", body, header);
    VAR content := response.Content;
   
    IF(numberOfFavorites = -1)
        numberOfFavorites := TODOUBLE(PARSETEXT(content, "<TotalMatches>", "</TotalMatches>"));
    END
    
    VAR returnedFavorites := TODOUBLE(PARSETEXT(content, "<NumberReturned>", "</NumberReturned>"));
    readFavorites := readFavorites + returnedFavorites;
    
    VAR favoritesXml := DECODE(PARSETEXT(content, "<Result>", "</Result>"), "xml");
    radioUri := PARSEXML(favoritesXml, "//DIDL-Lite:item[dc:title='" + radio_name + "']/DIDL-Lite:res[@protocolInfo='x-sonosapi-stream:*:*:*']");
    
    IF(!ISNULL(radioUri) AND LENGTH(radioUri) > 0)
        BREAK;
    END
LOOP WHILE readFavorites < numberOfFavorites

IF(ISNULL(radioUri) OR LENGTH(radioUri) = 0)
    ADDERROR("Unable to find radio with name '" + radio_name + "'. Make sure it's included in your Sonos favorite radio stations");
    RETURN(-3);
END

######################
# find radio metadata
VAR radioMetaData := PARSEXML(favoritesXml, "//DIDL-Lite:item[dc:title='" + radio_name + "'and DIDL-Lite:res[@protocolInfo='x-sonosapi-stream:*:*:*']]/r:resMD");

IF(ISNULL(radioMetaData) OR LENGTH(radioMetaData) = 0)
	ADDERROR("Unable to find radio metadata");
	RETURN(-4);
END

######################
# set radio uri
VAR metaDataEncoded := ENCODE(radioMetaData, "xml");
VAR uriEncoded := ENCODE(radioUri, "xml");

header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:SetAVTransportURI xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><CurrentURI>" + uriEncoded + "</CurrentURI><CurrentURIMetaData>" + metaDataEncoded + "</CurrentURIMetaData></u:SetAVTransportURI></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
	ADDERROR("Unable to SetAVTransportURI (" + PARSEJSON(response, "Content", 1) + ")");
	RETURN(-5);
END
######################

######################
# play
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Play\"";
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>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
	ADDERROR("Unable to Play (" + PARSEJSON(response, "Content", 1) + ")");
	RETURN(-6);
END
######################
Stazione radio 5 Pulsante
action
Variabile: radio_nameExact name of a radio station from Sonos Favorites. Case-sensitive.

Stazione radio 5

Scrittura stato pulsante
######################
# check radio name is not empty
IF(ISNULL(radio_name) OR LENGTH(radio_name) = 0)
   ADDERROR("Please set radio name in device variables");
   RETURN(-1);
END

######################
# fetch favorites
VAR radioUri := NULL;
VAR numberOfFavorites := -1;
VAR readFavorites := 0;

DO
    VAR header := "SOAPACTION: \"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"";
    VAR body := "<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Browse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\"><ObjectID>FV:2</ObjectID><BrowseFlag>BrowseDirectChildren</BrowseFlag><Filter></Filter><StartingIndex>" + readFavorites + "</StartingIndex><RequestedCount>10</RequestedCount><SortCriteria></SortCriteria></u:Browse></s:Body></s:Envelope>";

    VAR response := SENDHTTPREQUEST("MediaServer/ContentDirectory/Control", "POST", body, header);
    VAR content := response.Content;
   
    IF(numberOfFavorites = -1)
        numberOfFavorites := TODOUBLE(PARSETEXT(content, "<TotalMatches>", "</TotalMatches>"));
    END
    
    VAR returnedFavorites := TODOUBLE(PARSETEXT(content, "<NumberReturned>", "</NumberReturned>"));
    readFavorites := readFavorites + returnedFavorites;
    
    VAR favoritesXml := DECODE(PARSETEXT(content, "<Result>", "</Result>"), "xml");
    radioUri := PARSEXML(favoritesXml, "//DIDL-Lite:item[dc:title='" + radio_name + "']/DIDL-Lite:res[@protocolInfo='x-sonosapi-stream:*:*:*']");
    
    IF(!ISNULL(radioUri) AND LENGTH(radioUri) > 0)
        BREAK;
    END
LOOP WHILE readFavorites < numberOfFavorites

IF(ISNULL(radioUri) OR LENGTH(radioUri) = 0)
    ADDERROR("Unable to find radio with name '" + radio_name + "'. Make sure it's included in your Sonos favorite radio stations");
    RETURN(-3);
END

######################
# find radio metadata
VAR radioMetaData := PARSEXML(favoritesXml, "//DIDL-Lite:item[dc:title='" + radio_name + "'and DIDL-Lite:res[@protocolInfo='x-sonosapi-stream:*:*:*']]/r:resMD");

IF(ISNULL(radioMetaData) OR LENGTH(radioMetaData) = 0)
	ADDERROR("Unable to find radio metadata");
	RETURN(-4);
END

######################
# set radio uri
VAR metaDataEncoded := ENCODE(radioMetaData, "xml");
VAR uriEncoded := ENCODE(radioUri, "xml");

header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI\"";
body := "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:SetAVTransportURI xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><CurrentURI>" + uriEncoded + "</CurrentURI><CurrentURIMetaData>" + metaDataEncoded + "</CurrentURIMetaData></u:SetAVTransportURI></s:Body></s:Envelope>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
	ADDERROR("Unable to SetAVTransportURI (" + PARSEJSON(response, "Content", 1) + ")");
	RETURN(-5);
END
######################

######################
# play
header := "SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#Play\"";
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>";

response := SENDHTTPREQUEST("MediaRenderer/AVTransport/Control", "POST", body, header);
IF(!response.IsSuccess)
	ADDERROR("Unable to Play (" + PARSEJSON(response, "Content", 1) + ")");
	RETURN(-6);
END
######################
Connessione: Packet Parser → HTTP
Possibili miglioramenti (14)
  • Bass control — Adjustable bass level -10 to 10
  • Treble control — Adjustable treble level -10 to 10
  • Loudness compensation — Boolean loudness on/off
  • Night mode — Boolean night mode (reduced dynamics), available on soundbar models
  • Crossfade — Boolean crossfade on/off between tracks
  • Seek (time/track) — Seek by TRACK_NR, REL_TIME, or TIME_DELTA
  • Queue management — Full queue CRUD — add, remove, reorder tracks
  • Group management — Multi-room group join/leave/delegate coordinator
  • Alarm management — Create, edit, delete alarms on the speaker
  • Battery status — Battery data available on portable models only (Move, Roam)
  • Relative volume — Increment/decrement volume by delta value
  • Sleep timer remaining — Read remaining sleep timer duration (hh:mm:ss)
  • Surround/Sub EQ — Subwoofer gain, surround level, height channel — available on multi-speaker setups
  • LED control — Status LED on/off

Fonti