Handle CancelledError in bluesound integration (#124873)

Catch CancledError in async_will_remove_from_hass
This commit is contained in:
Louis Christ 2024-08-30 11:38:07 +02:00 committed by GitHub
parent 6781a76de2
commit f394dfb8d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -309,7 +309,7 @@ class BluesoundPlayer(MediaPlayerEntity):
return True return True
async def _start_poll_command(self): async def _poll_loop(self):
"""Loop which polls the status of the player.""" """Loop which polls the status of the player."""
while True: while True:
try: try:
@ -335,7 +335,7 @@ class BluesoundPlayer(MediaPlayerEntity):
await super().async_added_to_hass() await super().async_added_to_hass()
self._polling_task = self.hass.async_create_background_task( self._polling_task = self.hass.async_create_background_task(
self._start_poll_command(), self._poll_loop(),
name=f"bluesound.polling_{self.host}:{self.port}", name=f"bluesound.polling_{self.host}:{self.port}",
) )
@ -345,7 +345,9 @@ class BluesoundPlayer(MediaPlayerEntity):
assert self._polling_task is not None assert self._polling_task is not None
if self._polling_task.cancel(): if self._polling_task.cancel():
await self._polling_task # the sleeps in _poll_loop will raise CancelledError
with suppress(CancelledError):
await self._polling_task
self.hass.data[DATA_BLUESOUND].remove(self) self.hass.data[DATA_BLUESOUND].remove(self)