Modified box.py to call haplaylist shell script

Box.py now calls haplaylist.sh and passes the plist variable. The haplaylist.sh calls a curl command to a home assistant instance running on another machine and passes the playlist variable to an input_text component. Also included is the musiccards.service for systemd to start box.py automatically on boot.
This commit is contained in:
hoveeman
2018-03-25 16:43:55 -04:00
committed by GitHub
parent 7852a100b8
commit 144fd273c8
3 changed files with 24 additions and 31 deletions

35
box.py
View File

@@ -1,34 +1,11 @@
#!/usr/bin/env python
from mpd import MPDClient
#from readtest import *
import re
from CardList import CardList
from Reader import Reader
import sys
def connectMPD():
try:
client = MPDClient() # create client object
client.timeout = 200 # network timeout in seconds (floats allowed), default: None
client.idletimeout = None
print "Connecting..."
client.connect("localhost", 6600)
print "Connected!"
return client
except:
print 'Could not connect to MPD server'
def play(client, plist):
try:
client.stop()
client.clear()
client.add(plist)
if re.search('playlist',plist):
client.shuffle()
client.play()
except:
print 'Could not play playlist %s' % plist
import subprocess
import shlex
reader = Reader()
cardList = CardList()
@@ -42,13 +19,9 @@ while True:
plist = cardList.getPlaylist(card)
print 'Playlist', plist
if plist != '':
client = connectMPD()
if plist=='pause':
client.pause()
else:
play(client, plist)
client.close()
subprocess.check_call( ["./haplaylist.sh %s" % plist], shell=True)
except KeyboardInterrupt:
sys.exit(0)
except:
pass

4
haplaylist.sh Normal file
View File

@@ -0,0 +1,4 @@
#!/bin/bash
playlist=$1
curl --silent --output /dev/null -X POST -H "x-ha-access: HOMEASSISTANTPASSWORD" -H "Content-Type: application/json" -d '{"entity_id": "input_text.playlist", "value": "'"$playlist"'"}' http://10.0.0.26:8123/api/services/input_text/set_value

16
musiccards.service Normal file
View File

@@ -0,0 +1,16 @@
[Unit]
Description=MusicCards
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/home/pi/music-cards
ExecStart=/usr/bin/python -u /home/pi/music-cards/box.py
Restart=on-failure
RestartSec=10
KillMode=process
[Install]
WantedBy=multi-user.target