From 9f65b8fef50ce9ebfdb963d4bad6eac32631bee1 Mon Sep 17 00:00:00 2001 From: doudz Date: Wed, 4 Jan 2017 22:05:41 +0100 Subject: [PATCH] add offline tts using pico (#5005) * add offline tts using pico * Update picotts.py * Update picotts.py * Update picotts.py * Update picotts.py * Update picotts.py * Update picotts.py * Update picotts.py * Update .coveragerc --- .coveragerc | 1 + homeassistant/components/tts/picotts.py | 57 +++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 homeassistant/components/tts/picotts.py diff --git a/.coveragerc b/.coveragerc index bd0376ee87a..38bea01387a 100644 --- a/.coveragerc +++ b/.coveragerc @@ -345,6 +345,7 @@ omit = homeassistant/components/switch/transmission.py homeassistant/components/switch/wake_on_lan.py homeassistant/components/thingspeak.py + homeassistant/components/tts/picotts.py homeassistant/components/upnp.py homeassistant/components/weather/openweathermap.py homeassistant/components/zeroconf.py diff --git a/homeassistant/components/tts/picotts.py b/homeassistant/components/tts/picotts.py new file mode 100644 index 00000000000..366973813a2 --- /dev/null +++ b/homeassistant/components/tts/picotts.py @@ -0,0 +1,57 @@ +""" +Support for the picotts speech service. + +For more details about this component, please refer to the documentation at +https://home-assistant.io/components/tts/picotts/ +""" +import os +import tempfile +import shutil +import subprocess +import logging +import voluptuous as vol + +from homeassistant.components.tts import Provider, PLATFORM_SCHEMA, CONF_LANG + +_LOGGER = logging.getLogger(__name__) + +SUPPORT_LANGUAGES = ['en-US', 'en-GB', 'de-DE', 'es-ES', 'fr-FR', 'it-IT'] + +DEFAULT_LANG = 'en-US' + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Optional(CONF_LANG, default=DEFAULT_LANG): vol.In(SUPPORT_LANGUAGES), +}) + + +def get_engine(hass, config): + """Setup pico speech component.""" + if shutil.which("pico2wave") is None: + _LOGGER.error("'pico2wave' was not found") + return False + return PicoProvider() + + +class PicoProvider(Provider): + """pico speech api provider.""" + + def get_tts_audio(self, message, language=None): + """Load TTS using pico2wave.""" + if language not in SUPPORT_LANGUAGES: + language = self.language + with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmpf: + fname = tmpf.name + cmd = ['pico2wave', '--wave', fname, '-l', language, message] + subprocess.call(cmd) + data = None + try: + with open(fname, 'rb') as voice: + data = voice.read() + except OSError: + _LOGGER.error("Error trying to read %s", fname) + return (None, None) + finally: + os.remove(fname) + if data: + return ("wav", data) + return (None, None)