Switch imap push coordinator to use eager_start (#115454)

When I turned on eager_start here the data would always end up being
None because _async_update_data always returned None. To fix this
it now returns the value from the push loop. It appears this race
would happen in production so this may be a bugfix but since
I do not use this integration it could use a second set of eyes
This commit is contained in:
J. Nick Koston 2024-04-11 21:14:35 -10:00 committed by GitHub
parent 9bf87329da
commit f3a3e6821b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -443,23 +443,24 @@ class ImapPushDataUpdateCoordinator(ImapDataUpdateCoordinator):
_LOGGER.debug("Connected to server %s using IMAP push", entry.data[CONF_SERVER])
super().__init__(hass, imap_client, entry, None)
self._push_wait_task: asyncio.Task[None] | None = None
self.number_of_messages: int | None = None
async def _async_update_data(self) -> int | None:
"""Update the number of unread emails."""
await self.async_start()
return None
return self.number_of_messages
async def async_start(self) -> None:
"""Start coordinator."""
self._push_wait_task = self.hass.async_create_background_task(
self._async_wait_push_loop(), "Wait for IMAP data push", eager_start=False
self._async_wait_push_loop(), "Wait for IMAP data push"
)
async def _async_wait_push_loop(self) -> None:
"""Wait for data push from server."""
while True:
try:
number_of_messages = await self._async_fetch_number_of_messages()
self.number_of_messages = await self._async_fetch_number_of_messages()
except InvalidAuth as ex:
self.auth_errors += 1
await self._cleanup()
@ -489,7 +490,7 @@ class ImapPushDataUpdateCoordinator(ImapDataUpdateCoordinator):
continue
else:
self.auth_errors = 0
self.async_set_updated_data(number_of_messages)
self.async_set_updated_data(self.number_of_messages)
try:
idle: asyncio.Future = await self.imap_client.idle_start()
await self.imap_client.wait_server_push()