mirror of
https://github.com/home-assistant/core.git
synced 2026-04-02 08:26:41 +01:00
Improve subentry error handling for Telegram bot (#165863)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -153,6 +153,7 @@ STEP_WEBHOOKS_DATA_SCHEMA: vol.Schema = vol.Schema(
|
||||
vol.Required(CONF_TRUSTED_NETWORKS): vol.Coerce(str),
|
||||
}
|
||||
)
|
||||
SUBENTRY_SCHEMA: vol.Schema = vol.Schema({vol.Required(CONF_CHAT_ID): vol.Coerce(int)})
|
||||
OPTIONS_SCHEMA: vol.Schema = vol.Schema(
|
||||
{
|
||||
vol.Required(
|
||||
@@ -598,24 +599,30 @@ class AllowedChatIdsSubEntryFlowHandler(ConfigSubentryFlow):
|
||||
)
|
||||
|
||||
errors: dict[str, str] = {}
|
||||
description_placeholders = DESCRIPTION_PLACEHOLDERS.copy()
|
||||
|
||||
if user_input is not None:
|
||||
config_entry: TelegramBotConfigEntry = self._get_entry()
|
||||
bot = config_entry.runtime_data.bot
|
||||
|
||||
# validate chat id
|
||||
chat_id: int = user_input[CONF_CHAT_ID]
|
||||
chat_name = await _async_get_chat_name(bot, chat_id)
|
||||
if chat_name:
|
||||
try:
|
||||
chat_info: ChatFullInfo = await bot.get_chat(chat_id)
|
||||
except BadRequest:
|
||||
errors["base"] = "chat_not_found"
|
||||
except TelegramError as err:
|
||||
errors["base"] = "telegram_error"
|
||||
description_placeholders[ERROR_MESSAGE] = str(err)
|
||||
|
||||
if not errors:
|
||||
return self.async_create_entry(
|
||||
title=f"{chat_name} ({chat_id})",
|
||||
title=chat_info.effective_name or str(chat_id),
|
||||
data={CONF_CHAT_ID: chat_id},
|
||||
unique_id=str(chat_id),
|
||||
)
|
||||
|
||||
errors["base"] = "chat_not_found"
|
||||
|
||||
service: TelegramNotificationService = self._get_entry().runtime_data
|
||||
description_placeholders = DESCRIPTION_PLACEHOLDERS.copy()
|
||||
description_placeholders["bot_username"] = f"@{service.bot.username}"
|
||||
description_placeholders["bot_url"] = f"https://t.me/{service.bot.username}"
|
||||
|
||||
@@ -639,7 +646,7 @@ class AllowedChatIdsSubEntryFlowHandler(ConfigSubentryFlow):
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=self.add_suggested_values_to_schema(
|
||||
vol.Schema({vol.Required(CONF_CHAT_ID): vol.Coerce(int)}),
|
||||
SUBENTRY_SCHEMA,
|
||||
suggested_values,
|
||||
),
|
||||
description_placeholders=description_placeholders,
|
||||
@@ -677,11 +684,3 @@ async def _get_most_recent_chat(
|
||||
)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
async def _async_get_chat_name(bot: Bot, chat_id: int) -> str:
|
||||
try:
|
||||
chat_info: ChatFullInfo = await bot.get_chat(chat_id)
|
||||
return chat_info.effective_name or str(chat_id)
|
||||
except BadRequest:
|
||||
return ""
|
||||
|
||||
@@ -92,7 +92,8 @@
|
||||
},
|
||||
"entry_type": "Allowed chat ID",
|
||||
"error": {
|
||||
"chat_not_found": "Chat not found"
|
||||
"chat_not_found": "Chat not found",
|
||||
"telegram_error": "[%key:component::telegram_bot::config::error::telegram_error%]"
|
||||
},
|
||||
"initiate_flow": {
|
||||
"user": "Add allowed chat ID"
|
||||
|
||||
@@ -557,7 +557,7 @@ async def test_subentry_flow(
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert subentry.subentry_type == SUBENTRY_TYPE_ALLOWED_CHAT_IDS
|
||||
assert subentry.title == "mock title (987654321)"
|
||||
assert subentry.title == "mock title"
|
||||
assert subentry.unique_id == "987654321"
|
||||
assert subentry.data == {CONF_CHAT_ID: 987654321}
|
||||
|
||||
@@ -596,6 +596,22 @@ async def test_subentry_flow_chat_error(
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
# test: network error
|
||||
|
||||
with patch("homeassistant.components.telegram_bot.bot.Bot.get_chat") as mock_bot:
|
||||
mock_bot.side_effect = NetworkError("mock network error")
|
||||
|
||||
result = await hass.config_entries.subentries.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_CHAT_ID: 1234567890},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"]["base"] == "telegram_error"
|
||||
assert result["description_placeholders"]["error_message"] == "mock network error"
|
||||
|
||||
# test: chat not found
|
||||
|
||||
with patch("homeassistant.components.telegram_bot.bot.Bot.get_chat") as mock_bot:
|
||||
|
||||
Reference in New Issue
Block a user