Fix matrix blocking call by running sync_forever in background_task (#122800)

Fix blocking call by running sync_forever in background_task
This commit is contained in:
Paarth Shah 2024-07-31 00:44:59 -07:00 committed by GitHub
parent 7f4dabf546
commit e0a1aaa1b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -209,15 +209,22 @@ class MatrixBot:
await self._resolve_room_aliases(listening_rooms) await self._resolve_room_aliases(listening_rooms)
self._load_commands(commands) self._load_commands(commands)
await self._join_rooms() await self._join_rooms()
# Sync once so that we don't respond to past events. # Sync once so that we don't respond to past events.
_LOGGER.debug("Starting initial sync for %s", self._mx_id)
await self._client.sync(timeout=30_000) await self._client.sync(timeout=30_000)
_LOGGER.debug("Finished initial sync for %s", self._mx_id)
self._client.add_event_callback(self._handle_room_message, RoomMessageText) self._client.add_event_callback(self._handle_room_message, RoomMessageText)
await self._client.sync_forever( _LOGGER.debug("Starting sync_forever for %s", self._mx_id)
timeout=30_000, self.hass.async_create_background_task(
loop_sleep_time=1_000, self._client.sync_forever(
) # milliseconds. timeout=30_000,
loop_sleep_time=1_000,
), # milliseconds.
name=f"{self.__class__.__name__}: sync_forever for '{self._mx_id}'",
)
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, handle_startup) self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, handle_startup)