Add a lock to homekit_controller platform loads (#116539)

pull/116512/head
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
1 changed files with 18 additions and 14 deletions

View File

@ -153,6 +153,7 @@ class HKDevice:
self._subscriptions: dict[tuple[int, int], set[CALLBACK_TYPE]] = {}
self._pending_subscribes: set[tuple[int, int]] = set()
self._subscribe_timer: CALLBACK_TYPE | None = None
self._load_platforms_lock = asyncio.Lock()
@property
def entity_map(self) -> Accessories:
@ -327,6 +328,7 @@ class HKDevice:
)
# BLE devices always get an RSSI sensor as well
if "sensor" not in self.platforms:
async with self._load_platforms_lock:
await self._async_load_platforms({"sensor"})
@callback
@ -804,6 +806,7 @@ class HKDevice:
async def _async_load_platforms(self, platforms: set[str]) -> None:
"""Load a group of platforms."""
assert self._load_platforms_lock.locked(), "Must be called with lock held"
if not (to_load := platforms - self.platforms):
return
self.platforms.update(to_load)
@ -813,6 +816,7 @@ class HKDevice:
async def async_load_platforms(self) -> None:
"""Load any platforms needed by this HomeKit device."""
async with self._load_platforms_lock:
to_load: set[str] = set()
for accessory in self.entity_map.accessories:
for service in accessory.services: