From 0af41d9cb16549e307cf7e1a471a7c8e66451a0d Mon Sep 17 00:00:00 2001 From: hanwg Date: Wed, 11 Jun 2025 22:19:22 +0800 Subject: [PATCH] Bug fix for Telegram bot integration: Handle plain text parse_mode (#146535) --- homeassistant/components/telegram_bot/bot.py | 4 ++-- homeassistant/components/telegram_bot/config_flow.py | 7 +++++-- homeassistant/components/telegram_bot/services.yaml | 1 + homeassistant/components/telegram_bot/strings.json | 5 +++-- tests/components/telegram_bot/test_config_flow.py | 6 +++--- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/telegram_bot/bot.py b/homeassistant/components/telegram_bot/bot.py index 58878485cb2..7749c7f1183 100644 --- a/homeassistant/components/telegram_bot/bot.py +++ b/homeassistant/components/telegram_bot/bot.py @@ -233,13 +233,13 @@ class TelegramNotificationService: """Initialize the service.""" self.app = app self.config = config - self._parsers = { + self._parsers: dict[str, str | None] = { PARSER_HTML: ParseMode.HTML, PARSER_MD: ParseMode.MARKDOWN, PARSER_MD2: ParseMode.MARKDOWN_V2, PARSER_PLAIN_TEXT: None, } - self.parse_mode = self._parsers.get(parser) + self.parse_mode = self._parsers[parser] self.bot = bot self.hass = hass self._last_message_id: dict[int, int] = {} diff --git a/homeassistant/components/telegram_bot/config_flow.py b/homeassistant/components/telegram_bot/config_flow.py index 63435a494f4..d9b334a4ac1 100644 --- a/homeassistant/components/telegram_bot/config_flow.py +++ b/homeassistant/components/telegram_bot/config_flow.py @@ -54,6 +54,7 @@ from .const import ( PARSER_HTML, PARSER_MD, PARSER_MD2, + PARSER_PLAIN_TEXT, PLATFORM_BROADCAST, PLATFORM_POLLING, PLATFORM_WEBHOOKS, @@ -126,8 +127,8 @@ OPTIONS_SCHEMA: vol.Schema = vol.Schema( ATTR_PARSER, ): SelectSelector( SelectSelectorConfig( - options=[PARSER_MD, PARSER_MD2, PARSER_HTML], - translation_key="parsers", + options=[PARSER_MD, PARSER_MD2, PARSER_HTML, PARSER_PLAIN_TEXT], + translation_key="parse_mode", ) ) } @@ -143,6 +144,8 @@ class OptionsFlowHandler(OptionsFlow): """Manage the options.""" if user_input is not None: + if user_input[ATTR_PARSER] == PARSER_PLAIN_TEXT: + user_input[ATTR_PARSER] = None return self.async_create_entry(data=user_input) return self.async_show_form( diff --git a/homeassistant/components/telegram_bot/services.yaml b/homeassistant/components/telegram_bot/services.yaml index 75f7a39a25f..1577d76b527 100644 --- a/homeassistant/components/telegram_bot/services.yaml +++ b/homeassistant/components/telegram_bot/services.yaml @@ -27,6 +27,7 @@ send_message: - "markdown" - "markdownv2" - "plain_text" + translation_key: "parse_mode" disable_notification: selector: boolean: diff --git a/homeassistant/components/telegram_bot/strings.json b/homeassistant/components/telegram_bot/strings.json index cff3141c092..d772edf1945 100644 --- a/homeassistant/components/telegram_bot/strings.json +++ b/homeassistant/components/telegram_bot/strings.json @@ -106,11 +106,12 @@ "webhooks": "Webhooks" } }, - "parsers": { + "parse_mode": { "options": { "markdown": "Markdown (Legacy)", "markdownv2": "MarkdownV2", - "html": "HTML" + "html": "HTML", + "plain_text": "Plain text" } } }, diff --git a/tests/components/telegram_bot/test_config_flow.py b/tests/components/telegram_bot/test_config_flow.py index 62a0c02b979..47b6d99b9ce 100644 --- a/tests/components/telegram_bot/test_config_flow.py +++ b/tests/components/telegram_bot/test_config_flow.py @@ -19,8 +19,8 @@ from homeassistant.components.telegram_bot.const import ( ERROR_MESSAGE, ISSUE_DEPRECATED_YAML, ISSUE_DEPRECATED_YAML_IMPORT_ISSUE_ERROR, - PARSER_HTML, PARSER_MD, + PARSER_PLAIN_TEXT, PLATFORM_BROADCAST, PLATFORM_WEBHOOKS, SUBENTRY_TYPE_ALLOWED_CHAT_IDS, @@ -56,13 +56,13 @@ async def test_options_flow( result = await hass.config_entries.options.async_configure( result["flow_id"], { - ATTR_PARSER: PARSER_HTML, + ATTR_PARSER: PARSER_PLAIN_TEXT, }, ) await hass.async_block_till_done() assert result["type"] == FlowResultType.CREATE_ENTRY - assert result["data"][ATTR_PARSER] == PARSER_HTML + assert result["data"][ATTR_PARSER] is None async def test_reconfigure_flow_broadcast(