diff --git a/homeassistant/components/telegram_bot/bot.py b/homeassistant/components/telegram_bot/bot.py index 4debb75838b5..4b21aab557e9 100644 --- a/homeassistant/components/telegram_bot/bot.py +++ b/homeassistant/components/telegram_bot/bot.py @@ -585,15 +585,20 @@ class TelegramNotificationService: ) _LOGGER.debug("downloaded: %s", entry.get(ATTR_URL) or entry.get(ATTR_FILE)) - caption: str | None = entry.get(ATTR_CAPTION) + # The parse mode of the media group itself only applies to a caption + # of the whole group, so each caption needs its own + caption_kwargs: dict[str, Any] = { + "caption": entry.get(ATTR_CAPTION), + "parse_mode": params[ATTR_PARSER], + } if entry[ATTR_MEDIA_TYPE] == InputMediaType.AUDIO: - media.append(InputMediaAudio(file_content, caption=caption)) + media.append(InputMediaAudio(file_content, **caption_kwargs)) elif entry[ATTR_MEDIA_TYPE] == InputMediaType.DOCUMENT: - media.append(InputMediaDocument(file_content, caption=caption)) + media.append(InputMediaDocument(file_content, **caption_kwargs)) elif entry[ATTR_MEDIA_TYPE] == InputMediaType.PHOTO: - media.append(InputMediaPhoto(file_content, caption=caption)) + media.append(InputMediaPhoto(file_content, **caption_kwargs)) else: - media.append(InputMediaVideo(file_content, caption=caption)) + media.append(InputMediaVideo(file_content, **caption_kwargs)) return await self._send_msg_formatted( self.bot.send_media_group, @@ -604,7 +609,6 @@ class TelegramNotificationService: protect_content=kwargs.get(ATTR_PROTECT_CONTENT, False), message_thread_id=params[ATTR_MESSAGE_THREAD_ID], reply_to_message_id=params[ATTR_REPLY_TO_MSGID], - parse_mode=params[ATTR_PARSER], context=context, ) diff --git a/tests/components/telegram_bot/test_telegram_bot.py b/tests/components/telegram_bot/test_telegram_bot.py index 3d76576365d1..85dff2b4f6b8 100644 --- a/tests/components/telegram_bot/test_telegram_bot.py +++ b/tests/components/telegram_bot/test_telegram_bot.py @@ -2733,6 +2733,61 @@ async def test_send_media_group( } +async def test_send_media_group_caption_parse_mode( + hass: HomeAssistant, + mock_broadcast_config_entry: MockConfigEntry, + mock_external_calls: None, +) -> None: + """Test the parse mode applies to the captions of the media items.""" + mock_broadcast_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_broadcast_config_entry.entry_id) + await hass.async_block_till_done() + + with ( + patch( + "homeassistant.components.telegram_bot.bot.httpx2.AsyncClient.get" + ) as mock_get, + patch( + "homeassistant.components.telegram_bot.bot.Bot.send_media_group", + AsyncMock( + return_value=[ + Message( + message_id=12345, + date=dt_util.utcnow(), + chat=Chat(id=123456, type=ChatType.PRIVATE), + ) + ] + ), + ) as mock_send_media_group, + ): + mock_get.return_value = AsyncMock(status_code=200, content=b"mock content") + + await hass.services.async_call( + DOMAIN, + SERVICE_SEND_MEDIA_GROUP, + { + ATTR_PARSER: PARSER_MD, + ATTR_MEDIA: [ + { + ATTR_MEDIA_TYPE: InputMediaType.PHOTO, + ATTR_URL: "https://mock/photo.jpg", + ATTR_CAPTION: "*bold*", + }, + { + ATTR_MEDIA_TYPE: InputMediaType.PHOTO, + ATTR_URL: "https://mock/photo2.jpg", + }, + ], + }, + blocking=True, + return_response=True, + ) + + media = mock_send_media_group.call_args.kwargs[ATTR_MEDIA] + assert media[0].caption == "*bold*" + assert media[0].parse_mode == PARSER_MD + + @pytest.mark.parametrize( "subdirectory", ["outside", "nested/deeper"],