diff --git a/zha/application/gateway.py b/zha/application/gateway.py index 3a65681f..ece23336 100644 --- a/zha/application/gateway.py +++ b/zha/application/gateway.py @@ -4,7 +4,7 @@ import asyncio from collections.abc import AsyncGenerator -from contextlib import suppress +from contextlib import asynccontextmanager, suppress from dataclasses import dataclass from datetime import timedelta from enum import Enum @@ -275,6 +275,14 @@ async def async_initialize(self) -> None: await self.shutdown() raise + @asynccontextmanager + async def request_priority( + self, priority: t.PacketPriority | None + ) -> AsyncGenerator[None, None]: + """Context manager to request a priority for radio access.""" + async with self.application_controller.request_priority(priority): + yield + def connection_lost(self, exc: Exception) -> None: """Handle connection lost event.""" _LOGGER.debug("Connection to the radio was lost: %r", exc) @@ -371,7 +379,8 @@ async def async_initialize_devices_and_entities(self) -> None: async def fetch_updated_state() -> None: """Fetch updated state for mains powered devices.""" if self.config.config.device_options.enable_mains_startup_polling: - await self.async_fetch_updated_state_mains() + async with self.request_priority(t.PacketPriority.LOW): + await self.async_fetch_updated_state_mains() else: _LOGGER.debug("Polling of mains powered devices at startup is disabled") _LOGGER.debug("Allowing polled requests") @@ -615,8 +624,9 @@ async def _async_device_joined(self, zha_device: Device) -> None: zha_device.available = True zha_device.on_network = True - await zha_device.async_configure() - await zha_device.async_initialize() + async with self.request_priority(t.PacketPriority.HIGH): + await zha_device.async_configure() + await zha_device.async_initialize() self.emit( ZHA_GW_MSG_DEVICE_FULL_INIT,