Include chat ID in Telegram bot subentry title (#147643)

This commit is contained in:
hanwg 2025-07-01 21:42:32 +08:00 committed by Franck Nijhof
parent 414318f3fb
commit c61935fc41
No known key found for this signature in database
GPG Key ID: AB33ADACE7101952
2 changed files with 5 additions and 8 deletions

View File

@ -237,12 +237,13 @@ class TelgramBotConfigFlow(ConfigFlow, domain=DOMAIN):
subentries: list[ConfigSubentryData] = []
allowed_chat_ids: list[int] = import_data[CONF_ALLOWED_CHAT_IDS]
assert self._bot is not None, "Bot should be initialized during import"
for chat_id in allowed_chat_ids:
chat_name: str = await _async_get_chat_name(self._bot, chat_id)
subentry: ConfigSubentryData = ConfigSubentryData(
data={CONF_CHAT_ID: chat_id},
subentry_type=CONF_ALLOWED_CHAT_IDS,
title=chat_name,
title=f"{chat_name} ({chat_id})",
unique_id=str(chat_id),
)
subentries.append(subentry)
@ -380,7 +381,6 @@ class TelgramBotConfigFlow(ConfigFlow, domain=DOMAIN):
"""Shutdown the bot if it exists."""
if self._bot:
await self._bot.shutdown()
self._bot = None
async def _validate_bot(
self,
@ -649,7 +649,7 @@ class AllowedChatIdsSubEntryFlowHandler(ConfigSubentryFlow):
chat_name = await _async_get_chat_name(bot, chat_id)
if chat_name:
return self.async_create_entry(
title=chat_name,
title=f"{chat_name} ({chat_id})",
data={CONF_CHAT_ID: chat_id},
unique_id=str(chat_id),
)
@ -663,10 +663,7 @@ class AllowedChatIdsSubEntryFlowHandler(ConfigSubentryFlow):
)
async def _async_get_chat_name(bot: Bot | None, chat_id: int) -> str:
if not bot:
return str(chat_id)
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)

View File

@ -383,7 +383,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"
assert subentry.title == "mock title (987654321)"
assert subentry.unique_id == "987654321"
assert subentry.data == {CONF_CHAT_ID: 987654321}