Skip to content

Commit 0af41d9

Browse files
authored
Bug fix for Telegram bot integration: Handle plain text parse_mode (home-assistant#146535)
1 parent b02c041 commit 0af41d9

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

homeassistant/components/telegram_bot/bot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,13 @@ def __init__(
233233
"""Initialize the service."""
234234
self.app = app
235235
self.config = config
236-
self._parsers = {
236+
self._parsers: dict[str, str | None] = {
237237
PARSER_HTML: ParseMode.HTML,
238238
PARSER_MD: ParseMode.MARKDOWN,
239239
PARSER_MD2: ParseMode.MARKDOWN_V2,
240240
PARSER_PLAIN_TEXT: None,
241241
}
242-
self.parse_mode = self._parsers.get(parser)
242+
self.parse_mode = self._parsers[parser]
243243
self.bot = bot
244244
self.hass = hass
245245
self._last_message_id: dict[int, int] = {}

homeassistant/components/telegram_bot/config_flow.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
PARSER_HTML,
5555
PARSER_MD,
5656
PARSER_MD2,
57+
PARSER_PLAIN_TEXT,
5758
PLATFORM_BROADCAST,
5859
PLATFORM_POLLING,
5960
PLATFORM_WEBHOOKS,
@@ -126,8 +127,8 @@
126127
ATTR_PARSER,
127128
): SelectSelector(
128129
SelectSelectorConfig(
129-
options=[PARSER_MD, PARSER_MD2, PARSER_HTML],
130-
translation_key="parsers",
130+
options=[PARSER_MD, PARSER_MD2, PARSER_HTML, PARSER_PLAIN_TEXT],
131+
translation_key="parse_mode",
131132
)
132133
)
133134
}
@@ -143,6 +144,8 @@ async def async_step_init(
143144
"""Manage the options."""
144145

145146
if user_input is not None:
147+
if user_input[ATTR_PARSER] == PARSER_PLAIN_TEXT:
148+
user_input[ATTR_PARSER] = None
146149
return self.async_create_entry(data=user_input)
147150

148151
return self.async_show_form(

homeassistant/components/telegram_bot/services.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ send_message:
2727
- "markdown"
2828
- "markdownv2"
2929
- "plain_text"
30+
translation_key: "parse_mode"
3031
disable_notification:
3132
selector:
3233
boolean:

homeassistant/components/telegram_bot/strings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,12 @@
106106
"webhooks": "Webhooks"
107107
}
108108
},
109-
"parsers": {
109+
"parse_mode": {
110110
"options": {
111111
"markdown": "Markdown (Legacy)",
112112
"markdownv2": "MarkdownV2",
113-
"html": "HTML"
113+
"html": "HTML",
114+
"plain_text": "Plain text"
114115
}
115116
}
116117
},

tests/components/telegram_bot/test_config_flow.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
ERROR_MESSAGE,
2020
ISSUE_DEPRECATED_YAML,
2121
ISSUE_DEPRECATED_YAML_IMPORT_ISSUE_ERROR,
22-
PARSER_HTML,
2322
PARSER_MD,
23+
PARSER_PLAIN_TEXT,
2424
PLATFORM_BROADCAST,
2525
PLATFORM_WEBHOOKS,
2626
SUBENTRY_TYPE_ALLOWED_CHAT_IDS,
@@ -56,13 +56,13 @@ async def test_options_flow(
5656
result = await hass.config_entries.options.async_configure(
5757
result["flow_id"],
5858
{
59-
ATTR_PARSER: PARSER_HTML,
59+
ATTR_PARSER: PARSER_PLAIN_TEXT,
6060
},
6161
)
6262
await hass.async_block_till_done()
6363

6464
assert result["type"] == FlowResultType.CREATE_ENTRY
65-
assert result["data"][ATTR_PARSER] == PARSER_HTML
65+
assert result["data"][ATTR_PARSER] is None
6666

6767

6868
async def test_reconfigure_flow_broadcast(

0 commit comments

Comments
 (0)