Typescript library to control your sonos speakers. Can be used in other typescript (or node) apps.
To use the library just add it to your project. npm install @svrooij/sonos
. And start using it. This library isn't meant to be used by itself, as you see in the examples you still need to use node (or typescript).
See Documentation
If you need help using this library, join us on discord.
This library is developed with Sonos groups in mind. We created a SonosManager to discover all known groups and keep track of changes to them.
const SonosManager = require('@svrooij/sonos').SonosManager
const manager = new SonosManager()
manager.InitializeWithDiscovery(10)
.then(console.log)
.then(() => {
manager.Devices.forEach(d => console.log('Device %s (%s) is joined in %s', d.Name, d.uuid, d.GroupName))
})
.catch(console.error)
In some network situations (or Docker usage) SSDP won't work, but you can also start the manager if you know one (static) IP of a single speaker.
const SonosManager = require('@svrooij/sonos').SonosManager
const manager = new SonosManager()
manager.InitializeFromDevice(process.env.SONOS_HOST || '192.168.96.56')
.then(console.log)
.then(() => {
manager.Devices.forEach(d => console.log('Device %s (%s) is joined in %s', d.Name, d.uuid, d.GroupName))
})
.catch(console.error)
If you just want to control a single device and don't want to use the SonosManager, you can also create a instance of SonosDevice, but you'll be missing the group options.
const SonosDevice = require('@svrooij/sonos').SonosDevice
const sonos = new SonosDevice(process.env.SONOS_HOST || '192.168.96.56')
sonos.LoadDeviceData()
.then(success => {
console.log(sonos.Name)
})
.catch(console.error)
A lot of people want to send text to sonos to use for notifications (or a welcome message in your B&B). This library has support for text-to-speech but you'll need a text-to-speech endpoint. To keep this library as clean as possible, the text-to-speech server is build in a seperate package. See node-sonos-tts-polly for a text-to-speech server that uses Amazon Polly for speech generation.
For my sponsors I've setup a hosted version, so if you don't want to setup your own server, you know what to do.
The text-to-speech works as following:
.PlayNotification({})
command, with the tts url.You can also set the endpoint with the SONOS_TTS_ENDPOINT
environment variable, so you don't have to supply it every time. The default language can be set with the environment variable SONOS_TTS_LANG
.
The server I've build is based on Amazon Polly, but I invite eveybody to build their own if you want to support an other tts service.
const SonosDevice = require('../lib').SonosDevice
const sonos = new SonosDevice(process.env.SONOS_HOST || '192.168.96.56')
sonos.PlayTTS({ text: 'Someone at the front-door', lang: 'en-US', gender: 'male', volume: 50, endpoint: 'https://your.tts.endpoint/api/generate' })
.then(played => {
console.log('Played notification %o', played)
// Timeout to allow event subscriptions to cancel.
setTimeout(() => {
process.exit(0)
}, 500)
})
Name | Maintainer | Description |
---|---|---|
sonos2mqtt | @svrooij | A bridge between sonos and mqtt, so you can control all your sonos devices right from your mqtt server |
sonos-cli | @svrooij | An experimental command line interface for your sonos devices. |
Also using this library, but not in the list? Send a PR.
You can contribute in many ways. Asking good questions, solving bugs, sponsoring me on github. This library is build in my spare time, so don't be rude about it.
If you're using a music service that currently isn't supported for metadata generation, you should check out the metadata generator.
It works by taking an url (which you can get by running the get-position-info sample). And generating a Track for it. Use the information out the console to get the right values.
The values you'll be looking for are ProtocolInfo
, TrackUri
, UpnpClass
, ItemID
and ParentID
.
Thanks goes to these wonderful people (emoji key):
Stephan van Rooij š» š š¤ š§ |
Sven Werner š» |
H. Klages š |
This project follows the all-contributors specification. Contributions of any kind welcome!
This will contain usefull information if you want to fix a bug you've found in
this library. You always start with cloning the repo and doing a
npm install
in the folder. I like consistancy so everything is in a specific order :wink:.
This library has two VSCode launch configurations.
One for running the current open example, you can set breakpoints in the example
file and in the typescript code! Be sure to change the IP to your own in .vscode/launch.json
,
so you don't have to edit all the example files.
And it has a launch configuration to run the current Mocha test file, be sure to have a mocha test (in test folder) open.
Most of this library is generated by the generator. You can use the generator in your own project. Or just use the service file. I could use some help improving the code of the generator.
Creating a library from scratch is quite hard, and I'm using a lot of stuff from node-sonos. That wouldn't exists without the contributors.