Overview

These instructions outline how to create a homekit device to play an internet stream on your Sonos device.

These scripts are used with the CMD4 Plugin for Homebridge.

Cmd4 Script

playMusic.js
//  playMusic.js 
//
// > SONOS_HOST=192.168.1.21 node playMusic.js Get anything On 1
//
// Parameters are:
//    Get <accessory name> <characteristic>
//    Set <accessory name> <characteristic> <value>

'use strict';

// Fan
const CHARACTERISTIC_ON="Active"
const CHARACTERISTIC_VOLUME="RotationSpeed"

//Sonos variables
const Sonos = require('sonos').Sonos
const sonos = new Sonos(process.env.SONOS_HOST)

const stationId = 's13606'
const stationTitle = 'Radio Paradise'

var length = process.argv.length;
var device = "";
var io = "";
var characteristic = "";
var option = "";

if (length == 2) process.exit(0);

if (length <= 2) {
   console.log("Usage: " + __filedevice + " <Get|Set> <AccessoryName> [Value]");
   process.exit(-1);
}

if (length >= 2) io = process.argv[2];
if (length >= 3) device = process.argv[3];
if (length >= 4) characteristic  = process.argv[4];
if (length >= 5) option  = process.argv[5];

/*
* playMusic
*
*/
const playMusic = function(){

  sonos.getCurrentState().then(state => {
    if(state != "playing"){
      sonos.playTuneinRadio(stationId, stationTitle).then(success => {
           console.log("PlayMusic on device: " + device + " done! ");    
      }).catch(err => { 
        console.log('Error occurred %j', err) 
      });
    }    
  }).catch(err => { console.log('Error occurred %j', err) })
}

/*
* stop
*
*/
const stop = function(){

  sonos.stop().then(success => {
      console.log("Stop music on device: " + device + " done! ");    
  }).catch(err => { 
    console.log('Error occurred %j', err) 
  });
}

/*
* getState 
*
*/
const getState = function(){

  sonos.getCurrentState().then(state => {
    if(state == "playing"){
      console.log('1');  
    }else{
      console.log('0');  
    }    
  }).catch(err => { console.log('Error occurred %j', err) })
}

/*
* getVolume
*
*/
const getVolume = function(){

  sonos.getVolume().then(volume => {
      console.log(volume);  
  }).catch(err => { console.log('Error occurred %j', err) })
}

/*
* setVolume
*
*/
const setVolume = function(volume){

  if(volume <= 50){
    sonos.setVolume(volume).then(success => {
        console.log("Volume on device: " + device + " set to " + volume);    
    }).catch(err => { 
      console.log('Error occurred %j', err) 
    });
  }else{
      console.log('Too Loud!') 
    }
}


switch(io)
{
   case "Get":
   {
            switch(characteristic)
            {
               case CHARACTERISTIC_ON:
               {  
                  getState();
                  break;
               }
               case CHARACTERISTIC_VOLUME: 
               {
                  getVolume();
                  break;
               }
               default:
                 console.log("Unknown Characteristic for:"  + io  +  " Device:" + device  +  " Characteristic:" + characteristic);
                 process.exit(-1);
            }
      	    break;

   } // End of Switch for "Get"
   case "Set":
   {
            switch(characteristic)
            {
               case CHARACTERISTIC_ON:
               {                    
                  switch(option)
                  {
                    case "0":
                      stop();
                      break;
                    case "1":
                      playMusic();
                      break;
                    default:
                      console.log("Unknown Option '" + option + "' for:"  + io  +  " Device:" + device  +  " Characteristic:" + characteristic);
                      process.exit(-1);   
                  }
                  break;
               }
               case CHARACTERISTIC_VOLUME:
               {
                  setVolume(option);                  
                  break;
               }
               default:
                 console.log("Unknown Characteristic for:"  + io  +  " Device:" + device  +  " Characteristic:" + characteristic);
                 process.exit(-1);
            }
            break;
        
   } // End of Switch Device for "Set"
   default:
      console.log("Unknown IO" + io );
      process.exit(-1);
}


Homebridge Configuration


config.json
...
		{
            "platform": "Cmd4",
            "name": "Cmd4",
            "accessories": [
                {
                    "type": "Fanv2",
                    "displayName": "Kitchen Sonos",
                    "active":                    "INACTIVE",
					"rotationSpeed":              10,
                    "name":                      "Kitchen Sonos",
                    "timeout": 30000,
                    "polling": false,
                    "interval": 100,
                    "stateChangeResponseTime": 3,
                    "state_cmd": "SONOS_HOST=192.168.1.21 node /homebridge/Cmd4Scripts/playMusic.js"
                }
		}
...


References

ReferenceURL
Cmd4 Plugin for Homebridgehttps://github.com/ztalbot2000/homebridge-cmd4


  • No labels