Repository: home-assistant/core
File: homeassistant/components/portainer/coordinator.py (Python)

Code before:
                translation_domain=DOMAIN,
                translation_key="invalid_auth",
                translation_placeholders={"error": repr(err)},
            ) from err
        except PortainerConnectionError as err:
            raise UpdateFailed(
                translation_domain=DOMAIN,
                translation_key="cannot_connect",
                translation_placeholders={"error": repr(err)},
            ) from err

        mapped_endpoints: dict[int, PortainerCoordinatorData] = {}
        for endpoint in endpoints:
            if endpoint.status == ENDPOINT_STATUS_DOWN:
                _LOGGER.debug(
                    "Skipping offline endpoint: %s (ID: %d)",
                    endpoint.name,
                    endpoint.id,
                )
                continue

            try:
                containers = await self.portainer.get_containers(endpoint.id)
                docker_version = await self.portainer.docker_version(endpoint.id)
                docker_info = await self.portainer.docker_info(endpoint.id)
                docker_system_df = await self.portainer.docker_system_df(endpoint.id)

                container_map: dict[str, PortainerContainerData] = {}

                container_stats_task = [
                    (
                        container,
                        self.portainer.container_stats(
                            endpoint_id=endpoint.id,
                            container_id=container.id,
                        ),
                    )
                    for container in containers
                    if container.state == CONTAINER_STATE_RUNNING
                ]

                container_stats_gather = await asyncio.gather(
                    *[task for _, task in container_stats_task]
                )
                for (container, _), container_stats in zip(
                    container_stats_task, container_stats_gather, strict=False
                ):
                    container_name = container.names[0].replace("/", " ").strip()

                    # Store previous stats if available. This is used to calculate deltas for CPU and network usage
                    # In the first call it will be None, since it has nothing to compare with

Review comment (suggestion):
As this integration grows more and more, we may consider not downloading all the data every time, but only the data that is needed.

systemmonitor implements such a pattern:
https://github.com/home-assistant/core/blob/3b2a7ba561c8d70741a5a99d0d37e4e50f975f7c/homeassistant/components/systemmonitor/coordinator.py#L122-L147
https://github.com/home-assistant/core/blob/3b2a7ba561c8d70741a5a99d0d37e4e50f975f7c/homeassistant/components/systemmonitor/coordinator.py#L153-L163
https://github.com/home-assistant/core/blob/3b2a7ba561c8d70741a5a99d0d37e4e50f975f7c/homeassistant/components/systemmonitor/sensor.py#L661-L673


Code after:
                translation_domain=DOMAIN,
                translation_key="invalid_auth",
                translation_placeholders={"error": repr(err)},
            ) from err
        except PortainerConnectionError as err:
            raise UpdateFailed(
                translation_domain=DOMAIN,
                translation_key="cannot_connect",
                translation_placeholders={"error": repr(err)},
            ) from err

        mapped_endpoints: dict[int, PortainerCoordinatorData] = {}
        for endpoint in endpoints:
            if endpoint.status == ENDPOINT_STATUS_DOWN:
                _LOGGER.debug(
                    "Skipping offline endpoint: %s (ID: %d)",
                    endpoint.name,
                    endpoint.id,
                )
                continue

            try:
                (
                    containers,
                    docker_version,
                    docker_info,
                    docker_system_df,
                ) = await asyncio.gather(
                    self.portainer.get_containers(endpoint.id),
                    self.portainer.docker_version(endpoint.id),
                    self.portainer.docker_info(endpoint.id),
                    self.portainer.docker_system_df(endpoint.id),
                )

                prev_endpoint = self.data.get(endpoint.id) if self.data else None
                container_map: dict[str, PortainerContainerData] = {}

                # Map containers, started and stopped
                for container in containers:
                    container_name = self._get_container_name(container.names[0])
                    prev_container = (
                        prev_endpoint.containers[container_name]
                        if prev_endpoint
                        else None
                    )
                    container_map[container_name] = PortainerContainerData(
                        container=container,
                        stats=None,
                        stats_pre=prev_container.stats if prev_container else None,
                    )