Add additional support over NC (#28527)
* Add voice support over NC * Add disocery support for TTS / STT * fix cloud TTS discovery * Fix dev config * Fix discovery * Bump hass-nabucasa 0.25 * Add channel support * Fix lint * Update homeassistant/components/cloud/__init__.py Co-Authored-By: Paulus Schoutsen <balloob@gmail.com> * Update homeassistant/components/cloud/tts.py Co-Authored-By: Paulus Schoutsen <balloob@gmail.com> * Update homeassistant/components/cloud/tts.py Co-Authored-By: Paulus Schoutsen <balloob@gmail.com> * Update homeassistant/components/cloud/tts.py Co-Authored-By: Paulus Schoutsen <balloob@gmail.com> * bump hass-nabucasa * Update tts.py * fix lint
This commit is contained in:
1 parent
925e26b061
commit
005a1b2713
25 files changed
+282
-38
No files matched your search
@@ -23,6 +23,7 @@ from homeassistant.util.aiohttp import MockRequest
|
||||
from . import account_link, http_api
|
||||
from .client import CloudClient
|
||||
from .const import (
|
||||
CONF_ACCOUNT_LINK_URL,
|
||||
CONF_ACME_DIRECTORY_SERVER,
|
||||
CONF_ALEXA,
|
||||
CONF_ALEXA_ACCESS_TOKEN_URL,
|
||||
@@ -38,7 +39,7 @@ from .const import (
|
||||
CONF_REMOTE_API_URL,
|
||||
CONF_SUBSCRIPTION_INFO_URL,
|
||||
CONF_USER_POOL_ID,
|
||||
CONF_ACCOUNT_LINK_URL,
|
||||
CONF_VOICE_API_URL,
|
||||
DOMAIN,
|
||||
MODE_DEV,
|
||||
MODE_PROD,
|
||||
@@ -103,6 +104,7 @@ CONFIG_SCHEMA = vol.Schema(
|
||||
vol.Optional(CONF_ALEXA_ACCESS_TOKEN_URL): vol.Url(),
|
||||
vol.Optional(CONF_GOOGLE_ACTIONS_REPORT_STATE_URL): vol.Url(),
|
||||
vol.Optional(CONF_ACCOUNT_LINK_URL): vol.Url(),
|
||||
vol.Optional(CONF_VOICE_API_URL): vol.Url(),
|
||||
}
|
||||
)
|
||||
},
|
||||
@@ -230,21 +232,28 @@ async def async_setup(hass, config):
|
||||
DOMAIN, SERVICE_REMOTE_DISCONNECT, _service_handler
|
||||
)
|
||||
|
||||
loaded_binary_sensor = False
|
||||
loaded = False
|
||||
|
||||
async def _on_connect():
|
||||
"""Discover RemoteUI binary sensor."""
|
||||
nonlocal loaded_binary_sensor
|
||||
nonlocal loaded
|
||||
|
||||
if loaded_binary_sensor:
|
||||
# Prevent multiple discovery
|
||||
if loaded:
|
||||
return
|
||||
loaded = True
|
||||
|
||||
loaded_binary_sensor = True
|
||||
hass.async_create_task(
|
||||
hass.helpers.discovery.async_load_platform(
|
||||
"binary_sensor", DOMAIN, {}, config
|
||||
)
|
||||
)
|
||||
hass.async_create_task(
|
||||
hass.helpers.discovery.async_load_platform("stt", DOMAIN, {}, config)
|
||||
)
|
||||
hass.async_create_task(
|
||||
hass.helpers.discovery.async_load_platform("tts", DOMAIN, {}, config)
|
||||
)
|
||||
|
||||
cloud.iot.register_on_connect(_on_connect)
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@ CONF_ACME_DIRECTORY_SERVER = "acme_directory_server"
|
||||
CONF_ALEXA_ACCESS_TOKEN_URL = "alexa_access_token_url"
|
||||
CONF_GOOGLE_ACTIONS_REPORT_STATE_URL = "google_actions_report_state_url"
|
||||
CONF_ACCOUNT_LINK_URL = "account_link_url"
|
||||
CONF_VOICE_API_URL = "voice_api_url"
|
||||
|
||||
MODE_DEV = "development"
|
||||
MODE_PROD = "production"
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"domain": "cloud",
|
||||
"name": "Cloud",
|
||||
"documentation": "https://www.home-assistant.io/integrations/cloud",
|
||||
"requirements": ["hass-nabucasa==0.23"],
|
||||
"requirements": ["hass-nabucasa==0.26"],
|
||||
"dependencies": ["http", "webhook"],
|
||||
"codeowners": ["@home-assistant/cloud"]
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
"""Support for the cloud for speech to text service."""
|
||||
from typing import List
|
||||
|
||||
from aiohttp import StreamReader
|
||||
from hass_nabucasa import Cloud
|
||||
from hass_nabucasa.voice import VoiceError
|
||||
|
||||
from homeassistant.components.stt import Provider, SpeechMetadata, SpeechResult
|
||||
from homeassistant.components.stt.const import (
|
||||
AudioBitRates,
|
||||
AudioChannels,
|
||||
AudioCodecs,
|
||||
AudioFormats,
|
||||
AudioSampleRates,
|
||||
SpeechResultState,
|
||||
)
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
SUPPORT_LANGUAGES = [
|
||||
"da-DK",
|
||||
"de-DE",
|
||||
"en-AU",
|
||||
"en-CA",
|
||||
"en-GB",
|
||||
"en-US",
|
||||
"es-ES",
|
||||
"fi-FI",
|
||||
"fr-CA",
|
||||
"fr-FR",
|
||||
"it-IT",
|
||||
"ja-JP",
|
||||
"nl-NL",
|
||||
"pl-PL",
|
||||
"pt-PT",
|
||||
"ru-RU",
|
||||
"sv-SE",
|
||||
"th-TH",
|
||||
"zh-CN",
|
||||
"zh-HK",
|
||||
]
|
||||
|
||||
|
||||
async def async_get_engine(hass, config, discovery_info=None):
|
||||
"""Set up Cloud speech component."""
|
||||
cloud: Cloud = hass.data[DOMAIN]
|
||||
|
||||
return CloudProvider(cloud)
|
||||
|
||||
|
||||
class CloudProvider(Provider):
|
||||
"""NabuCasa speech API provider."""
|
||||
|
||||
def __init__(self, cloud: Cloud) -> None:
|
||||
"""Hass NabuCasa Speech to text."""
|
||||
self.cloud = cloud
|
||||
|
||||
@property
|
||||
def supported_languages(self) -> List[str]:
|
||||
"""Return a list of supported languages."""
|
||||
return SUPPORT_LANGUAGES
|
||||
|
||||
@property
|
||||
def supported_formats(self) -> List[AudioFormats]:
|
||||
"""Return a list of supported formats."""
|
||||
return [AudioFormats.WAV, AudioFormats.OGG]
|
||||
|
||||
@property
|
||||
def supported_codecs(self) -> List[AudioCodecs]:
|
||||
"""Return a list of supported codecs."""
|
||||
return [AudioCodecs.PCM, AudioCodecs.OPUS]
|
||||
|
||||
@property
|
||||
def supported_bit_rates(self) -> List[AudioBitRates]:
|
||||
"""Return a list of supported bitrates."""
|
||||
return [AudioBitRates.BITRATE_16]
|
||||
|
||||
@property
|
||||
def supported_sample_rates(self) -> List[AudioSampleRates]:
|
||||
"""Return a list of supported samplerates."""
|
||||
return [AudioSampleRates.SAMPLERATE_16000]
|
||||
|
||||
@property
|
||||
def supported_channels(self) -> List[AudioChannels]:
|
||||
"""Return a list of supported channels."""
|
||||
return [AudioChannels.CHANNEL_MONO]
|
||||
|
||||
async def async_process_audio_stream(
|
||||
self, metadata: SpeechMetadata, stream: StreamReader
|
||||
) -> SpeechResult:
|
||||
"""Process an audio stream to STT service."""
|
||||
content = f"audio/{metadata.format!s}; codecs=audio/{metadata.codec!s}; samplerate=16000"
|
||||
|
||||
# Process STT
|
||||
try:
|
||||
result = await self.cloud.voice.process_stt(
|
||||
stream, content, metadata.language
|
||||
)
|
||||
except VoiceError:
|
||||
return SpeechResult(None, SpeechResultState.ERROR)
|
||||
|
||||
# Return Speech as Text
|
||||
return SpeechResult(
|
||||
result.text,
|
||||
SpeechResultState.SUCCESS if result.success else SpeechResultState.ERROR,
|
||||
)
|
||||
@@ -0,0 +1,81 @@
|
||||
"""Support for the cloud for text to speech service."""
|
||||
|
||||
from hass_nabucasa.voice import VoiceError
|
||||
from hass_nabucasa import Cloud
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.tts import CONF_LANG, PLATFORM_SCHEMA, Provider
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
CONF_GENDER = "gender"
|
||||
|
||||
SUPPORT_LANGUAGES = ["en-US", "de-DE", "es-ES"]
|
||||
SUPPORT_GENDER = ["male", "female"]
|
||||
|
||||
DEFAULT_LANG = "en-US"
|
||||
DEFAULT_GENDER = "female"
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Optional(CONF_LANG, default=DEFAULT_LANG): vol.In(SUPPORT_LANGUAGES),
|
||||
vol.Optional(CONF_GENDER, default=DEFAULT_GENDER): vol.In(SUPPORT_GENDER),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_get_engine(hass, config, discovery_info=None):
|
||||
"""Set up Cloud speech component."""
|
||||
cloud: Cloud = hass.data[DOMAIN]
|
||||
|
||||
if discovery_info is not None:
|
||||
language = DEFAULT_LANG
|
||||
gender = DEFAULT_GENDER
|
||||
else:
|
||||
language = config[CONF_LANG]
|
||||
gender = config[CONF_GENDER]
|
||||
|
||||
return CloudProvider(cloud, language, gender)
|
||||
|
||||
|
||||
class CloudProvider(Provider):
|
||||
"""NabuCasa Cloud speech API provider."""
|
||||
|
||||
def __init__(self, cloud: Cloud, language: str, gender: str):
|
||||
"""Initialize cloud provider."""
|
||||
self.cloud = cloud
|
||||
self.name = "Cloud"
|
||||
self._language = language
|
||||
self._gender = gender
|
||||
|
||||
@property
|
||||
def default_language(self):
|
||||
"""Return the default language."""
|
||||
return self._language
|
||||
|
||||
@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, emotion."""
|
||||
return [CONF_GENDER]
|
||||
|
||||
@property
|
||||
def default_options(self):
|
||||
"""Return a dict include default options."""
|
||||
return {CONF_GENDER: self._gender}
|
||||
|
||||
async def async_get_tts_audio(self, message, language, options=None):
|
||||
"""Load TTS from NabuCasa Cloud."""
|
||||
# Process TTS
|
||||
try:
|
||||
data = await self.cloud.voice.process_tts(
|
||||
message, language, gender=options[CONF_GENDER]
|
||||
)
|
||||
except VoiceError:
|
||||
return (None, None)
|
||||
|
||||
return ("mp3", data)
|
||||
Reference in new issue
Block a user