Hi Eairy, Not exactly what you asked, but will use aplay to play a local file. This was my very first python script; could use some updating!
# I wrote this script because I wanted my Home Assistant to be able to use my Raspberry Pi running Snips to play audio files for alerts though the Snips speakers
# This script only works with wav files, don't try an mp3 unless you want to hear a lot of static!
# This python script that will connect to the mosquitto eclipse broker used in Snips on a Raspberry Pi
# The script listens on the mosquitto topic "ha/playaudio/# and if it receives a message containing the full path to wav file will play that wav file
# The mosquitto.conf file on the Raspberry Pi running Snips needs to be modified to include the ha/playaudio topic with the "in" direction as shown below:
# topic ha/playaudio/# in
# On a Raspberry Pi with Debian Stretch installed, the mosquitto.conf file is located in /etc/mosquitto
# To use this script with Home Assistant, have Home Assistant publish a message on ha/playaudio/ with the path to the wav file as the message
# Here's an example of an automation that will play the Front_Center.wav file located in /usr/share/sounds/alsa/ directory. (part of the standard Raspberry Pi install)
#- id: '6535824662089'
# alias: Reminder
# - at: '09:10:05'
# platform: time
# - service: mqtt.publish
# "topic": "ha/playaudio/",
# "payload": "/usr/share/sounds/alsa/Front_Center.wav"
# Be careful of the spacing when creating automations, Home Assistant is very picky. A copy and paste of the above automation into the automation.yaml should work after all of the "#"'s are removed
# Depending on where you locate this file (I have it in /use/share/) and how you create it, you may need to modify its properties to run it
# Finally, there isn't any santization or checking of the message before it is run. This can cause bad juju if a nefarious party wanted to try something, so use at your own risk!
# !/usr/bin/env python
# encoding: utf-8
from __future__ import print_function #brings the print function from Python 3 into Python 2.6+, only necessary for debugging, and has to be first line in program
import paho.mqtt.client as mqtt #import the client, paho.mqtt.client is not installed as part of standard Snips install, if you need to install: pip install paho-mqtt
def on_connect(client, userdata, flags, rc):
# print("Connected with result code " + str(rc)) # for debugging, uncomment to check if connected, should be result code 0
def on_message(client, userdata, message):
print("message received " ,str(message.payload.decode("utf-8"))) # for debugging, uncomment to check message
# print("message topic=",message.topic) # for debugging, uncomment to check topic
# print("message qos=",message.qos) # for debugging, uncomment to check quality of service
# print("message retain flag=",message.retain) # for debugging, uncomment to check retained message flag
wavefile = ''
wavefile = str(message.payload.decode("utf-8"))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("XXX.XXX.XXX.XXX", 1883, 60)