lukasroegner / homebridge-sonos-multiroom

Plugin for real Sonos multiroom experience in homebridge.
MIT License
35 stars 3 forks source link

homebridge-sonos-multiroom

This project is a homebridge plugin for the Sonos system. Instead of trying to support all features of the Sonos devices, it aims to provide a simple feature set while enabling a real multiroom experience.

Who is it for?

The use case for this plugin is simple: you want your music or TV audio stream to follow you around in your home. This can be accomplished with a combination of this plugin and HomeKit motion/occupancy sensors.

Which HomeKit accessories are provided?

The plugin exposes each zone (e.g. room) of your Sonos system as an outlet, which can be switched ON and OFF (you can also enable switches for night mode and speech enhancement for the Playbar and Playbase).

How does multiroom handling work?

The exposed accessories have the following logic when being switched ON:

The exposed accessories have the following logic when being switched OFF:

Now, create HomeKit automations for your motion/occupancy sensors for each room

Result: If you enter a room, it will automatically start playback of music/TV that is playing in another room. If you leave the room, music playback stops.

Installation

Install the plugin via npm:

npm install homebridge-sonos-multiroom -g

Configuration

{
    "platforms": [
        {
            "platform": "SonosMultiroomPlatform",
            "discoveryTimeout": 5000,
            "zones": [
                {
                    "name": "Living Room",
                    "isNightModeEnabled": true,
                    "isSpeechEnhancementEnabled": true,
                    "priorities": [
                        "Bedroom",
                        "Bathroom"
                    ],
                    "isAutoPlayDisabled": false
                },
                {
                    "name": "Bathroom",
                    "priorities": [
                        "Living Room",
                        "Bedroom"
                    ],
                    "isAutoPlayDisabled": false
                },
                {
                    "name": "Bedroom",
                    "priorities": [
                        "Living Room",
                        "Bathroom"
                    ],
                    "isAutoPlayDisabled": false
                }
            ],
            "isApiEnabled": false,
            "apiPort": 40809,
            "apiToken": "<YOUR-TOKEN>"
        }
    ]
}

discoveryTimeout (optional): Time span in milliseconds for which the plugin searches for Sonos devices. Defaults to 5000.

zones: An array of all zone (e.g. rooms) that you want the plugin to expose to HomeKit.

name: The name of the zone. Must match the name in the Sonos app.

isNightModeEnabled (optional): If set to true, a switch is exposed for the night mode. Defaults to false. (only for Playbar/Playbase)

isSpeechEnhancementEnabled (optional): If set to true, a switch is exposed for the speech enhancement. Defaults to false. (only for Playbar/Playbase)

priorities (optional): If provided, this list of zone names defines the priority when searching for a music/TV stream to play when the accessories is switched to ON.

isAutoPlayDisabled (optional): If set to true, the Sonos won't start playing if no other Sonos is currently playing. Defaults to false.

isApiEnabled (optional): Enables an HTTP API for controlling Sonos zones. Defaults to false. See API for more information.

apiPort (optional): The port that the API (if enabled) runs on. Defaults to 40809, please change this setting of the port is already in use.

apiToken (optional): The token that has to be included in each request of the API. Is required if the API is enabled and has no default value.

API

This plugin also provides an HTTP API to control some features of the Sonos system. It has been created so that you can further automate the system with HomeKit shortcuts. Starting with iOS 13, you can use shortcuts for HomeKit automation. Those automations that are executed on the HomeKit coordinator (i.e. iPad, AppleTV or HomePod) also support HTTP requests, which means you can automate your Sonos system without annoying switches and buttons exposed in HomeKit.

If the API is enabled, it can be reached at the specified port on the host of this plugin.

http://<YOUR-HOST-IP-ADDRESS>:<apiPort>

The token has to be specified as value of the Authorization header on each request:

Authorization: <YOUR-TOKEN>

API - Get single value of Sonos zone

Use the zones/<ZONE-NAME>/<PROPERTY-NAME> endpoint to retrieve a single value of a Sonos zone. The HTTP method has to be GET:

http://<YOUR-HOST-IP-ADDRESS>:<apiPort>/zones/<ZONE-NAME>/<PROPERTY-NAME>

The response is a plain text response (easier to handle in HomeKit shortcuts), the following property names are supported:

API - Get all values of Sonos zone

Use the zones/<ZONE-NAME> endpoint to retrieve all values of a Sonos zone. The HTTP method has to be GET:

http://<YOUR-HOST-IP-ADDRESS>:<apiPort>/zones/<ZONE-NAME>

The response is a JSON object containing all values:

{
    "led-state": true,
    "current-track": {
        "title": "...",
        "artist": "...",
        "album": "..."
    }
    "current-state": "playing",
    "volume": 16,
    "mute": false
}

When retrieving all values, the current-track property may also be null or TV.

API - Set values of Sonos zone

Use the zones/<ZONE-NAME> endpoint to set values of a Sonos zone. The HTTP method has to be POST:

http://<YOUR-HOST-IP-ADDRESS>:<apiPort>/zones/<ZONE-NAME>

The body of the request has to be JSON containing the new values:

{
    "<PROPERTY-NAME>": <VALUE>
}

Multiple properties can be set with one request.

The following property names are supported:

API - Get Sonos favorites

Use the sonos-favorite endpoint to retrieve your Sonos favorites including the URI. The HTTP method has to be GET:

http://<YOUR-HOST-IP-ADDRESS>:<apiPort>/sonos-favorites

The response is a JSON array containing all values:

[
    {
        "uri": "..."
        "title": "...",
        "artist": "...",
        "album": "..."
    },
    {
        "uri": "..."
        "title": "...",
        "artist": "...",
        "album": "..."
    },
    ...
]

The fields for artist and album may be null if it is a playlist.

Tips