Control Sonos and play a specific radio channel [Solved]
I’ve been doing some searching but have thus far not been able to solve (2) and (3). Hoping that someone here has been successful! Will store the solution for (1) in case someone else is interested.
1. Control Sonos in an automation-event
automation:
alias: "Turn of music when I leave home"
trigger:
platform: state
entity_id: device_tracker.iphone
from: 'home'
to: 'not_home'
action:
service: media_player.turn_off
2. Control Sonos in a scene [Solved]
Here, I would like to set a dinner-scene which starts playing the Sonos and possibly runs some other stuff too. Trying to get the Sonos to work first though. The code below has been updated based on the solution provided by @martinhjelmare below and works on my configuration.
scene:
- name: Dinner
entities:
media_player.kitchen:
state: playing
3. Control Sonos and play a specific radio channel [Solved]
This is my current configuration, which unfortunately renders an error. I’m trying too play a specific playlist (tunein radio channel). Does anyone have a clever solution for this?
Please post your configs within code block markdown tags. That’s three back-ticks, newline, code, newline, three back-tics. It’s hard to troubleshoot format and syntax otherwise.
For number two, I suspect you have a syntax issue, perhaps indentation. State playing should be supported.
Thanks for pointing out how to comment configs, I didn’t know what syntax to use to make the code more readable.
I looked through the state.py-file and noted that playing was one of the allowed states, hence my trying it. Tried to remove the indent in the config-file as well as put playing within single quotes and typing it (without and without quotes) in large caps. None of these steps worked. Any other ideas on what I could try?
I was now able to solve the third problem too. Will update the code above accordingly. Still struggling with getting Sonos playlists to work but will look into it more tomorrow.
I had a similar issue getting sonos playlists to work, ended up resorting to using the sonos http api Component from this post http://www.makeuseof.com/tag/send-voice-notifications-sonos-speakers/ I was then able to use a script using curl to trigger playing the playlist via http://Pi_ip_address/sonos_device/playlist/playlist_name. There are probably a number of ways to achieve this, but this works for me right now!
Can you go into more detail on how you accomplished this, i.e. playing a specific sonos favorites station? I am a home assistant noob as I just started with it a short while ago.
I’ve now made a very simple Python script that queries the sonos http api for playlists and outputs them into a yaml-file that is picked up by Home Assistant. Python script below and rest of config (ie automation to start playing the playlist when it’s changed) is available on github.
Thanks,
Patrik
#!/usr/bin/python
import json, sys, urllib
# LOAD PLAYLIST-LIST IN JSON-FORMAT
url = "http://192.168.0.140:5005/kitchen/playlists"
response = urllib.urlopen(url)
data = json.loads(response.read())
f = open("/home/hass/.homeassistant/extraconfig/input_select/sonos_playlists.yaml","w")
f.write("sonos_playlist:" + "\n")
f.write(" name: Playlist" + "\n")
f.write(" options:" + "\n")
for key in data:
f.write(" - '" + key + "'\n")
f.write(" icon: 'mdi:playlist-check'" + "\n")
f.close()
## Shell commands
f = open("/home/hass/.homeassistant/extraconfig/shell_command/sonos_playlist.yaml","w")
for key in data:
keyname = ''.join(e for e in key if e.isalnum()).lower()
keyvalue = urllib.quote(key)
f.write("sonos_playlist_" + keyname + ": '/usr/bin/curl +X http://192.168.0.140:5005/kitchen/playlist/" + keyvalue + "'\n")
f.close()
@pautomate very nice, great example!! I ended up following your lead but used the SoCo library that HA already uses and straight automation. Mine looks like so:
One thing I haven’t tested yet is whether this plays Pandora favorites correctly as I notices in the http_api source you used they have an option for credentials and I wonder if there needs to be some type of authentication going on… I’ll investigate later tonight and update as I find out more.
Playing pandora does result in an error because SoCo requires the ‘meta’ (at least a title per the docs). When I run the command manually from a .py file it starts the Pandora station just fine.