1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-22 02:47:14 +00:00
Files
core/tests/components/elevenlabs/test_config_flow.py
ehendrix23 d3a8f3191b Add Speech-to-Text (stt) to elevenlabs (#147838)
Co-authored-by: Norbert Rittel <norbert@rittel.de>
2025-10-10 17:01:22 +02:00

294 lines
8.2 KiB
Python

"""Test the ElevenLabs text-to-speech config flow."""
from unittest.mock import AsyncMock
import pytest
from homeassistant.components.elevenlabs.const import (
CONF_CONFIGURE_VOICE,
CONF_MODEL,
CONF_SIMILARITY,
CONF_STABILITY,
CONF_STT_AUTO_LANGUAGE,
CONF_STT_MODEL,
CONF_STYLE,
CONF_USE_SPEAKER_BOOST,
CONF_VOICE,
DEFAULT_SIMILARITY,
DEFAULT_STABILITY,
DEFAULT_STT_MODEL,
DEFAULT_STYLE,
DEFAULT_TTS_MODEL,
DEFAULT_USE_SPEAKER_BOOST,
DOMAIN,
)
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_API_KEY
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
async def test_user_step(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_async_client: AsyncMock,
) -> None:
"""Test user step create entry result."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert not result["errors"]
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_API_KEY: "api_key",
},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "ElevenLabs"
assert result["data"] == {
"api_key": "api_key",
}
assert result["options"] == {
CONF_MODEL: DEFAULT_TTS_MODEL,
CONF_VOICE: "voice1",
CONF_STT_MODEL: DEFAULT_STT_MODEL,
CONF_STT_AUTO_LANGUAGE: False,
}
mock_setup_entry.assert_called_once()
async def test_invalid_api_key(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_async_client_api_error: AsyncMock,
request: pytest.FixtureRequest,
) -> None:
"""Test user step with invalid api key."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert not result["errors"]
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_API_KEY: "api_key",
},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "invalid_api_key"}
mock_setup_entry.assert_not_called()
# Use a working client
request.getfixturevalue("mock_async_client")
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_API_KEY: "api_key",
},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "ElevenLabs"
assert result["data"] == {
"api_key": "api_key",
}
assert result["options"] == {
CONF_MODEL: DEFAULT_TTS_MODEL,
CONF_VOICE: "voice1",
CONF_STT_MODEL: DEFAULT_STT_MODEL,
CONF_STT_AUTO_LANGUAGE: False,
}
mock_setup_entry.assert_called_once()
async def test_voices_error(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_async_client_voices_error: AsyncMock,
request: pytest.FixtureRequest,
) -> None:
"""Test user step with invalid api key."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert not result["errors"]
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_API_KEY: "api_key",
},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "unknown"}
mock_setup_entry.assert_not_called()
# Use a working client
request.getfixturevalue("mock_async_client")
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_API_KEY: "api_key",
},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "ElevenLabs"
assert result["data"] == {
"api_key": "api_key",
}
assert result["options"] == {
CONF_MODEL: DEFAULT_TTS_MODEL,
CONF_VOICE: "voice1",
CONF_STT_MODEL: DEFAULT_STT_MODEL,
CONF_STT_AUTO_LANGUAGE: False,
}
mock_setup_entry.assert_called_once()
async def test_models_error(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_async_client_models_error: AsyncMock,
request: pytest.FixtureRequest,
) -> None:
"""Test user step with invalid api key."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert not result["errors"]
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_API_KEY: "api_key",
},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "unknown"}
mock_setup_entry.assert_not_called()
# Use a working client
request.getfixturevalue("mock_async_client")
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_API_KEY: "api_key",
},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "ElevenLabs"
assert result["data"] == {
"api_key": "api_key",
}
assert result["options"] == {
CONF_MODEL: DEFAULT_TTS_MODEL,
CONF_VOICE: "voice1",
CONF_STT_MODEL: DEFAULT_STT_MODEL,
CONF_STT_AUTO_LANGUAGE: False,
}
mock_setup_entry.assert_called_once()
async def test_options_flow_init(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_async_client: AsyncMock,
mock_entry: MockConfigEntry,
) -> None:
"""Test options flow init."""
mock_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(mock_entry.entry_id)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_MODEL: "model1",
CONF_VOICE: "voice1",
CONF_STT_MODEL: "scribe_v1_experimental",
CONF_STT_AUTO_LANGUAGE: True,
},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert mock_entry.options == {
CONF_MODEL: "model1",
CONF_VOICE: "voice1",
CONF_STT_MODEL: "scribe_v1_experimental",
CONF_STT_AUTO_LANGUAGE: True,
}
mock_setup_entry.assert_called_once()
async def test_options_flow_voice_settings_default(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_async_client: AsyncMock,
mock_entry: MockConfigEntry,
) -> None:
"""Test options flow voice settings."""
mock_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(mock_entry.entry_id)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_MODEL: "model1",
CONF_VOICE: "voice1",
CONF_STT_MODEL: "scribe_v1_experimental",
CONF_STT_AUTO_LANGUAGE: False,
CONF_CONFIGURE_VOICE: True,
},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "voice_settings"
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert mock_entry.options == {
CONF_MODEL: "model1",
CONF_VOICE: "voice1",
CONF_STT_MODEL: "scribe_v1_experimental",
CONF_STT_AUTO_LANGUAGE: False,
CONF_SIMILARITY: DEFAULT_SIMILARITY,
CONF_STABILITY: DEFAULT_STABILITY,
CONF_STYLE: DEFAULT_STYLE,
CONF_USE_SPEAKER_BOOST: DEFAULT_USE_SPEAKER_BOOST,
}