Skip to content

Commit

Permalink
Fix exception handling in Microsoft TTS
Browse files Browse the repository at this point in the history
pycsspeechtts uses the requests library, but Microsoft TTS previously
caught HTTPException from the standard library. This is changed to
catch requests.HTTPError and return `(None, None)` consistent with
other TTS integrations. This will properly raise HomeAssistantError
for display in the frontend.

Follow up to PR #92215 which adds tests for Microsoft TTS.
  • Loading branch information
daradib committed May 4, 2023
1 parent 35ad79a commit 2a70089
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
4 changes: 2 additions & 2 deletions homeassistant/components/microsoft/tts.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Support for the Microsoft Cognitive Services text-to-speech service."""
from http.client import HTTPException
import logging

from pycsspeechtts import pycsspeechtts
from requests.exceptions import HTTPError
import voluptuous as vol

from homeassistant.components.tts import CONF_LANG, PLATFORM_SCHEMA, Provider
Expand Down Expand Up @@ -194,7 +194,7 @@ def get_tts_audio(self, message, language, options=None):
contour=self._contour,
text=message,
)
except HTTPException as ex:
except HTTPError as ex:
_LOGGER.error("Error occurred for Microsoft TTS: %s", ex)
return (None, None)
return ("mp3", data)
6 changes: 2 additions & 4 deletions tests/components/microsoft/test_tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from pycsspeechtts import pycsspeechtts
import pytest
from requests import HTTPError

from homeassistant.components import media_source, tts
from homeassistant.components.media_player import (
Expand All @@ -16,7 +15,7 @@
from homeassistant.components.microsoft.tts import SUPPORTED_LANGUAGES
from homeassistant.config import async_process_ha_core_config
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceNotFound
from homeassistant.exceptions import HomeAssistantError, ServiceNotFound
from homeassistant.setup import async_setup_component

from tests.common import async_mock_service
Expand Down Expand Up @@ -236,7 +235,6 @@ async def test_service_say_error(hass: HomeAssistant, mock_tts, calls) -> None:
)

assert len(calls) == 1
# Note: the integration currently catches HTTPException instead of HTTPError.
with pytest.raises(HTTPError):
with pytest.raises(HomeAssistantError):
await get_media_source_url(hass, calls[0].data[ATTR_MEDIA_CONTENT_ID])
assert len(mock_tts.mock_calls) == 2

0 comments on commit 2a70089

Please sign in to comment.