Ensure instances of async_add_entities can be garbage collected (#42919)
parent
7f640c4a2e
commit
3ec7258c21
|
@ -63,6 +63,8 @@ class EntityPlatform:
|
||||||
self.config_entry: Optional[config_entries.ConfigEntry] = None
|
self.config_entry: Optional[config_entries.ConfigEntry] = None
|
||||||
self.entities: Dict[str, Entity] = {} # pylint: disable=used-before-assignment
|
self.entities: Dict[str, Entity] = {} # pylint: disable=used-before-assignment
|
||||||
self._tasks: List[asyncio.Future] = []
|
self._tasks: List[asyncio.Future] = []
|
||||||
|
# Stop tracking tasks after setup is completed
|
||||||
|
self._setup_complete = False
|
||||||
# Method to cancel the state change listener
|
# Method to cancel the state change listener
|
||||||
self._async_unsub_polling: Optional[CALLBACK_TYPE] = None
|
self._async_unsub_polling: Optional[CALLBACK_TYPE] = None
|
||||||
# Method to cancel the retry of setup
|
# Method to cancel the retry of setup
|
||||||
|
@ -197,7 +199,7 @@ class EntityPlatform:
|
||||||
await asyncio.shield(task)
|
await asyncio.shield(task)
|
||||||
|
|
||||||
# Block till all entities are done
|
# Block till all entities are done
|
||||||
if self._tasks:
|
while self._tasks:
|
||||||
pending = [task for task in self._tasks if not task.done()]
|
pending = [task for task in self._tasks if not task.done()]
|
||||||
self._tasks.clear()
|
self._tasks.clear()
|
||||||
|
|
||||||
|
@ -205,6 +207,7 @@ class EntityPlatform:
|
||||||
await asyncio.gather(*pending)
|
await asyncio.gather(*pending)
|
||||||
|
|
||||||
hass.config.components.add(full_name)
|
hass.config.components.add(full_name)
|
||||||
|
self._setup_complete = True
|
||||||
return True
|
return True
|
||||||
except PlatformNotReady:
|
except PlatformNotReady:
|
||||||
tries += 1
|
tries += 1
|
||||||
|
@ -258,14 +261,13 @@ class EntityPlatform:
|
||||||
self, new_entities: Iterable["Entity"], update_before_add: bool = False
|
self, new_entities: Iterable["Entity"], update_before_add: bool = False
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Schedule adding entities for a single platform async."""
|
"""Schedule adding entities for a single platform async."""
|
||||||
self._tasks.append(
|
task = self.hass.async_create_task(
|
||||||
self.hass.async_create_task(
|
self.async_add_entities(new_entities, update_before_add=update_before_add),
|
||||||
self.async_add_entities(
|
|
||||||
new_entities, update_before_add=update_before_add
|
|
||||||
),
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if not self._setup_complete:
|
||||||
|
self._tasks.append(task)
|
||||||
|
|
||||||
def add_entities(
|
def add_entities(
|
||||||
self, new_entities: Iterable["Entity"], update_before_add: bool = False
|
self, new_entities: Iterable["Entity"], update_before_add: bool = False
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -523,6 +525,7 @@ class EntityPlatform:
|
||||||
if self._async_unsub_polling is not None:
|
if self._async_unsub_polling is not None:
|
||||||
self._async_unsub_polling()
|
self._async_unsub_polling()
|
||||||
self._async_unsub_polling = None
|
self._async_unsub_polling = None
|
||||||
|
self._setup_complete = False
|
||||||
|
|
||||||
async def async_destroy(self) -> None:
|
async def async_destroy(self) -> None:
|
||||||
"""Destroy an entity platform.
|
"""Destroy an entity platform.
|
||||||
|
|
Loading…
Reference in New Issue