Bug fix for Telegram bot integration: Handle plain text parse_mode (#146535)

This commit is contained in:
hanwg 2025-06-11 22:19:22 +08:00 committed by GitHub
parent b02c0419b4
commit 0af41d9cb1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 14 additions and 9 deletions

View File

@ -233,13 +233,13 @@ class TelegramNotificationService:
"""Initialize the service.""" """Initialize the service."""
self.app = app self.app = app
self.config = config self.config = config
self._parsers = { self._parsers: dict[str, str | None] = {
PARSER_HTML: ParseMode.HTML, PARSER_HTML: ParseMode.HTML,
PARSER_MD: ParseMode.MARKDOWN, PARSER_MD: ParseMode.MARKDOWN,
PARSER_MD2: ParseMode.MARKDOWN_V2, PARSER_MD2: ParseMode.MARKDOWN_V2,
PARSER_PLAIN_TEXT: None, PARSER_PLAIN_TEXT: None,
} }
self.parse_mode = self._parsers.get(parser) self.parse_mode = self._parsers[parser]
self.bot = bot self.bot = bot
self.hass = hass self.hass = hass
self._last_message_id: dict[int, int] = {} self._last_message_id: dict[int, int] = {}

View File

@ -54,6 +54,7 @@ from .const import (
PARSER_HTML, PARSER_HTML,
PARSER_MD, PARSER_MD,
PARSER_MD2, PARSER_MD2,
PARSER_PLAIN_TEXT,
PLATFORM_BROADCAST, PLATFORM_BROADCAST,
PLATFORM_POLLING, PLATFORM_POLLING,
PLATFORM_WEBHOOKS, PLATFORM_WEBHOOKS,
@ -126,8 +127,8 @@ OPTIONS_SCHEMA: vol.Schema = vol.Schema(
ATTR_PARSER, ATTR_PARSER,
): SelectSelector( ): SelectSelector(
SelectSelectorConfig( SelectSelectorConfig(
options=[PARSER_MD, PARSER_MD2, PARSER_HTML], options=[PARSER_MD, PARSER_MD2, PARSER_HTML, PARSER_PLAIN_TEXT],
translation_key="parsers", translation_key="parse_mode",
) )
) )
} }
@ -143,6 +144,8 @@ class OptionsFlowHandler(OptionsFlow):
"""Manage the options.""" """Manage the options."""
if user_input is not None: 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_create_entry(data=user_input)
return self.async_show_form( return self.async_show_form(

View File

@ -27,6 +27,7 @@ send_message:
- "markdown" - "markdown"
- "markdownv2" - "markdownv2"
- "plain_text" - "plain_text"
translation_key: "parse_mode"
disable_notification: disable_notification:
selector: selector:
boolean: boolean:

View File

@ -106,11 +106,12 @@
"webhooks": "Webhooks" "webhooks": "Webhooks"
} }
}, },
"parsers": { "parse_mode": {
"options": { "options": {
"markdown": "Markdown (Legacy)", "markdown": "Markdown (Legacy)",
"markdownv2": "MarkdownV2", "markdownv2": "MarkdownV2",
"html": "HTML" "html": "HTML",
"plain_text": "Plain text"
} }
} }
}, },

View File

@ -19,8 +19,8 @@ from homeassistant.components.telegram_bot.const import (
ERROR_MESSAGE, ERROR_MESSAGE,
ISSUE_DEPRECATED_YAML, ISSUE_DEPRECATED_YAML,
ISSUE_DEPRECATED_YAML_IMPORT_ISSUE_ERROR, ISSUE_DEPRECATED_YAML_IMPORT_ISSUE_ERROR,
PARSER_HTML,
PARSER_MD, PARSER_MD,
PARSER_PLAIN_TEXT,
PLATFORM_BROADCAST, PLATFORM_BROADCAST,
PLATFORM_WEBHOOKS, PLATFORM_WEBHOOKS,
SUBENTRY_TYPE_ALLOWED_CHAT_IDS, SUBENTRY_TYPE_ALLOWED_CHAT_IDS,
@ -56,13 +56,13 @@ async def test_options_flow(
result = await hass.config_entries.options.async_configure( result = await hass.config_entries.options.async_configure(
result["flow_id"], result["flow_id"],
{ {
ATTR_PARSER: PARSER_HTML, ATTR_PARSER: PARSER_PLAIN_TEXT,
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert result["type"] == FlowResultType.CREATE_ENTRY 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( async def test_reconfigure_flow_broadcast(