diff --git a/src/data/cloud/tts.ts b/src/data/cloud/tts.ts index caffc8bb97..e38c65c911 100644 --- a/src/data/cloud/tts.ts +++ b/src/data/cloud/tts.ts @@ -1,5 +1,4 @@ import { caseInsensitiveStringCompare } from "../../common/string/compare"; -import { LocalizeFunc } from "../../common/translations/localize"; import { HomeAssistant } from "../../types"; export interface CloudTTSInfo { @@ -27,27 +26,21 @@ export const getCloudTtsLanguages = (info?: CloudTTSInfo) => { return languages; }; -export const getCloudTtsSupportedGenders = ( +export const getCloudTtsSupportedVoices = ( language: string, - info: CloudTTSInfo | undefined, - localize: LocalizeFunc + info: CloudTTSInfo | undefined ) => { - const genders: Array<[string, string]> = []; + const voices: Array = []; if (!info) { - return genders; + return voices; } - for (const [curLang, gender] of info.languages) { + for (const [curLang, voice] of info.languages) { if (curLang === language) { - genders.push([ - gender, - gender === "male" || gender === "female" - ? localize(`ui.components.media-browser.tts.gender_${gender}`) - : gender, - ]); + voices.push(voice); } } - return genders.sort((a, b) => caseInsensitiveStringCompare(a[1], b[1])); + return voices.sort((a, b) => caseInsensitiveStringCompare(a, b)); }; diff --git a/src/panels/config/cloud/account/cloud-tts-pref.ts b/src/panels/config/cloud/account/cloud-tts-pref.ts index c1e85fccdc..fe716b9a5d 100644 --- a/src/panels/config/cloud/account/cloud-tts-pref.ts +++ b/src/panels/config/cloud/account/cloud-tts-pref.ts @@ -14,7 +14,7 @@ import { CloudTTSInfo, getCloudTTSInfo, getCloudTtsLanguages, - getCloudTtsSupportedGenders, + getCloudTtsSupportedVoices, } from "../../../../data/cloud/tts"; import { showAlertDialog } from "../../../../dialogs/generic/show-dialog-box"; import type { HomeAssistant } from "../../../../types"; @@ -37,11 +37,7 @@ export class CloudTTSPref extends LitElement { const languages = this.getLanguages(this.ttsInfo); const defaultVoice = this.cloudStatus.prefs.tts_default_voice; - const genders = this.getSupportedGenders( - defaultVoice[0], - this.ttsInfo, - this.hass.localize - ); + const voices = this.getSupportedVoices(defaultVoice[0], this.ttsInfo); return html` - ${genders.map( - ([key, label]) => - html`${label}` + ${voices.map( + (voice) => + html`${voice}` )} @@ -90,6 +86,16 @@ export class CloudTTSPref extends LitElement { `; } + protected updated(changedProps) { + if ( + changedProps.has("cloudStatus") && + this.cloudStatus?.prefs.tts_default_voice?.[0] !== + changedProps.get("cloudStatus")?.prefs.tts_default_voice?.[0] + ) { + this.renderRoot.querySelector("ha-select")?.layoutOptions(); + } + } + protected willUpdate(changedProps) { super.willUpdate(changedProps); if (!this.hasUpdated) { @@ -104,7 +110,7 @@ export class CloudTTSPref extends LitElement { private getLanguages = memoizeOne(getCloudTtsLanguages); - private getSupportedGenders = memoizeOne(getCloudTtsSupportedGenders); + private getSupportedVoices = memoizeOne(getCloudTtsSupportedVoices); private _openTryDialog() { showTryTtsDialog(this, { @@ -119,19 +125,15 @@ export class CloudTTSPref extends LitElement { this.savingPreferences = true; const language = ev.detail.value; - const curGender = this.cloudStatus!.prefs.tts_default_voice[1]; - const genders = this.getSupportedGenders( - language, - this.ttsInfo, - this.hass.localize - ); - const newGender = genders.find((item) => item[0] === curGender) - ? curGender - : genders[0][0]; + const curVoice = this.cloudStatus!.prefs.tts_default_voice[1]; + const voices = this.getSupportedVoices(language, this.ttsInfo); + const newVoice = voices.find((item) => item === curVoice) + ? curVoice + : voices[0]; try { await updateCloudPref(this.hass, { - tts_default_voice: [language, newGender], + tts_default_voice: [language, newVoice], }); fireEvent(this, "ha-refresh-cloud-status"); } catch (err: any) { @@ -145,17 +147,17 @@ export class CloudTTSPref extends LitElement { } } - async _handleGenderChange(ev) { + async _handleVoiceChange(ev) { if (ev.target.value === this.cloudStatus!.prefs.tts_default_voice[1]) { return; } this.savingPreferences = true; const language = this.cloudStatus!.prefs.tts_default_voice[0]; - const gender = ev.target.value; + const voice = ev.target.value; try { await updateCloudPref(this.hass, { - tts_default_voice: [language, gender], + tts_default_voice: [language, voice], }); fireEvent(this, "ha-refresh-cloud-status"); } catch (err: any) { @@ -163,7 +165,7 @@ export class CloudTTSPref extends LitElement { // eslint-disable-next-line no-console console.error(err); showAlertDialog(this, { - text: `Unable to save default gender. ${err}`, + text: `Unable to save default voice. ${err}`, warning: true, }); } diff --git a/src/panels/config/cloud/account/dialog-cloud-tts-try.ts b/src/panels/config/cloud/account/dialog-cloud-tts-try.ts index 876bce4eab..bf5b8632d7 100644 --- a/src/panels/config/cloud/account/dialog-cloud-tts-try.ts +++ b/src/panels/config/cloud/account/dialog-cloud-tts-try.ts @@ -184,7 +184,7 @@ export class DialogTryTts extends LitElement { this._loadingExample = true; const language = this._params!.defaultVoice[0]; - const gender = this._params!.defaultVoice[1]; + const voice = this._params!.defaultVoice[1]; let url; try { @@ -192,7 +192,7 @@ export class DialogTryTts extends LitElement { platform: "cloud", message, language, - options: { gender }, + options: { voice }, }); url = result.path; } catch (err: any) { diff --git a/src/translations/en.json b/src/translations/en.json index 1fa1aa6875..c5fd82a529 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -3524,7 +3524,7 @@ "title": "Text-to-speech", "info": "Bring personality to your home by having it speak to you by using our text-to-speech services. You can use this in automations and scripts by using the {service} service.", "default_language": "Default language to use", - "default_gender": "Default gender to use", + "default_voice": "Default voice to use", "try": "Try", "dialog": { "header": "Try text-to-speech",