diff --git a/homeassistant/components/telegram_bot/entity.py b/homeassistant/components/telegram_bot/entity.py new file mode 100644 index 00000000000..95adc934781 --- /dev/null +++ b/homeassistant/components/telegram_bot/entity.py @@ -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}")}, + ) diff --git a/homeassistant/components/telegram_bot/notify.py b/homeassistant/components/telegram_bot/notify.py index 822bd7b925d..510b25493d8 100644 --- a/homeassistant/components/telegram_bot/notify.py +++ b/homeassistant/components/telegram_bot/notify.py @@ -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) diff --git a/tests/components/telegram_bot/test_notify.py b/tests/components/telegram_bot/test_notify.py index d43d5492760..969eef568b9 100644 --- a/tests/components/telegram_bot/test_notify.py +++ b/tests/components/telegram_bot/test_notify.py @@ -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"