1
mirror of https://github.com/home-assistant/core synced 2024-09-06 10:29:55 +02:00

Fix exception handling in Microsoft TTS (#92556)

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.
This commit is contained in:
Dara Adib 2023-05-25 05:29:13 -04:00 committed by GitHub
parent 4ef315b32a
commit e2daffc117
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 6 deletions

View File

@ -1,8 +1,8 @@
"""Support for the Microsoft Cognitive Services text-to-speech service.""" """Support for the Microsoft Cognitive Services text-to-speech service."""
from http.client import HTTPException
import logging import logging
from pycsspeechtts import pycsspeechtts from pycsspeechtts import pycsspeechtts
from requests.exceptions import HTTPError
import voluptuous as vol import voluptuous as vol
from homeassistant.components.tts import CONF_LANG, PLATFORM_SCHEMA, Provider from homeassistant.components.tts import CONF_LANG, PLATFORM_SCHEMA, Provider
@ -121,7 +121,7 @@ class MicrosoftProvider(Provider):
contour=self._contour, contour=self._contour,
text=message, text=message,
) )
except HTTPException as ex: except HTTPError as ex:
_LOGGER.error("Error occurred for Microsoft TTS: %s", ex) _LOGGER.error("Error occurred for Microsoft TTS: %s", ex)
return (None, None) return (None, None)
return ("mp3", data) return ("mp3", data)

View File

@ -3,7 +3,6 @@ from unittest.mock import patch
from pycsspeechtts import pycsspeechtts from pycsspeechtts import pycsspeechtts
import pytest import pytest
from requests import HTTPError
from homeassistant.components import media_source, tts from homeassistant.components import media_source, tts
from homeassistant.components.media_player import ( from homeassistant.components.media_player import (
@ -14,7 +13,7 @@ from homeassistant.components.media_player import (
from homeassistant.components.microsoft.tts import SUPPORTED_LANGUAGES from homeassistant.components.microsoft.tts import SUPPORTED_LANGUAGES
from homeassistant.config import async_process_ha_core_config from homeassistant.config import async_process_ha_core_config
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceNotFound from homeassistant.exceptions import HomeAssistantError, ServiceNotFound
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import async_mock_service from tests.common import async_mock_service
@ -314,7 +313,6 @@ async def test_service_say_error(hass: HomeAssistant, mock_tts, calls) -> None:
) )
assert len(calls) == 1 assert len(calls) == 1
# Note: the integration currently catches HTTPException instead of HTTPError. with pytest.raises(HomeAssistantError):
with pytest.raises(HTTPError):
await get_media_source_url(hass, calls[0].data[ATTR_MEDIA_CONTENT_ID]) await get_media_source_url(hass, calls[0].data[ATTR_MEDIA_CONTENT_ID])
assert len(mock_tts.mock_calls) == 2 assert len(mock_tts.mock_calls) == 2