1 """Config flow for the Google Cloud integration."""
3 from __future__
import annotations
7 from typing
import TYPE_CHECKING, Any, cast
9 from google.cloud
import texttospeech
10 import voluptuous
as vol
31 CONF_SERVICE_ACCOUNT_INFO,
39 from .helpers
import (
43 validate_service_account_info,
46 _LOGGER = logging.getLogger(__name__)
48 UPLOADED_KEY_FILE =
"uploaded_key_file"
50 STEP_USER_DATA_SCHEMA = vol.Schema(
60 """Handle a config flow for Google Cloud integration."""
64 _name: str |
None =
None
65 entry: ConfigEntry |
None =
None
66 abort_reason: str |
None =
None
69 """Read and parse an uploaded JSON file."""
71 contents = file_path.read_text()
72 return cast(dict[str, Any], json.loads(contents))
75 self, user_input: dict[str, Any] |
None =
None
76 ) -> ConfigFlowResult:
77 """Handle the initial step."""
78 errors: dict[str, Any] = {}
79 if user_input
is not None:
81 service_account_info = await self.hass.async_add_executor_job(
86 _LOGGER.exception(
"Reading uploaded JSON file failed")
87 errors[
"base"] =
"invalid_file"
89 data = {CONF_SERVICE_ACCOUNT_INFO: service_account_info}
92 assert self.abort_reason
94 self.entry, data=data, reason=self.abort_reason
99 data_schema=STEP_USER_DATA_SCHEMA,
101 description_placeholders={
102 "url":
"https://console.cloud.google.com/apis/credentials/serviceaccountkey"
107 """Import Google Cloud configuration from YAML."""
109 def _read_key_file() -> dict[str, Any]:
111 self.hass.config.path(import_data[CONF_KEY_FILE]), encoding=
"utf8"
113 return cast(dict[str, Any], json.load(f))
115 service_account_info = await self.hass.async_add_executor_job(_read_key_file)
119 _LOGGER.exception(
"Reading credentials JSON file failed")
124 options.pop(CONF_KEY_FILE)
125 _LOGGER.debug(
"Creating imported config entry with options: %s", options)
128 data={CONF_SERVICE_ACCOUNT_INFO: service_account_info},
135 config_entry: ConfigEntry,
136 ) -> GoogleCloudOptionsFlowHandler:
137 """Create the options flow."""
142 """Google Cloud options flow."""
145 self, user_input: dict[str, Any] |
None =
None
146 ) -> ConfigFlowResult:
147 """Manage the options."""
148 if user_input
is not None:
152 client: texttospeech.TextToSpeechAsyncClient = (
153 texttospeech.TextToSpeechAsyncClient.from_service_account_info(
165 default=DEFAULT_LANG,
168 mode=SelectSelectorMode.DROPDOWN, options=
list(voices)
176 default=DEFAULT_STT_MODEL,
179 mode=SelectSelectorMode.DROPDOWN,
180 options=SUPPORTED_STT_MODELS,
dict[str, Any] _parse_uploaded_file(self, str uploaded_file_id)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_import(self, dict[str, Any] import_data)
GoogleCloudOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
Iterator[Path] process_uploaded_file(HomeAssistant hass, str file_id)
dict[str, list[str]] async_tts_voices(texttospeech.TextToSpeechAsyncClient client)
vol.Schema tts_options_schema(Mapping[str, Any] config_options, dict[str, list[str]] voices, bool from_config_flow=False)
vol.Schema tts_platform_schema()
None validate_service_account_info(Mapping[str, str] info)
None open(self, **Any kwargs)