Fix schema validation error in Telegram (#160367)

This commit is contained in:
hanwg
2026-01-07 19:27:17 +08:00
committed by GitHub
parent 225be65f71
commit 6953bd4599
2 changed files with 115 additions and 6 deletions

View File

@@ -80,10 +80,6 @@ class TelegramNotificationService(BaseNotificationService):
def send_message(self, message="", **kwargs): def send_message(self, message="", **kwargs):
"""Send a message to a user.""" """Send a message to a user."""
service_data = {ATTR_TARGET: kwargs.get(ATTR_TARGET, self._chat_id)} service_data = {ATTR_TARGET: kwargs.get(ATTR_TARGET, self._chat_id)}
if ATTR_TITLE in kwargs:
service_data.update({ATTR_TITLE: kwargs.get(ATTR_TITLE)})
if message:
service_data.update({ATTR_MESSAGE: message})
data = kwargs.get(ATTR_DATA) data = kwargs.get(ATTR_DATA)
# Set message tag # Set message tag
@@ -161,6 +157,12 @@ class TelegramNotificationService(BaseNotificationService):
) )
# Send message # Send message
if ATTR_TITLE in kwargs:
service_data.update({ATTR_TITLE: kwargs.get(ATTR_TITLE)})
if message:
service_data.update({ATTR_MESSAGE: message})
_LOGGER.debug( _LOGGER.debug(
"TELEGRAM NOTIFIER calling %s.send_message with %s", "TELEGRAM NOTIFIER calling %s.send_message with %s",
TELEGRAM_BOT_DOMAIN, TELEGRAM_BOT_DOMAIN,

View File

@@ -1,12 +1,14 @@
"""The tests for the telegram.notify platform.""" """The tests for the telegram.notify platform."""
from unittest.mock import patch from typing import Any
from unittest.mock import AsyncMock, call, patch
from homeassistant import config as hass_config from homeassistant import config as hass_config
from homeassistant.components import notify from homeassistant.components import notify
from homeassistant.components.notify import ATTR_DATA, ATTR_MESSAGE, ATTR_TITLE
from homeassistant.components.telegram import DOMAIN from homeassistant.components.telegram import DOMAIN
from homeassistant.const import SERVICE_RELOAD from homeassistant.const import SERVICE_RELOAD
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant, ServiceRegistry
from homeassistant.helpers import issue_registry as ir from homeassistant.helpers import issue_registry as ir
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@@ -54,3 +56,108 @@ async def test_reload_notify(
issue_id="migrate_notify", issue_id="migrate_notify",
) )
assert len(issue_registry.issues) == 1 assert len(issue_registry.issues) == 1
async def test_notify(hass: HomeAssistant) -> None:
"""Test notify."""
assert await async_setup_component(
hass,
notify.DOMAIN,
{
notify.DOMAIN: [
{
"name": DOMAIN,
"platform": DOMAIN,
"chat_id": 1,
},
]
},
)
await hass.async_block_till_done()
original_call = ServiceRegistry.async_call
with patch(
"homeassistant.core.ServiceRegistry.async_call", new_callable=AsyncMock
) as mock_service_call:
# setup mock
async def call_service(*args, **kwargs) -> Any:
if args[0] == notify.DOMAIN:
return await original_call(
hass.services, args[0], args[1], args[2], kwargs["blocking"]
)
return AsyncMock()
mock_service_call.side_effect = call_service
# test send message
data: dict[str, Any] = {"title": "mock title", "message": "mock message"}
await hass.services.async_call(
notify.DOMAIN,
DOMAIN,
{ATTR_TITLE: "mock title", ATTR_MESSAGE: "mock message"},
blocking=True,
)
await hass.async_block_till_done()
assert mock_service_call.mock_calls == [
call(
"notify",
"telegram",
data,
blocking=True,
),
call(
"telegram_bot",
"send_message",
{"target": 1, "title": "mock title", "message": "mock message"},
False,
None,
None,
False,
),
]
mock_service_call.reset_mock()
# test send file
data = {
ATTR_TITLE: "mock title",
ATTR_MESSAGE: "mock message",
ATTR_DATA: {
"photo": {"url": "https://mock/photo.jpg", "caption": "mock caption"}
},
}
await hass.services.async_call(
notify.DOMAIN,
DOMAIN,
data,
blocking=True,
)
await hass.async_block_till_done()
assert mock_service_call.mock_calls == [
call(
"notify",
"telegram",
data,
blocking=True,
),
call(
"telegram_bot",
"send_photo",
{
"target": 1,
"url": "https://mock/photo.jpg",
"caption": "mock caption",
},
False,
None,
None,
False,
),
]