node-sonos
Control your Sonos devices with JavaScript (node.js)
node-sonos gives you the power to control all your Sonos devices from your own apps in JavaScript. Automatically discover your devices on the network and control the playback and queue with instant events announcing change.
Features
- Device Discovery
- Queue Control
- Volume Control
- Spotify Support
- Radio
- Change Events
Install
Published versions (recommended)
$ npm install sonos
From the repo, living on the edge
$ npm install git://github.com/bencevans/node-sonos.git
Quick Start
Discovering Devices
const { DeviceDiscovery } = require('sonos')
// event on all found...
DeviceDiscovery((device) => {
console.log('found device at ' + device.host)
// mute every device...
device.setMuted(true)
.then(d => console.log(`${d.host} now muted`))
})
// find one device
DeviceDiscovery().once('DeviceAvailable', (device) => {
console.log('found device at ' + device.host)
// get all groups
device.getAllGroups().then(groups => {
groups.forEach(group => {
console.log(group.Name);
})
})
})
Discovering devices async
const DeviceDiscovery = require('sonos').AsyncDeviceDiscovery
let discovery = new DeviceDiscovery()
discovery.discover().then((device, model) => {
// Do stuff, see examples/devicediscovery.js
})
Controlling Known Devices
const { Sonos } = require('sonos')
const device = new Sonos('192.168.1.56');
device.play()
.then(() => console.log('now playing'))
device.getVolume()
.then((volume) => console.log(`current volume = ${volume}`))
API
- DeviceDiscovery([options], [deviceAvailableListener])
- Class: DeviceDiscovery([options])
- Event: 'DeviceAvailable'
- destroy()
- Class: 'AsyncDeviceDiscovery'
- discover([options])
- discoverMultiple([options])
- Class: Sonos(host, [port])
- currentTrack()
- deviceDescription()
- flush()
- getCurrentState()
- getLEDState()
- getMusicLibrary(search, options)
- getMuted()
getTopology()Doesn't work if you upgraded to Sonos v9.1. Check-out getAllGroups() for some replacement.- getVolume()
- getZoneAttrs()
- getZoneInfo()
- getQueue()
- next()
- parseDIDL(didl)
- pause()
- play(uri)
- togglePlayback()
- previous()
- queue(uri, positionInQueue)
- queueNext(uri)
- request(endpoint, action, body, responseTag)
- seek(seconds)
- setLEDState(desiredState)
- setMuted(muted)
- setName(name)
- getPlayMode()
- setPlayMode(mode)
- setVolume(volume)
- stop()
- setSpotifyRegion(region)
- alarmClockService()
- ListAlarms()
- PatchAlarm(id,options)
- SetAlarm(id,enabled)
- joinGroup(otherDeviceName)
- leaveGroup()
- getAllGroups()
- startListening(options)
- stopListening()
- Event: 'CurrentTrack'
- Event: 'NextTrack'
- Event: 'PlayState' and 'PlaybackStopped'
- Event: 'AVTransport'
- Event: 'Volume'
- Event: 'Muted'
- Event: 'RenderingControl'
Documentation
We tried to add jsdoc info to all functions, and generate documentation from it. /docs
Examples
Additional examples can be found in the /examples directory within the repository.
In The Wild
node-sonos in use across the interwebs. Missing yours? Add it and send us a pull request!
Apps
- AirSonos - Apple AirPlay (iOS, OS X) support to all Sonos devices on a network.
- sonos-cli - Command Line Interface for Sonos
- sonos2mqtt - Bridge between Sonos and an MQTT server
- homebridge-zp - Homebridge plugin for Sonos ZonePlayer
- ZenMusic - Control Sonos thru #Slack!
- gladys-sonos - Control Sonos with Gladys a Raspberry Pi Home Assistant
- sonos-web - Sonos controller for your web browser
- BudgieStream - Stream system output to Sonos
- GrooveSensor - Motion-activated Sonos playback
Writeups
- How we gave our studio WWE-style entrances using iBeacons and Sonos - A technical run-down of using futuristic technology for sheer entertainment value
Maintainers
- Ben Evans (@bencevans)
- Stephen Wan (@stephen)
- Marshall T. Rose (@mrose17)
- Stephan van Rooij (@svrooij)
Contributors β¨
Thanks goes to these wonderful people (emoji key):