Skip to content

Commit

Permalink
Clean up when TTS base URL is applied (home-assistant#70835)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored Apr 27, 2022
1 parent c4e85b7 commit 56000c0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 45 deletions.
61 changes: 17 additions & 44 deletions homeassistant/components/tts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,55 +203,28 @@ async def async_say_handle(service: ServiceCall) -> None:
language = service.data.get(ATTR_LANGUAGE)
options = service.data.get(ATTR_OPTIONS)

if tts.base_url is None or tts.base_url == get_url(hass):
tts.process_options(p_type, language, options)
params = {
"message": message,
}
if cache is not None:
params["cache"] = "true" if cache else "false"
if language is not None:
params["language"] = language
if options is not None:
params.update(options)

await hass.services.async_call(
DOMAIN_MP,
SERVICE_PLAY_MEDIA,
{
ATTR_ENTITY_ID: entity_ids,
ATTR_MEDIA_CONTENT_ID: generate_media_source_id(
DOMAIN,
str(yarl.URL.build(path=p_type, query=params)),
),
ATTR_MEDIA_CONTENT_TYPE: MEDIA_TYPE_MUSIC,
},
blocking=True,
context=service.context,
)
return

try:
url = await tts.async_get_url_path(
p_type, message, cache=cache, language=language, options=options
)
except HomeAssistantError as err:
_LOGGER.error("Error on init TTS: %s", err)
return

base = tts.base_url or get_url(hass)
url = base + url

data = {
ATTR_MEDIA_CONTENT_ID: url,
ATTR_MEDIA_CONTENT_TYPE: MEDIA_TYPE_MUSIC,
ATTR_ENTITY_ID: entity_ids,
tts.process_options(p_type, language, options)
params = {
"message": message,
}
if cache is not None:
params["cache"] = "true" if cache else "false"
if language is not None:
params["language"] = language
if options is not None:
params.update(options)

await hass.services.async_call(
DOMAIN_MP,
SERVICE_PLAY_MEDIA,
data,
{
ATTR_ENTITY_ID: entity_ids,
ATTR_MEDIA_CONTENT_ID: generate_media_source_id(
DOMAIN,
str(yarl.URL.build(path=p_type, query=params)),
),
ATTR_MEDIA_CONTENT_TYPE: MEDIA_TYPE_MUSIC,
},
blocking=True,
context=service.context,
)
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/tts/media_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.network import get_url

from .const import DOMAIN

Expand Down Expand Up @@ -64,6 +65,9 @@ async def async_resolve_media(self, item: MediaSourceItem) -> PlayMedia:

mime_type = mimetypes.guess_type(url)[0] or "audio/mpeg"

if manager.base_url and manager.base_url != get_url(self.hass):
url = f"{manager.base_url}{url}"

return PlayMedia(url, mime_type)

async def async_browse_media(
Expand Down
3 changes: 2 additions & 1 deletion tests/components/tts/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ async def test_setup_component_and_test_service_with_base_url_set(hass):
assert len(calls) == 1
assert calls[0].data[ATTR_MEDIA_CONTENT_TYPE] == MEDIA_TYPE_MUSIC
assert (
calls[0].data[ATTR_MEDIA_CONTENT_ID] == "http://fnord"
await get_media_source_url(hass, calls[0].data[ATTR_MEDIA_CONTENT_ID])
== "http://fnord"
"/api/tts_proxy/42f18378fd4393d18c8dd11d03fa9563c1e54491"
"_en_-_demo.mp3"
)
Expand Down

0 comments on commit 56000c0

Please sign in to comment.