From 4bdd8dbd40d6a45bf29eed8baa262e2b91fe7451 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 26 Feb 2024 16:31:18 -1000 Subject: [PATCH] Refactor rainmachine to increase chance of reusing the connection (#111573) --- .../components/rainmachine/__init__.py | 28 ++++++------------- homeassistant/components/rainmachine/util.py | 3 +- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/rainmachine/__init__.py b/homeassistant/components/rainmachine/__init__.py index 730e288df02..2e821fc7a7a 100644 --- a/homeassistant/components/rainmachine/__init__.py +++ b/homeassistant/components/rainmachine/__init__.py @@ -1,7 +1,6 @@ """Support for RainMachine devices.""" from __future__ import annotations -import asyncio from collections.abc import Callable, Coroutine from dataclasses import dataclass from datetime import timedelta @@ -206,13 +205,10 @@ async def async_update_programs_and_zones( programs affect zones and certain combinations of zones affect programs. """ data: RainMachineData = hass.data[DOMAIN][entry.entry_id] - - await asyncio.gather( - *[ - data.coordinators[DATA_PROGRAMS].async_refresh(), - data.coordinators[DATA_ZONES].async_refresh(), - ] - ) + # No gather here to allow http keep-alive to reuse + # the connection for each coordinator. + await data.coordinators[DATA_PROGRAMS].async_refresh() + await data.coordinators[DATA_ZONES].async_refresh() async def async_setup_entry( # noqa: C901 @@ -302,14 +298,6 @@ async def async_setup_entry( # noqa: C901 return data - async def async_init_coordinator( - coordinator: RainMachineDataUpdateCoordinator, - ) -> None: - """Initialize a RainMachineDataUpdateCoordinator.""" - await coordinator.async_initialize() - await coordinator.async_config_entry_first_refresh() - - controller_init_tasks = [] coordinators = {} for api_category, update_interval in COORDINATOR_UPDATE_INTERVAL_MAP.items(): coordinator = coordinators[api_category] = RainMachineDataUpdateCoordinator( @@ -320,9 +308,11 @@ async def async_setup_entry( # noqa: C901 update_interval=update_interval, update_method=partial(async_update, api_category), ) - controller_init_tasks.append(async_init_coordinator(coordinator)) - - await asyncio.gather(*controller_init_tasks) + coordinator.async_initialize() + # Its generally faster not to gather here so we can + # reuse the connection instead of creating a new + # connection for each coordinator. + await coordinator.async_config_entry_first_refresh() hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = RainMachineData( diff --git a/homeassistant/components/rainmachine/util.py b/homeassistant/components/rainmachine/util.py index 77a91c627a9..a557b701824 100644 --- a/homeassistant/components/rainmachine/util.py +++ b/homeassistant/components/rainmachine/util.py @@ -120,7 +120,8 @@ class RainMachineDataUpdateCoordinator(DataUpdateCoordinator[dict]): # pylint: self.config_entry.entry_id ) - async def async_initialize(self) -> None: + @callback + def async_initialize(self) -> None: """Initialize the coordinator.""" @callback