Skip to content

Commit

Permalink
Add tts get engine ws command (#92336)
Browse files Browse the repository at this point in the history
Co-authored-by: Erik Montnemery <erik@montnemery.com>
  • Loading branch information
bramkragten and emontnemery committed May 11, 2023
1 parent 4568207 commit 75f8ea4
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
42 changes: 42 additions & 0 deletions homeassistant/components/tts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def async_get_text_to_speech_languages(hass: HomeAssistant) -> set[str]:
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up TTS."""
websocket_api.async_register_command(hass, websocket_list_engines)
websocket_api.async_register_command(hass, websocket_get_engine)
websocket_api.async_register_command(hass, websocket_list_engine_voices)

# Legacy config options
Expand Down Expand Up @@ -969,6 +970,47 @@ def websocket_list_engines(
)


@websocket_api.websocket_command(
{
"type": "tts/engine/get",
vol.Required("engine_id"): str,
}
)
@callback
def websocket_get_engine(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict
) -> None:
"""Get text to speech engine info."""
component: EntityComponent[TextToSpeechEntity] = hass.data[DOMAIN]
manager: SpeechManager = hass.data[DATA_TTS_MANAGER]

engine_id = msg["engine_id"]
provider_info: dict[str, Any]

provider: TextToSpeechEntity | Provider | None = next(
(entity for entity in component.entities if entity.entity_id == engine_id), None
)
if not provider:
provider = manager.providers.get(engine_id)

if not provider:
connection.send_error(
msg["id"],
websocket_api.const.ERR_NOT_FOUND,
f"tts engine {engine_id} not found",
)
return

provider_info = {
"engine_id": engine_id,
"supported_languages": provider.supported_languages,
}

connection.send_message(
websocket_api.result_message(msg["id"], {"provider": provider_info})
)


@websocket_api.websocket_command(
{
"type": "tts/engine/voices",
Expand Down
48 changes: 46 additions & 2 deletions tests/components/tts/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -1623,7 +1623,7 @@ async def async_get_tts_audio(
async def test_ws_list_engines(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, setup: str, engine_id: str
) -> None:
"""Test streaming audio and getting response."""
"""Test listing tts engines and supported languages."""
client = await hass_ws_client()

await client.send_json_auto_id({"type": "tts/engine/list"})
Expand Down Expand Up @@ -1690,6 +1690,50 @@ async def test_ws_list_engines(
}


@pytest.mark.parametrize(
("setup", "engine_id"),
[
("mock_setup", "test"),
("mock_config_entry_setup", "tts.test"),
],
indirect=["setup"],
)
async def test_ws_get_engine(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, setup: str, engine_id: str
) -> None:
"""Test getting an tts engine."""
client = await hass_ws_client()

await client.send_json_auto_id({"type": "tts/engine/get", "engine_id": engine_id})

msg = await client.receive_json()
assert msg["success"]
assert msg["result"] == {
"provider": {
"engine_id": engine_id,
"supported_languages": ["de_CH", "de_DE", "en_GB", "en_US"],
}
}


@pytest.mark.parametrize(
("setup", "engine_id"),
[("mock_setup", "not_existing"), ("mock_config_entry_setup", "tts.not_existing")],
indirect=["setup"],
)
async def test_ws_get_engine_none_existing(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, setup: str, engine_id: str
) -> None:
"""Test getting a non existing tts engine."""
client = await hass_ws_client()

await client.send_json_auto_id({"type": "tts/engine/get", "engine_id": engine_id})

msg = await client.receive_json()
assert not msg["success"]
assert msg["error"]["code"] == "not_found"


@pytest.mark.parametrize(
("setup", "engine_id"),
[
Expand All @@ -1701,7 +1745,7 @@ async def test_ws_list_engines(
async def test_ws_list_voices(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, setup: str, engine_id: str
) -> None:
"""Test streaming audio and getting response."""
"""Test listing supported voices for a tts engine and language."""
client = await hass_ws_client()

await client.send_json_auto_id(
Expand Down

0 comments on commit 75f8ea4

Please sign in to comment.