From e2daffc117478df18f2887cf4dfb7bd39cc0f9ab Mon Sep 17 00:00:00 2001 From: Dara Adib Date: Thu, 25 May 2023 05:29:13 -0400 Subject: [PATCH] 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. --- homeassistant/components/microsoft/tts.py | 4 ++-- tests/components/microsoft/test_tts.py | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/microsoft/tts.py b/homeassistant/components/microsoft/tts.py index 38f46d886de..9bcd7f533f8 100644 --- a/homeassistant/components/microsoft/tts.py +++ b/homeassistant/components/microsoft/tts.py @@ -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 @@ -121,7 +121,7 @@ class MicrosoftProvider(Provider): 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) diff --git a/tests/components/microsoft/test_tts.py b/tests/components/microsoft/test_tts.py index f01eff55690..dd183ae7120 100644 --- a/tests/components/microsoft/test_tts.py +++ b/tests/components/microsoft/test_tts.py @@ -3,7 +3,6 @@ from unittest.mock import patch from pycsspeechtts import pycsspeechtts import pytest -from requests import HTTPError from homeassistant.components import media_source, tts 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.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 @@ -314,7 +313,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