Add a lock to homekit_controller platform loads (#116539)

This commit is contained in:
J. Nick Koston 2024-05-01 19:23:43 -05:00 committed by GitHub
parent 713ce0dd17
commit 657c9ec25b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -153,6 +153,7 @@ class HKDevice:
self._subscriptions: dict[tuple[int, int], set[CALLBACK_TYPE]] = {} self._subscriptions: dict[tuple[int, int], set[CALLBACK_TYPE]] = {}
self._pending_subscribes: set[tuple[int, int]] = set() self._pending_subscribes: set[tuple[int, int]] = set()
self._subscribe_timer: CALLBACK_TYPE | None = None self._subscribe_timer: CALLBACK_TYPE | None = None
self._load_platforms_lock = asyncio.Lock()
@property @property
def entity_map(self) -> Accessories: def entity_map(self) -> Accessories:
@ -327,6 +328,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:
async with self._load_platforms_lock:
await self._async_load_platforms({"sensor"}) await self._async_load_platforms({"sensor"})
@callback @callback
@ -804,6 +806,7 @@ class HKDevice:
async def _async_load_platforms(self, platforms: set[str]) -> None: async def _async_load_platforms(self, platforms: set[str]) -> None:
"""Load a group of platforms.""" """Load a group of platforms."""
assert self._load_platforms_lock.locked(), "Must be called with lock held"
if not (to_load := platforms - self.platforms): if not (to_load := platforms - self.platforms):
return return
self.platforms.update(to_load) self.platforms.update(to_load)
@ -813,6 +816,7 @@ class HKDevice:
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."""
async with self._load_platforms_lock:
to_load: set[str] = set() to_load: set[str] = set()
for accessory in self.entity_map.accessories: for accessory in self.entity_map.accessories:
for service in accessory.services: for service in accessory.services: