Add strict typing in Google Cloud (#125068)

This commit is contained in:
tronikos 2024-09-02 04:07:12 -07:00 committed by GitHub
parent 2ce6bd2378
commit f4a16c8dc9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 45 additions and 18 deletions

View File

@ -210,6 +210,7 @@ homeassistant.components.glances.*
homeassistant.components.goalzero.* homeassistant.components.goalzero.*
homeassistant.components.google.* homeassistant.components.google.*
homeassistant.components.google_assistant_sdk.* homeassistant.components.google_assistant_sdk.*
homeassistant.components.google_cloud.*
homeassistant.components.google_photos.* homeassistant.components.google_photos.*
homeassistant.components.google_sheets.* homeassistant.components.google_sheets.*
homeassistant.components.gpsd.* homeassistant.components.gpsd.*

View File

@ -4,7 +4,6 @@ from __future__ import annotations
import functools import functools
import operator import operator
from types import MappingProxyType
from typing import Any from typing import Any
from google.cloud import texttospeech from google.cloud import texttospeech
@ -51,8 +50,9 @@ async def async_tts_voices(
def tts_options_schema( 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 schema for TTS options with default values from config or constants."""
return vol.Schema( 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 schema for TTS platform."""
return vol.Schema( return vol.Schema(
{ {

View File

@ -2,6 +2,7 @@
import logging import logging
import os import os
from typing import Any, cast
from google.api_core.exceptions import GoogleAPIError from google.api_core.exceptions import GoogleAPIError
from google.cloud import texttospeech from google.cloud import texttospeech
@ -11,9 +12,11 @@ from homeassistant.components.tts import (
CONF_LANG, CONF_LANG,
PLATFORM_SCHEMA as TTS_PLATFORM_SCHEMA, PLATFORM_SCHEMA as TTS_PLATFORM_SCHEMA,
Provider, Provider,
TtsAudioType,
Voice, Voice,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import ( from .const import (
CONF_ENCODING, CONF_ENCODING,
@ -34,7 +37,11 @@ _LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = TTS_PLATFORM_SCHEMA.extend(tts_platform_schema().schema) 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.""" """Set up Google Cloud TTS component."""
if key_file := config.get(CONF_KEY_FILE): if key_file := config.get(CONF_KEY_FILE):
key_file = hass.config.path(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) _LOGGER.error("File %s doesn't exist", key_file)
return None return None
if key_file: if key_file:
client = texttospeech.TextToSpeechAsyncClient.from_service_account_json( client = texttospeech.TextToSpeechAsyncClient.from_service_account_file(
key_file key_file
) )
else: else:
@ -69,8 +76,8 @@ class GoogleCloudTTSProvider(Provider):
hass: HomeAssistant, hass: HomeAssistant,
client: texttospeech.TextToSpeechAsyncClient, client: texttospeech.TextToSpeechAsyncClient,
voices: dict[str, list[str]], voices: dict[str, list[str]],
language, language: str,
options_schema, options_schema: vol.Schema,
) -> None: ) -> None:
"""Init Google Cloud TTS service.""" """Init Google Cloud TTS service."""
self.hass = hass self.hass = hass
@ -81,24 +88,24 @@ class GoogleCloudTTSProvider(Provider):
self._options_schema = options_schema self._options_schema = options_schema
@property @property
def supported_languages(self): def supported_languages(self) -> list[str]:
"""Return list of supported languages.""" """Return a list of supported languages."""
return list(self._voices) return list(self._voices)
@property @property
def default_language(self): def default_language(self) -> str:
"""Return the default language.""" """Return the default language."""
return self._language return self._language
@property @property
def supported_options(self): def supported_options(self) -> list[str]:
"""Return a list of supported options.""" """Return a list of supported options."""
return [option.schema for option in self._options_schema.schema] return [option.schema for option in self._options_schema.schema]
@property @property
def default_options(self): def default_options(self) -> dict[str, Any]:
"""Return a dict including default options.""" """Return a dict including default options."""
return self._options_schema({}) return cast(dict[str, Any], self._options_schema({}))
@callback @callback
def async_get_supported_voices(self, language: str) -> list[Voice] | None: def async_get_supported_voices(self, language: str) -> list[Voice] | None:
@ -107,16 +114,25 @@ class GoogleCloudTTSProvider(Provider):
return None return None
return [Voice(voice, voice) for voice in voices] return [Voice(voice, voice) for voice in voices]
async def async_get_tts_audio(self, message, language, options): async def async_get_tts_audio(
"""Load TTS from google.""" self,
message: str,
language: str,
options: dict[str, Any],
) -> TtsAudioType:
"""Load TTS from Google Cloud."""
try: try:
options = self._options_schema(options) options = self._options_schema(options)
except vol.Invalid as err: except vol.Invalid as err:
_LOGGER.error("Error: %s when validating options: %s", err, options) _LOGGER.error("Error: %s when validating options: %s", err, options)
return None, None return None, None
encoding = texttospeech.AudioEncoding[options[CONF_ENCODING]] encoding: texttospeech.AudioEncoding = texttospeech.AudioEncoding[
gender = texttospeech.SsmlVoiceGender[options[CONF_GENDER]] options[CONF_ENCODING]
] # type: ignore[misc]
gender: texttospeech.SsmlVoiceGender | None = texttospeech.SsmlVoiceGender[
options[CONF_GENDER]
] # type: ignore[misc]
voice = options[CONF_VOICE] voice = options[CONF_VOICE]
if voice: if voice:
gender = None gender = None

View File

@ -1856,6 +1856,16 @@ disallow_untyped_defs = true
warn_return_any = true warn_return_any = true
warn_unreachable = 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.*] [mypy-homeassistant.components.google_photos.*]
check_untyped_defs = true check_untyped_defs = true
disallow_incomplete_defs = true disallow_incomplete_defs = true