Add notify platform for Telegram bot (#149853)

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
hanwg
2025-08-12 21:24:13 +08:00
committed by GitHub
parent 072ae2b955
commit 455cf2fb42
3 changed files with 146 additions and 2 deletions

View File

@@ -19,6 +19,7 @@ from homeassistant.const import (
CONF_PLATFORM,
CONF_SOURCE,
CONF_URL,
Platform,
)
from homeassistant.core import (
HomeAssistant,
@@ -291,6 +292,8 @@ MODULES: dict[str, ModuleType] = {
PLATFORM_WEBHOOKS: webhooks,
}
PLATFORMS: list[Platform] = [Platform.NOTIFY]
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Telegram bot component."""
@@ -477,15 +480,21 @@ async def async_setup_entry(hass: HomeAssistant, entry: TelegramBotConfigEntry)
)
entry.runtime_data = notify_service
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(update_listener))
return True
async def update_listener(hass: HomeAssistant, entry: TelegramBotConfigEntry) -> None:
"""Handle options update."""
"""Handle config changes."""
entry.runtime_data.parse_mode = entry.options[ATTR_PARSER]
# reload entities
await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
async def async_unload_entry(
hass: HomeAssistant, entry: TelegramBotConfigEntry
@@ -494,4 +503,5 @@ async def async_unload_entry(
# broadcast platform has no app
if entry.runtime_data.app:
await entry.runtime_data.app.shutdown()
return True
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

View File

@@ -0,0 +1,62 @@
"""Telegram bot notification entity."""
from typing import Any
import telegram
from homeassistant.components.notify import NotifyEntity, NotifyEntityFeature
from homeassistant.config_entries import ConfigSubentry
from homeassistant.const import CONF_PLATFORM
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import TelegramBotConfigEntry
from .const import ATTR_TITLE, CONF_CHAT_ID, DOMAIN
async def async_setup_entry(
hass: HomeAssistant,
config_entry: TelegramBotConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the telegram bot notification entity platform."""
for subentry_id, subentry in config_entry.subentries.items():
async_add_entities(
[TelegramBotNotifyEntity(config_entry, subentry)],
config_subentry_id=subentry_id,
)
class TelegramBotNotifyEntity(NotifyEntity):
"""Representation of a telegram bot notification entity."""
_attr_supported_features = NotifyEntityFeature.TITLE
def __init__(
self,
config_entry: TelegramBotConfigEntry,
subentry: ConfigSubentry,
) -> None:
"""Initialize a notification entity."""
bot_id = config_entry.runtime_data.bot.id
chat_id = subentry.data[CONF_CHAT_ID]
self._attr_unique_id = f"{bot_id}_{chat_id}"
self.name = subentry.title
self._attr_device_info = DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
manufacturer="Telegram",
model=config_entry.data[CONF_PLATFORM].capitalize(),
sw_version=telegram.__version__,
identifiers={(DOMAIN, f"{bot_id}")},
)
self._target = chat_id
self._service = config_entry.runtime_data
async def async_send_message(self, message: str, title: str | None = None) -> None:
"""Send a message."""
kwargs: dict[str, Any] = {ATTR_TITLE: title}
await self._service.send_message(message, self._target, self._context, **kwargs)

View File

@@ -0,0 +1,72 @@
"""Test the telegram bot notify platform."""
from datetime import datetime
from unittest.mock import AsyncMock, patch
from freezegun.api import freeze_time
from telegram import Chat, Message
from telegram.constants import ChatType, ParseMode
from homeassistant.components.notify import (
ATTR_MESSAGE,
ATTR_TITLE,
DOMAIN as NOTIFY_DOMAIN,
SERVICE_SEND_MESSAGE,
)
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import Context, HomeAssistant
from tests.common import async_capture_events
@freeze_time("2025-01-09T12:00:00+00:00")
async def test_send_message(
hass: HomeAssistant,
webhook_platform: None,
) -> None:
"""Test publishing ntfy message."""
context = Context()
events = async_capture_events(hass, "telegram_sent")
with patch(
"homeassistant.components.telegram_bot.bot.Bot.send_message",
AsyncMock(
return_value=Message(
message_id=12345,
date=datetime.now(),
chat=Chat(id=123456, type=ChatType.PRIVATE),
)
),
) as mock_send_message:
await hass.services.async_call(
NOTIFY_DOMAIN,
SERVICE_SEND_MESSAGE,
{
ATTR_ENTITY_ID: "notify.telegram_bot_123456_12345678",
ATTR_MESSAGE: "mock message",
ATTR_TITLE: "mock title",
},
blocking=True,
context=context,
)
await hass.async_block_till_done()
mock_send_message.assert_called_once_with(
12345678,
"mock title\nmock message",
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=None,
disable_notification=False,
reply_to_message_id=None,
reply_markup=None,
read_timeout=None,
message_thread_id=None,
)
state = hass.states.get("notify.telegram_bot_123456_12345678")
assert state
assert state.state == "2025-01-09T12:00:00+00:00"
assert len(events) == 1
assert events[0].context == context