1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-15 07:36:16 +00:00

Handle config entry not loaded for Telegram bot (#161951)

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
hanwg
2026-02-12 05:05:21 +08:00
committed by GitHub
parent 9c821fb5f5
commit 90b67f90fa
5 changed files with 33 additions and 7 deletions

View File

@@ -11,6 +11,7 @@ from telegram.error import InvalidToken, TelegramError
import voluptuous as vol
from homeassistant.components.script import DOMAIN as SCRIPT_DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import (
ATTR_DOMAIN,
ATTR_ENTITY_ID,
@@ -739,10 +740,11 @@ def _build_targets(
for chat_id in chat_ids:
# map chat_id to notify entity ID
if not hasattr(config_entry, "runtime_data"):
if config_entry.state is not ConfigEntryState.LOADED:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="missing_config_entry",
translation_key="entry_not_loaded",
translation_placeholders={"telegram_bot": config_entry.title},
)
entity_id = entity_registry.async_get_entity_id(

View File

@@ -588,6 +588,12 @@ class AllowedChatIdsSubEntryFlowHandler(ConfigSubentryFlow):
) -> SubentryFlowResult:
"""Create allowed chat ID."""
if self._get_entry().state != ConfigEntryState.LOADED:
return self.async_abort(
reason="entry_not_loaded",
description_placeholders={"telegram_bot": self._get_entry().title},
)
errors: dict[str, str] = {}
if user_input is not None:

View File

@@ -87,7 +87,8 @@
"config_subentries": {
"allowed_chat_ids": {
"abort": {
"already_configured": "Chat already configured"
"already_configured": "Chat already configured",
"entry_not_loaded": "[%key:component::telegram_bot::exceptions::entry_not_loaded::message%]"
},
"entry_type": "Allowed chat ID",
"error": {
@@ -186,6 +187,9 @@
"allowlist_external_dirs_error": {
"message": "File path has not been configured in allowlist_external_dirs."
},
"entry_not_loaded": {
"message": "{telegram_bot} is not loaded"
},
"failed_to_load_file": {
"message": "Failed to load file: {error}"
},
@@ -204,9 +208,6 @@
"missing_allowed_chat_ids": {
"message": "No allowed chat IDs found. Please add allowed chat IDs for {bot_name}."
},
"missing_config_entry": {
"message": "No config entries found or setup failed. Please set up the Telegram Bot first."
},
"missing_input": {
"message": "{field} is required."
},

View File

@@ -484,6 +484,22 @@ async def test_subentry_flow(
assert subentry.data == {CONF_CHAT_ID: 987654321}
async def test_subentry_flow_config_not_ready(
hass: HomeAssistant, mock_broadcast_config_entry: MockConfigEntry
) -> None:
"""Test subentry flow where config entry is not loaded."""
mock_broadcast_config_entry.add_to_hass(hass)
result = await hass.config_entries.subentries.async_init(
(mock_broadcast_config_entry.entry_id, SUBENTRY_TYPE_ALLOWED_CHAT_IDS),
context={"source": SOURCE_USER},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "entry_not_loaded"
assert result["description_placeholders"] == {"telegram_bot": "Mock Title"}
async def test_subentry_flow_chat_error(
hass: HomeAssistant, mock_broadcast_config_entry: MockConfigEntry
) -> None:

View File

@@ -1079,7 +1079,8 @@ async def test_send_message_config_entry_error(
)
await hass.async_block_till_done()
assert err.value.translation_key == "missing_config_entry"
assert err.value.translation_key == "entry_not_loaded"
assert err.value.translation_placeholders == {"telegram_bot": "Mock Title"}
async def test_delete_message(