Quite a time ago I bought a Sonos Play:1 because I was super sure I can control this with my new Google Home Mini and this would be super fancy. Sadly I was negatively suprised :(. There wasn't and still isn't native support and even on google there were not that many options to work around this. I checked out if IFTTT is able to connect to my Sonos but no luck here. I read that Logitech Harmony products are able to control the Sonos players but this wasn't working as I expected and I keept searching. I found an option with the service Yonomi which seem to be similar to IFTTT but i didn't want to register for another not fitting service. Yesterday I was thinking about the way I searched and that I might find solutions which requires development skills and I found something. Actually Sonos players got an HTTP API and there is a NodeJS project out there which wraps this API in a simple to use gateway. I'll explain in the next articles how to setup the Sonos HTTP gateway, a Google Assistant gateway and the Dialogflow project to be able to talk to the google assistant something like "Ok Google, tell my Sonos to play Rodrieguez Jr."
Requirements
- NodeJS
- Git
- Docker (optional)
- PC or Raspberry PI
Setting up the HTTP API gateway
First of all you have to create a developer application for the service you're using. I'm using Spotify and went to their developer console to get a client and secret id. If you got those you have to clone the sonos http api project by jishi. This project aims to create a easy to use wrapper for the Sonos HTTP API.
Now you have to create a settings.json inside the cloned project to tell the API Gateway which port to use and which credentials to use for the target music service.
You either can run the application using node server.js or you do it like me and use Docker compose to start both services together
Here is my Dockerfile:
- FROM hypriot/rpi-node:latest
- RUN apt-get install curl
- COPY package.json ./package.json
- RUN npm i --production
- COPY lib ./lib
- COPY presets ./presets
- COPY server.js ./server.js
- COPY settings.js ./settings.js
- COPY settings.json ./settings.json
- COPY static ./static
- EXPOSE 5005
- HEALTHCHECK --interval=1m --timeout=2s \
- CMD curl -LSs http://localhost:5005 || exit 1
- CMD NODE_LOG_LEVEL=debug node server.js
Setting up the Google Assistant Gateway
To be able to receive the notifications from Google Assistant we need to provide a URL where Google can post his data to. This data will be consumed by the project home-sonos and will trigger the corresponding HTTP API of our Sonos wrapper service. To set this up you have to clone the repository
Then you need to configure this service and tell him how to call the Sonos HTTP wrapper, which music service to use (e.g. spotify) and whats the zone of the Sonos player. You can do this by modifying the ./common/settings.js
Also this service can be started by calling node server.js but as mentioned before I prefer the Docker compose tooling. So you can create a Dockerfile as well.
- FROM hypriot/rpi-node:latest
- RUN apt-get install curl
- COPY package.json ./package.json
- RUN npm i
- COPY agent ./agent
- COPY common ./common
- COPY lib ./lib
- COPY index.js ./index.js
- COPY server.js ./server.js
- EXPOSE 5005
- HEALTHCHECK --interval=1m --timeout=2s \
- CMD curl -LSs http://localhost:7000 || exit 1
- CMD NODE_LOG_LEVEL=debug PORT=7000 node server.js
As the last step you have to create a docker-compose.yaml to tell Docker how the services have to be started.
We're almost done Just start the docker container by running docker-compose up and you're ready to go.
To kill the service run docker-compose down.
Here you can find the next article which will explain how to create a DynDNS domain: Control Sonos with Google Assistant - DynDNS - Part 2