spoken logfiles for your apps?
thats easy possible now.
let appdaemon tell you why the lights are turning on or if motion is detected. with just 2 lines of code.
want to create a playlist of music in between the messages? also possible with 2 lines.
here is the app:
###########################################################################################
#
# Rene Tode ( [email protected] )
#
# 2016/12/21 Germany
#
###########################################################################################
# you need to install gtts and mpg321
# that can be done by:
#
# sudo apt-get install gtts
# sudo apt-get install mpg321
#
# save this app in your app dir with name: sound.py
#
###########################################################################################
# in the appdaemon configfile you need to set:
#
# [soundfunctions]
# module = sound
# class = sound
# soundfilesdir = /an/empty!!/temp/file/dir/
#
###########################################################################################
# then you can use it like this in any app
#
# sound = self.get_app("soundfunctions")
# sound.say("Any text you like","your_language","your_priority")
#
# supported languages: * 'af' : 'Afrikaans'
# * 'sq' : 'Albanian'
# * 'ar' : 'Arabic'
# * 'hy' : 'Armenian'
# * 'bn' : 'Bengali'
# * 'ca' : 'Catalan'
# * 'zh' : 'Chinese'
# * 'zh-cn' : 'Chinese (Mandarin/China)'
# * 'zh-tw' : 'Chinese (Mandarin/Taiwan)'
# * 'zh-yue' : 'Chinese (Cantonese)'
# * 'hr' : 'Croatian'
# * 'cs' : 'Czech'
# * 'da' : 'Danish'
# * 'nl' : 'Dutch'
# * 'en' : 'English'
# * 'en-au' : 'English (Australia)'
# * 'en-uk' : 'English (United Kingdom)'
# * 'en-us' : 'English (United States)'
# * 'eo' : 'Esperanto'
# * 'fi' : 'Finnish'
# * 'fr' : 'French'
# * 'de' : 'German'
# * 'el' : 'Greek'
# * 'hi' : 'Hindi'
# * 'hu' : 'Hungarian'
# * 'is' : 'Icelandic'
# * 'id' : 'Indonesian'
# * 'it' : 'Italian'
# * 'ja' : 'Japanese'
# * 'ko' : 'Korean'
# * 'la' : 'Latin'
# * 'lv' : 'Latvian'
# * 'mk' : 'Macedonian'
# * 'no' : 'Norwegian'
# * 'pl' : 'Polish'
# * 'pt' : 'Portuguese'
# * 'pt-br' : 'Portuguese (Brazil)'
# * 'ro' : 'Romanian'
# * 'ru' : 'Russian'
# * 'sr' : 'Serbian'
# * 'sk' : 'Slovak'
# * 'es' : 'Spanish'
# * 'es-es' : 'Spanish (Spain)'
# * 'es-us' : 'Spanish (United States)'
# * 'sw' : 'Swahili'
# * 'sv' : 'Swedish'
# * 'ta' : 'Tamil'
# * 'th' : 'Thai'
# * 'tr' : 'Turkish'
# * 'vi' : 'Vietnamese'
# * 'cy' : 'Welsh'
#
# for priority give "1","2","3","4" or "5"
#
###########################################################################################
# you can also use:
#
# sound = self.get_app("soundfunctions")
# sound.playsound("any valid mp3 file","your_priority")
#
# to put music in your soundlist (or sounds)
#
###########################################################################################
import appdaemon.appapi as appapi
import datetime
import tempfile
import subprocess
import os
from gtts import gTTS
class sound(appapi.AppDaemon):
def initialize(self):
self.run_in(self.check_soundlist,30)
return
def check_soundlist(self, kwargs):
filelist = os.listdir(self.args["soundfilesdir"])
if filelist:
filelist = sorted(filelist)
for file in filelist:
priority = file[0]
tempfile = file[2]
fnamefile = open(self.args["soundfilesdir"] + file, 'r')
for line in fnamefile:
fname = line
self.play(fname)
if tempfile == "1":
os.remove(fname)
fnamefile.close()
os.remove(self.args["soundfilesdir"] + file)
self.run_in(self.check_soundlist,2)
return
self.run_in(self.check_soundlist,2)
return
def say(self,text,lang,priority):
with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as f:
fname = f.name
tts = gTTS(text=text, lang=lang)
tts.save(fname)
self. write_in_prioritylist(priority,fname,"1")
def playsound(self,file,priority):
self. write_in_prioritylist(priority,file,"2")
def play(self,filename):
cmd = ['mpg321',filename]
with tempfile.TemporaryFile() as f:
subprocess.call(cmd, stdout=f, stderr=f)
f.seek(0)
output = f.read()
def write_in_prioritylist(self,priority,file,tempfile):
try:
log = open(self.args["soundfilesdir"] + priority + "_" + tempfile + "_" + file[-10:], 'w')
log.write(file)
log.close()
except:
self.log("SOUNDFILEDIR PROBLEM!!")