Skip to content

Commit

Permalink
Add support for legacy voices (#141)
Browse files Browse the repository at this point in the history
* Add support for legacy voices

* Fix test
  • Loading branch information
carleeno authored Jul 24, 2024
1 parent 6f312ab commit 8505616
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions custom_components/elevenlabs_tts/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@
DEFAULT_STYLE = 0.2
CONF_USE_SPEAKER_BOOST = "use_speaker_boost"
DEFAULT_USE_SPEAKER_BOOST = True

LEGACY_VOICE_SUFFIX = " (Legacy)"
11 changes: 9 additions & 2 deletions custom_components/elevenlabs_tts/elevenlabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
DEFAULT_STYLE,
DEFAULT_USE_SPEAKER_BOOST,
DEFAULT_VOICE,
LEGACY_VOICE_SUFFIX,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -92,20 +93,26 @@ async def post(

async def get_voices(self) -> dict:
"""Get voices from the API."""
endpoint = "voices"
endpoint = "voices?show_legacy=true"
voices = await self.get(endpoint)
self._voices = voices.get("voices", [])

self.voices = []

for voice in self._voices:
new_voice = Voice(voice_id=voice["voice_id"], name=voice["name"])
if voice.get("is_legacy"):
name = voice["name"] + LEGACY_VOICE_SUFFIX
else:
name = voice["name"]
new_voice = Voice(voice_id=voice["voice_id"], name=name)
self.voices.append(new_voice)

return self._voices

async def get_voice_by_name_or_id(self, identifier: str) -> dict:
"""Get a voice by its name or ID."""
# Remove potential legacy suffix from identifier
identifier = identifier.replace(LEGACY_VOICE_SUFFIX, "")
_LOGGER.debug("Looking for voice with identifier %s", identifier)
for voice in self._voices:
if voice["name"] == identifier or voice["voice_id"] == identifier:
Expand Down
5 changes: 4 additions & 1 deletion tests/test_elevenlabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ async def test_get_voices(client):
assert voices == mock_response["voices"]

# Assert that the request was made with the correct URL and headers
assert respx.calls[0].request.url == "https://api.elevenlabs.io/v1/voices"
assert (
respx.calls[0].request.url
== "https://api.elevenlabs.io/v1/voices?show_legacy=true"
)
assert respx.calls[0].request.headers["xi-api-key"] == client._api_key

# Assert that the client's _voices attribute is updated correctly
Expand Down

0 comments on commit 8505616

Please sign in to comment.