diff --git a/homeassistant/components/telegram_bot/__init__.py b/homeassistant/components/telegram_bot/__init__.py index a9f51fcc74e..e418336eaa2 100644 --- a/homeassistant/components/telegram_bot/__init__.py +++ b/homeassistant/components/telegram_bot/__init__.py @@ -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( diff --git a/homeassistant/components/telegram_bot/config_flow.py b/homeassistant/components/telegram_bot/config_flow.py index e93aa80126d..5217f26742b 100644 --- a/homeassistant/components/telegram_bot/config_flow.py +++ b/homeassistant/components/telegram_bot/config_flow.py @@ -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: diff --git a/homeassistant/components/telegram_bot/strings.json b/homeassistant/components/telegram_bot/strings.json index 367a5b52793..eb2ade5d198 100644 --- a/homeassistant/components/telegram_bot/strings.json +++ b/homeassistant/components/telegram_bot/strings.json @@ -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." }, diff --git a/tests/components/telegram_bot/test_config_flow.py b/tests/components/telegram_bot/test_config_flow.py index 6d638600fa5..49564e100b5 100644 --- a/tests/components/telegram_bot/test_config_flow.py +++ b/tests/components/telegram_bot/test_config_flow.py @@ -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: diff --git a/tests/components/telegram_bot/test_telegram_bot.py b/tests/components/telegram_bot/test_telegram_bot.py index 5f3611f7e0d..7ade0ba3ffe 100644 --- a/tests/components/telegram_bot/test_telegram_bot.py +++ b/tests/components/telegram_bot/test_telegram_bot.py @@ -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(