1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-08 17:49:37 +01:00

Refactor Telegram bot entity (#153609)

Co-authored-by: Josef Zweck <josef@zweck.dev>
This commit is contained in:
hanwg
2025-10-06 20:57:46 +08:00
committed by GitHub
parent 425bdc0ba6
commit cb6e65f972
3 changed files with 53 additions and 24 deletions
@@ -0,0 +1,38 @@
"""Base entity for Telegram bot integration."""
import telegram
from homeassistant.const import CONF_PLATFORM
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity import Entity, EntityDescription
from . import TelegramBotConfigEntry
from .const import DOMAIN
class TelegramBotEntity(Entity):
"""Base entity."""
_attr_has_entity_name = True
def __init__(
self,
config_entry: TelegramBotConfigEntry,
entity_description: EntityDescription,
) -> None:
"""Initialize the entity."""
self.bot_id = config_entry.runtime_data.bot.id
self.config_entry = config_entry
self.entity_description = entity_description
self.service = config_entry.runtime_data
self._attr_unique_id = f"{self.bot_id}_{entity_description.key}"
self._attr_device_info = DeviceInfo(
name=config_entry.title,
entry_type=DeviceEntryType.SERVICE,
manufacturer="Telegram",
model=config_entry.data[CONF_PLATFORM].capitalize(),
sw_version=telegram.__version__,
identifiers={(DOMAIN, f"{self.bot_id}")},
)
+13 -22
View File
@@ -2,17 +2,18 @@
from typing import Any
import telegram
from homeassistant.components.notify import NotifyEntity, NotifyEntityFeature
from homeassistant.components.notify import (
NotifyEntity,
NotifyEntityDescription,
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
from .const import ATTR_TITLE, CONF_CHAT_ID
from .entity import TelegramBotEntity
async def async_setup_entry(
@@ -29,7 +30,7 @@ async def async_setup_entry(
)
class TelegramBotNotifyEntity(NotifyEntity):
class TelegramBotNotifyEntity(TelegramBotEntity, NotifyEntity):
"""Representation of a telegram bot notification entity."""
_attr_supported_features = NotifyEntityFeature.TITLE
@@ -40,23 +41,13 @@ class TelegramBotNotifyEntity(NotifyEntity):
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}")},
super().__init__(
config_entry, NotifyEntityDescription(key=subentry.data[CONF_CHAT_ID])
)
self._target = chat_id
self._service = config_entry.runtime_data
self.chat_id = subentry.data[CONF_CHAT_ID]
self._attr_name = subentry.title
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)
await self.service.send_message(message, self.chat_id, self._context, **kwargs)
+2 -2
View File
@@ -43,7 +43,7 @@ async def test_send_message(
NOTIFY_DOMAIN,
SERVICE_SEND_MESSAGE,
{
ATTR_ENTITY_ID: "notify.telegram_bot_123456_12345678",
ATTR_ENTITY_ID: "notify.testbot_mock_last_name_mock_title_12345678",
ATTR_MESSAGE: "mock message",
ATTR_TITLE: "mock title",
},
@@ -64,7 +64,7 @@ async def test_send_message(
message_thread_id=None,
)
state = hass.states.get("notify.telegram_bot_123456_12345678")
state = hass.states.get("notify.testbot_mock_last_name_mock_title_12345678")
assert state
assert state.state == "2025-01-09T12:00:00+00:00"