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

Code before:
    bootstrap = protect.bootstrap
    nvr_info = bootstrap.nvr
    auth_user = bootstrap.users.get(bootstrap.auth_user_id)

    # Check if API key is missing
    if not protect.is_api_key_set() and auth_user and nvr_info.can_write(auth_user):
        try:
            new_api_key = await protect.create_api_key(
                name=f"Home Assistant ({hass.config.location_name})"
            )
        except NotAuthorized as err:
            _LOGGER.error("Failed to create API key: %s", err)
        else:
            protect.set_api_key(new_api_key)
            hass.config_entries.async_update_entry(
                entry, data={**entry.data, CONF_API_KEY: new_api_key}
            )

    if not protect.is_api_key_set():
        hass.async_create_task(
            hass.config_entries.flow.async_init(
                DOMAIN,
                context={"source": "reauth", "entry_id": entry.entry_id},
                data=entry.data,
            )
        )
        raise ConfigEntryAuthFailed(
            translation_domain=DOMAIN,
            translation_key="api_key_required",
        )

    if auth_user and auth_user.cloud_account:
        ir.async_create_issue(
            hass,
            DOMAIN,
            "cloud_user",
            is_fixable=True,
            is_persistent=False,
            learn_more_url="https://www.home-assistant.io/integrations/unifiprotect/#local-user",
            severity=IssueSeverity.ERROR,
            translation_key="cloud_user",
            data={"entry_id": entry.entry_id},
        )

    if nvr_info.version < MIN_REQUIRED_PROTECT_V:
        raise ConfigEntryError(
            translation_domain=DOMAIN,
            translation_key="protect_version",
            translation_placeholders={
                "current_version": str(nvr_info.version),
                "min_version": str(MIN_REQUIRED_PROTECT_V),

Review comment (bug):
They should get the popup already when you raise `ConfigEntryAuthFailed` so I don't think we need to start it manually as well.

```suggestion
```

Code after:
    bootstrap = protect.bootstrap
    nvr_info = bootstrap.nvr
    auth_user = bootstrap.users.get(bootstrap.auth_user_id)

    # Check if API key is missing
    if not protect.is_api_key_set() and auth_user and nvr_info.can_write(auth_user):
        try:
            new_api_key = await protect.create_api_key(
                name=f"Home Assistant ({hass.config.location_name})"
            )
        except NotAuthorized as err:
            _LOGGER.error("Failed to create API key: %s", err)
        else:
            protect.set_api_key(new_api_key)
            hass.config_entries.async_update_entry(
                entry, data={**entry.data, CONF_API_KEY: new_api_key}
            )

    if not protect.is_api_key_set():
        raise ConfigEntryAuthFailed(
            translation_domain=DOMAIN,
            translation_key="api_key_required",
        )

    if auth_user and auth_user.cloud_account:
        ir.async_create_issue(
            hass,
            DOMAIN,
            "cloud_user",
            is_fixable=True,
            is_persistent=False,
            learn_more_url="https://www.home-assistant.io/integrations/unifiprotect/#local-user",
            severity=IssueSeverity.ERROR,
            translation_key="cloud_user",
            data={"entry_id": entry.entry_id},
        )

    if nvr_info.version < MIN_REQUIRED_PROTECT_V:
        raise ConfigEntryError(
            translation_domain=DOMAIN,
            translation_key="protect_version",
            translation_placeholders={
                "current_version": str(nvr_info.version),
                "min_version": str(MIN_REQUIRED_PROTECT_V),
            },
        )

    if entry.unique_id is None:
        hass.config_entries.async_update_entry(entry, unique_id=nvr_info.mac)

    entry.runtime_data = data_service