From 6786479a816af20c31df215cf2d510299a971e12 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 28 Apr 2024 08:54:34 -0500 Subject: [PATCH] Fix sonos events delaying shutdown (#116337) --- homeassistant/components/sonos/speaker.py | 22 ++++++++++++++-------- tests/components/sonos/test_switch.py | 4 ++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/sonos/speaker.py b/homeassistant/components/sonos/speaker.py index 667e2bb405f..e2529ddfe94 100644 --- a/homeassistant/components/sonos/speaker.py +++ b/homeassistant/components/sonos/speaker.py @@ -407,8 +407,8 @@ class SonosSpeaker: @callback def async_renew_failed(self, exception: Exception) -> None: """Handle a failed subscription renewal.""" - self.hass.async_create_task( - self._async_renew_failed(exception), eager_start=True + self.hass.async_create_background_task( + self._async_renew_failed(exception), "sonos renew failed", eager_start=True ) async def _async_renew_failed(self, exception: Exception) -> None: @@ -451,16 +451,20 @@ class SonosSpeaker: """Add the soco instance associated with the event to the callback.""" if "alarm_list_version" not in event.variables: return - self.hass.async_create_task( - self.alarms.async_process_event(event, self), eager_start=True + self.hass.async_create_background_task( + self.alarms.async_process_event(event, self), + "sonos process event", + eager_start=True, ) @callback def async_dispatch_device_properties(self, event: SonosEvent) -> None: """Update device properties from an event.""" self.event_stats.process(event) - self.hass.async_create_task( - self.async_update_device_properties(event), eager_start=True + self.hass.async_create_background_task( + self.async_update_device_properties(event), + "sonos device properties", + eager_start=True, ) async def async_update_device_properties(self, event: SonosEvent) -> None: @@ -483,8 +487,10 @@ class SonosSpeaker: return if "container_update_i_ds" not in event.variables: return - self.hass.async_create_task( - self.favorites.async_process_event(event, self), eager_start=True + self.hass.async_create_background_task( + self.favorites.async_process_event(event, self), + "sonos dispatch favorites", + eager_start=True, ) @callback diff --git a/tests/components/sonos/test_switch.py b/tests/components/sonos/test_switch.py index eb31d991a3a..d6814886d55 100644 --- a/tests/components/sonos/test_switch.py +++ b/tests/components/sonos/test_switch.py @@ -157,7 +157,7 @@ async def test_alarm_create_delete( alarm_event.variables["alarm_list_version"] = two_alarms["CurrentAlarmListVersion"] sub_callback(event=alarm_event) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert "switch.sonos_alarm_14" in entity_registry.entities assert "switch.sonos_alarm_15" in entity_registry.entities @@ -169,7 +169,7 @@ async def test_alarm_create_delete( alarm_clock.ListAlarms.return_value = one_alarm sub_callback(event=alarm_event) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert "switch.sonos_alarm_14" in entity_registry.entities assert "switch.sonos_alarm_15" not in entity_registry.entities