Group platform loads in homekit_controller to minimize executor use (#112148)

pull/112166/head
J. Nick Koston 2024-03-03 22:39:07 -10:00 committed by GitHub
parent 40c0b4caf0
commit 38f9285bd6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 20 deletions

View File

@ -30,7 +30,6 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.debounce import Debouncer from homeassistant.helpers.debounce import Debouncer
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.event import async_call_later, async_track_time_interval from homeassistant.helpers.event import async_call_later, async_track_time_interval
from homeassistant.util.async_ import create_eager_task
from .config_flow import normalize_hkid from .config_flow import normalize_hkid
from .const import ( from .const import (
@ -320,7 +319,7 @@ class HKDevice:
) )
# BLE devices always get an RSSI sensor as well # BLE devices always get an RSSI sensor as well
if "sensor" not in self.platforms: if "sensor" not in self.platforms:
await self.async_load_platform("sensor") await self._async_load_platforms({"sensor"})
@callback @callback
def _async_start_polling(self) -> None: def _async_start_polling(self) -> None:
@ -791,19 +790,14 @@ class HKDevice:
self.entities.add(entity_key) self.entities.add(entity_key)
break break
async def async_load_platform(self, platform: str) -> None: async def _async_load_platforms(self, platforms: set[str]) -> None:
"""Load a single platform idempotently.""" """Load a group of platforms."""
if platform in self.platforms: if not (to_load := platforms - self.platforms):
return return
self.platforms.update(to_load)
self.platforms.add(platform) await self.hass.config_entries.async_forward_entry_setups(
try: self.config_entry, platforms
await self.hass.config_entries.async_forward_entry_setup(
self.config_entry, platform
) )
except Exception:
self.platforms.remove(platform)
raise
async def async_load_platforms(self) -> None: async def async_load_platforms(self) -> None:
"""Load any platforms needed by this HomeKit device.""" """Load any platforms needed by this HomeKit device."""
@ -822,12 +816,7 @@ class HKDevice:
to_load.add(platform) to_load.add(platform)
if to_load: if to_load:
await asyncio.gather( await self._async_load_platforms(to_load)
*(
create_eager_task(self.async_load_platform(platform))
for platform in to_load
)
)
@callback @callback
def async_update_available_state(self, *_: Any) -> None: def async_update_available_state(self, *_: Any) -> None: