Raise an error when the Kodi notification fails (#182592)

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Chris
2026-09-18 14:47:38 +00:00
committed by GitHub
co-authored by Claude Opus 5
parent 7d29177a46
commit b29d2abbb8
3 changed files with 69 additions and 4 deletions
+8 -3
View File
@@ -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
+8 -1
View File
@@ -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).",
+53
View File
@@ -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
)