Iterate over entities safely in camera setup (#82080)

fixes undefined
This commit is contained in:
uvjustin 2022-11-15 22:02:35 +08:00 committed by GitHub
parent 4cce359960
commit e7dd31f37b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View file

@ -359,7 +359,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
await component.async_setup(config)
async def preload_stream(_event: Event) -> None:
for camera in component.entities:
for camera in list(component.entities):
stream_prefs = await prefs.get_dynamic_stream_settings(camera.entity_id)
if not stream_prefs.preload_stream:
continue

View file

@ -90,7 +90,12 @@ class EntityComponent(Generic[_EntityT]):
@property
def entities(self) -> Iterable[_EntityT]:
"""Return an iterable that returns all entities."""
"""
Return an iterable that returns all entities.
As the underlying dicts may change when async context is lost, callers that
iterate over this asynchronously should make a copy using list() before iterating.
"""
return chain.from_iterable(
platform.entities.values() # type: ignore[misc]
for platform in self._platforms.values()