Repository: home-assistant/core
File: homeassistant/components/frontend/pr_download.py (Python)

Code before:
        zip_file.extractall(str(frontend_dir))

    sha_file = cache_dir / ".sha"
    sha_file.write_text(cache_key)


async def download_pr_artifact(
    hass: HomeAssistant,
    pr_number: int,
    github_token: str,
    tmp_dir: pathlib.Path,
) -> pathlib.Path:
    """Download and extract frontend PR artifact from GitHub.

    Returns the path to the tmp directory containing hass_frontend/.
    Raises HomeAssistantError on failure.
    """
    try:
        session = async_get_clientsession(hass)
    except Exception as err:
        raise HomeAssistantError(f"Failed to get HTTP client session: {err}") from err

    client = GitHubAPI(token=github_token, session=session)

    head_sha, base_sha = await _get_pr_shas(client, pr_number)
    cache_key = f"{head_sha}:{base_sha}"

    frontend_dir = tmp_dir / "hass_frontend"
    sha_file = tmp_dir / ".sha"

    if frontend_dir.exists() and sha_file.exists():
        try:
            cached_key = await hass.async_add_executor_job(sha_file.read_text)
            if cached_key.strip() == cache_key:
                _LOGGER.info(
                    "Using cached PR #%s (commit %s) from %s",
                    pr_number,
                    head_sha[:8],
                    tmp_dir,
                )
                return tmp_dir
            _LOGGER.info(
                "PR #%s cache outdated (cached: %s, current: %s), re-downloading",
                pr_number,
                cached_key[:8],
                head_sha[:8],
            )
        except OSError as err:
            _LOGGER.debug("Failed to read cache SHA file: %s", err)

    artifact_url = await _find_pr_artifact(client, pr_number, head_sha)

Review comment (performance):
Instead of this, how about just letting `_get_pr_shas` return the cache key, and also rename it accordingly

Code after:
        zip_file.extractall(str(frontend_dir))

    sha_file = cache_dir / ".sha"
    sha_file.write_text(cache_key)


async def download_pr_artifact(
    hass: HomeAssistant,
    pr_number: int,
    github_token: str,
    tmp_dir: pathlib.Path,
) -> pathlib.Path:
    """Download and extract frontend PR artifact from GitHub.

    Returns the path to the tmp directory containing hass_frontend/.
    Raises HomeAssistantError on failure.
    """
    try:
        session = async_get_clientsession(hass)
    except Exception as err:
        raise HomeAssistantError(f"Failed to get HTTP client session: {err}") from err

    client = GitHubAPI(token=github_token, session=session)

    head_sha, base_sha = await _get_pr_shas(client, pr_number)
    cache_key = f"{head_sha}:{base_sha}"

    frontend_dir = tmp_dir / "hass_frontend"
    sha_file = tmp_dir / ".sha"

    if frontend_dir.exists() and sha_file.exists():
        try:
            cached_key = await hass.async_add_executor_job(sha_file.read_text)
            cached_key = cached_key.strip()
            if cached_key == cache_key:
                _LOGGER.info(
                    "Using cached PR #%s (commit %s) from %s",
                    pr_number,
                    cache_key,
                    tmp_dir,
                )
                return tmp_dir
            _LOGGER.info(
                "PR #%s cache outdated (cached: %s, current: %s), re-downloading",
                pr_number,
                cached_key,
                cache_key,
            )
        except OSError as err:
            _LOGGER.debug("Failed to read cache SHA file: %s", err)