diff --git a/homeassistant/components/kodi/notify.py b/homeassistant/components/kodi/notify.py index 4716cc97e9f8..57095197a70d 100644 --- a/homeassistant/components/kodi/notify.py +++ b/homeassistant/components/kodi/notify.py @@ -23,10 +23,13 @@ from homeassistant.const import ( CONF_USERNAME, ) from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import config_validation as cv from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType +from .const import DOMAIN + _LOGGER = logging.getLogger(__name__) DEFAULT_PORT = 8080 @@ -103,6 +106,8 @@ class KodiNotificationService(BaseNotificationService): title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT) await self._server.GUI.ShowNotification(title, message, icon, displaytime) - # pylint: disable-next=home-assistant-action-swallowed-exception - except jsonrpc_async.TransportError: - _LOGGER.warning("Unable to fetch Kodi data. Is Kodi online?") + except jsonrpc_async.TransportError as err: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="notify_failed", + ) from err diff --git a/homeassistant/components/kodi/strings.json b/homeassistant/components/kodi/strings.json index 20b90cd8e997..d8928245cdeb 100644 --- a/homeassistant/components/kodi/strings.json +++ b/homeassistant/components/kodi/strings.json @@ -54,11 +54,18 @@ "media_player": { "media_player": { "state_attributes": { - "dynamic_range": { "name": "Dynamic range" } + "dynamic_range": { + "name": "Dynamic range" + } } } } }, + "exceptions": { + "notify_failed": { + "message": "Unable to send the notification to Kodi. Is Kodi online?" + } + }, "services": { "add_to_playlist": { "description": "Adds music to the default playlist (i.e. playlistid=0).", diff --git a/tests/components/kodi/test_notify.py b/tests/components/kodi/test_notify.py new file mode 100644 index 000000000000..fedd96be52da --- /dev/null +++ b/tests/components/kodi/test_notify.py @@ -0,0 +1,53 @@ +"""Tests for the Kodi notify platform.""" + +from unittest.mock import AsyncMock, patch + +import jsonrpc_async +import pytest + +from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError +from homeassistant.setup import async_setup_component + +NOTIFY_CONFIG = { + NOTIFY_DOMAIN: { + "platform": "kodi", + "name": "kodi", + "host": "1.1.1.1", + "port": 8080, + } +} + + +async def setup_notify(hass: HomeAssistant, server: AsyncMock) -> None: + """Set up the Kodi notify platform with a mocked server.""" + with patch("jsonrpc_async.Server", return_value=server): + assert await async_setup_component(hass, NOTIFY_DOMAIN, NOTIFY_CONFIG) + await hass.async_block_till_done() + + +async def test_send_message(hass: HomeAssistant) -> None: + """Test sending a notification to Kodi.""" + server = AsyncMock() + await setup_notify(hass, server) + + await hass.services.async_call( + NOTIFY_DOMAIN, "kodi", {"message": "Hello", "title": "Test"}, blocking=True + ) + + server.GUI.ShowNotification.assert_called_once_with("Test", "Hello", "info", 10000) + + +async def test_send_message_transport_error(hass: HomeAssistant) -> None: + """Test an unreachable Kodi raises an error instead of only logging.""" + server = AsyncMock() + server.GUI.ShowNotification.side_effect = jsonrpc_async.TransportError( + "Unable to connect" + ) + await setup_notify(hass, server) + + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + NOTIFY_DOMAIN, "kodi", {"message": "Hello"}, blocking=True + )