core/homeassistant/components/demo/tts.py

55 lines
1.4 KiB
Python
Raw Normal View History

"""Support for the demo for text to speech service."""
import os
import voluptuous as vol
from homeassistant.components.tts import CONF_LANG, PLATFORM_SCHEMA, Provider
2019-07-31 19:25:30 +00:00
SUPPORT_LANGUAGES = ["en", "de"]
2019-07-31 19:25:30 +00:00
DEFAULT_LANG = "en"
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Optional(CONF_LANG, default=DEFAULT_LANG): vol.In(SUPPORT_LANGUAGES)}
)
def get_engine(hass, config, discovery_info=None):
"""Set up Demo speech component."""
2019-11-07 12:43:43 +00:00
return DemoProvider(config.get(CONF_LANG, DEFAULT_LANG))
class DemoProvider(Provider):
"""Demo speech API provider."""
def __init__(self, lang):
"""Initialize demo provider."""
self._lang = lang
2019-07-31 19:25:30 +00:00
self.name = "Demo"
@property
def default_language(self):
"""Return the default language."""
return self._lang
@property
def supported_languages(self):
"""Return list of supported languages."""
return SUPPORT_LANGUAGES
@property
def supported_options(self):
"""Return list of supported options like voice, emotionen."""
2019-07-31 19:25:30 +00:00
return ["voice", "age"]
def get_tts_audio(self, message, language, options=None):
"""Load TTS from demo."""
2019-07-31 19:25:30 +00:00
filename = os.path.join(os.path.dirname(__file__), "tts.mp3")
try:
2019-07-31 19:25:30 +00:00
with open(filename, "rb") as voice:
data = voice.read()
except OSError:
return (None, None)
2019-07-31 19:25:30 +00:00
return ("mp3", data)