Repository: home-assistant/core
File: homeassistant/components/telegram_bot/__init__.py (Python)

Code before:

        if not chat_ids and not targets:
            # no targets from service data, so we default to the first allowed chat IDs of the config entry
            subentries = list(config_entry.subentries.values())
            if not subentries:
                raise ServiceValidationError(
                    translation_domain=DOMAIN,
                    translation_key="missing_allowed_chat_ids",
                    translation_placeholders={
                        "bot_name": config_entry.title,
                    },
                )

            default_chat_id: int = subentries[0].data[ATTR_CHAT_ID]
            _LOGGER.debug(
                "Defaulting to chat ID %s for bot %s",
                default_chat_id,
                config_entry.title,
            )
            chat_ids = {default_chat_id}

        invalid_chat_ids: set[int] = set()
        for chat_id in chat_ids:
            # map chat_id to notify entity ID

            if config_entry.state != ConfigEntryState.LOADED:
                raise ServiceValidationError(
                    translation_domain=DOMAIN,
                    translation_key="entry_not_loaded",
                    translation_placeholders={"telegram_bot": config_entry.title},
                )

            entity_id = entity_registry.async_get_entity_id(
                "notify",
                DOMAIN,
                f"{config_entry.runtime_data.bot.id}_{chat_id}",
            )

            if not entity_id:
                invalid_chat_ids.add(chat_id)
            else:
                targets.append((config_entry, chat_id, entity_id))

        if invalid_chat_ids:
            raise ServiceValidationError(
                translation_domain=DOMAIN,
                translation_key="invalid_chat_ids",
                translation_placeholders={
                    "chat_ids": ", ".join(str(chat_id) for chat_id in invalid_chat_ids),
                    "bot_name": config_entry.title,
                },

Review comment (suggestion):
```suggestion
            if config_entry.state is not ConfigEntryState.LOADED:
```

Code after:

        if not chat_ids and not targets:
            # no targets from service data, so we default to the first allowed chat IDs of the config entry
            subentries = list(config_entry.subentries.values())
            if not subentries:
                raise ServiceValidationError(
                    translation_domain=DOMAIN,
                    translation_key="missing_allowed_chat_ids",
                    translation_placeholders={
                        "bot_name": config_entry.title,
                    },
                )

            default_chat_id: int = subentries[0].data[ATTR_CHAT_ID]
            _LOGGER.debug(
                "Defaulting to chat ID %s for bot %s",
                default_chat_id,
                config_entry.title,
            )
            chat_ids = {default_chat_id}

        invalid_chat_ids: set[int] = set()
        for chat_id in chat_ids:
            # map chat_id to notify entity ID

            if config_entry.state is not ConfigEntryState.LOADED:
                raise ServiceValidationError(
                    translation_domain=DOMAIN,
                    translation_key="entry_not_loaded",
                    translation_placeholders={"telegram_bot": config_entry.title},
                )

            entity_id = entity_registry.async_get_entity_id(
                "notify",
                DOMAIN,
                f"{config_entry.runtime_data.bot.id}_{chat_id}",
            )

            if not entity_id:
                invalid_chat_ids.add(chat_id)
            else:
                targets.append((config_entry, chat_id, entity_id))

        if invalid_chat_ids:
            raise ServiceValidationError(
                translation_domain=DOMAIN,
                translation_key="invalid_chat_ids",
                translation_placeholders={
                    "chat_ids": ", ".join(str(chat_id) for chat_id in invalid_chat_ids),
                    "bot_name": config_entry.title,
                },