mirror of
https://github.com/home-assistant/core.git
synced 2025-06-25 01:21:51 +02:00
Add strict typing in Google Cloud (#125068)
This commit is contained in:
@ -210,6 +210,7 @@ homeassistant.components.glances.*
|
||||
homeassistant.components.goalzero.*
|
||||
homeassistant.components.google.*
|
||||
homeassistant.components.google_assistant_sdk.*
|
||||
homeassistant.components.google_cloud.*
|
||||
homeassistant.components.google_photos.*
|
||||
homeassistant.components.google_sheets.*
|
||||
homeassistant.components.gpsd.*
|
||||
|
@ -4,7 +4,6 @@ from __future__ import annotations
|
||||
|
||||
import functools
|
||||
import operator
|
||||
from types import MappingProxyType
|
||||
from typing import Any
|
||||
|
||||
from google.cloud import texttospeech
|
||||
@ -51,8 +50,9 @@ async def async_tts_voices(
|
||||
|
||||
|
||||
def tts_options_schema(
|
||||
config_options: MappingProxyType[str, Any], voices: dict[str, list[str]]
|
||||
):
|
||||
config_options: dict[str, Any],
|
||||
voices: dict[str, list[str]],
|
||||
) -> vol.Schema:
|
||||
"""Return schema for TTS options with default values from config or constants."""
|
||||
return vol.Schema(
|
||||
{
|
||||
@ -152,7 +152,7 @@ def tts_options_schema(
|
||||
)
|
||||
|
||||
|
||||
def tts_platform_schema():
|
||||
def tts_platform_schema() -> vol.Schema:
|
||||
"""Return schema for TTS platform."""
|
||||
return vol.Schema(
|
||||
{
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
import logging
|
||||
import os
|
||||
from typing import Any, cast
|
||||
|
||||
from google.api_core.exceptions import GoogleAPIError
|
||||
from google.cloud import texttospeech
|
||||
@ -11,9 +12,11 @@ from homeassistant.components.tts import (
|
||||
CONF_LANG,
|
||||
PLATFORM_SCHEMA as TTS_PLATFORM_SCHEMA,
|
||||
Provider,
|
||||
TtsAudioType,
|
||||
Voice,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from .const import (
|
||||
CONF_ENCODING,
|
||||
@ -34,7 +37,11 @@ _LOGGER = logging.getLogger(__name__)
|
||||
PLATFORM_SCHEMA = TTS_PLATFORM_SCHEMA.extend(tts_platform_schema().schema)
|
||||
|
||||
|
||||
async def async_get_engine(hass, config, discovery_info=None):
|
||||
async def async_get_engine(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> Provider | None:
|
||||
"""Set up Google Cloud TTS component."""
|
||||
if key_file := config.get(CONF_KEY_FILE):
|
||||
key_file = hass.config.path(key_file)
|
||||
@ -42,7 +49,7 @@ async def async_get_engine(hass, config, discovery_info=None):
|
||||
_LOGGER.error("File %s doesn't exist", key_file)
|
||||
return None
|
||||
if key_file:
|
||||
client = texttospeech.TextToSpeechAsyncClient.from_service_account_json(
|
||||
client = texttospeech.TextToSpeechAsyncClient.from_service_account_file(
|
||||
key_file
|
||||
)
|
||||
else:
|
||||
@ -69,8 +76,8 @@ class GoogleCloudTTSProvider(Provider):
|
||||
hass: HomeAssistant,
|
||||
client: texttospeech.TextToSpeechAsyncClient,
|
||||
voices: dict[str, list[str]],
|
||||
language,
|
||||
options_schema,
|
||||
language: str,
|
||||
options_schema: vol.Schema,
|
||||
) -> None:
|
||||
"""Init Google Cloud TTS service."""
|
||||
self.hass = hass
|
||||
@ -81,24 +88,24 @@ class GoogleCloudTTSProvider(Provider):
|
||||
self._options_schema = options_schema
|
||||
|
||||
@property
|
||||
def supported_languages(self):
|
||||
"""Return list of supported languages."""
|
||||
def supported_languages(self) -> list[str]:
|
||||
"""Return a list of supported languages."""
|
||||
return list(self._voices)
|
||||
|
||||
@property
|
||||
def default_language(self):
|
||||
def default_language(self) -> str:
|
||||
"""Return the default language."""
|
||||
return self._language
|
||||
|
||||
@property
|
||||
def supported_options(self):
|
||||
def supported_options(self) -> list[str]:
|
||||
"""Return a list of supported options."""
|
||||
return [option.schema for option in self._options_schema.schema]
|
||||
|
||||
@property
|
||||
def default_options(self):
|
||||
def default_options(self) -> dict[str, Any]:
|
||||
"""Return a dict including default options."""
|
||||
return self._options_schema({})
|
||||
return cast(dict[str, Any], self._options_schema({}))
|
||||
|
||||
@callback
|
||||
def async_get_supported_voices(self, language: str) -> list[Voice] | None:
|
||||
@ -107,16 +114,25 @@ class GoogleCloudTTSProvider(Provider):
|
||||
return None
|
||||
return [Voice(voice, voice) for voice in voices]
|
||||
|
||||
async def async_get_tts_audio(self, message, language, options):
|
||||
"""Load TTS from google."""
|
||||
async def async_get_tts_audio(
|
||||
self,
|
||||
message: str,
|
||||
language: str,
|
||||
options: dict[str, Any],
|
||||
) -> TtsAudioType:
|
||||
"""Load TTS from Google Cloud."""
|
||||
try:
|
||||
options = self._options_schema(options)
|
||||
except vol.Invalid as err:
|
||||
_LOGGER.error("Error: %s when validating options: %s", err, options)
|
||||
return None, None
|
||||
|
||||
encoding = texttospeech.AudioEncoding[options[CONF_ENCODING]]
|
||||
gender = texttospeech.SsmlVoiceGender[options[CONF_GENDER]]
|
||||
encoding: texttospeech.AudioEncoding = texttospeech.AudioEncoding[
|
||||
options[CONF_ENCODING]
|
||||
] # type: ignore[misc]
|
||||
gender: texttospeech.SsmlVoiceGender | None = texttospeech.SsmlVoiceGender[
|
||||
options[CONF_GENDER]
|
||||
] # type: ignore[misc]
|
||||
voice = options[CONF_VOICE]
|
||||
if voice:
|
||||
gender = None
|
||||
|
10
mypy.ini
10
mypy.ini
@ -1856,6 +1856,16 @@ disallow_untyped_defs = true
|
||||
warn_return_any = true
|
||||
warn_unreachable = true
|
||||
|
||||
[mypy-homeassistant.components.google_cloud.*]
|
||||
check_untyped_defs = true
|
||||
disallow_incomplete_defs = true
|
||||
disallow_subclassing_any = true
|
||||
disallow_untyped_calls = true
|
||||
disallow_untyped_decorators = true
|
||||
disallow_untyped_defs = true
|
||||
warn_return_any = true
|
||||
warn_unreachable = true
|
||||
|
||||
[mypy-homeassistant.components.google_photos.*]
|
||||
check_untyped_defs = true
|
||||
disallow_incomplete_defs = true
|
||||
|
Reference in New Issue
Block a user