diff --git a/homeassistant/components/sonos/__init__.py b/homeassistant/components/sonos/__init__.py index 028c412cd75..0eab8dcc779 100644 --- a/homeassistant/components/sonos/__init__.py +++ b/homeassistant/components/sonos/__init__.py @@ -230,7 +230,8 @@ class SonosDiscoveryManager: return self.hass.async_create_task( - self.async_add_speakers(zones_to_add, subscription, soco.uid) + self.async_add_speakers(zones_to_add, subscription, soco.uid), + eager_start=True, ) async def async_subscription_failed(now: datetime.datetime) -> None: diff --git a/homeassistant/components/sonos/speaker.py b/homeassistant/components/sonos/speaker.py index ebb2738c641..667e2bb405f 100644 --- a/homeassistant/components/sonos/speaker.py +++ b/homeassistant/components/sonos/speaker.py @@ -407,7 +407,9 @@ 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)) + self.hass.async_create_task( + self._async_renew_failed(exception), eager_start=True + ) async def _async_renew_failed(self, exception: Exception) -> None: """Mark the speaker as offline after a subscription renewal failure. @@ -449,7 +451,9 @@ 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)) + self.hass.async_create_task( + self.alarms.async_process_event(event, self), eager_start=True + ) @callback def async_dispatch_device_properties(self, event: SonosEvent) -> None: @@ -479,7 +483,9 @@ 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)) + self.hass.async_create_task( + self.favorites.async_process_event(event, self), eager_start=True + ) @callback def async_dispatch_media_update(self, event: SonosEvent) -> None: @@ -601,7 +607,7 @@ class SonosSpeaker: self.available = True if not was_available: self.async_write_entity_states() - self.hass.async_create_task(self.async_subscribe()) + self.hass.async_create_task(self.async_subscribe(), eager_start=True) @callback def async_check_activity(self, now: datetime.datetime) -> None: @@ -818,7 +824,9 @@ class SonosSpeaker: if "zone_player_uui_ds_in_group" not in event.variables: return self.event_stats.process(event) - self.hass.async_create_task(self.create_update_groups_coro(event)) + self.hass.async_create_task( + self.create_update_groups_coro(event), eager_start=True + ) def create_update_groups_coro(self, event: SonosEvent | None = None) -> Coroutine: """Handle callback for topology change event.""" diff --git a/tests/components/sonos/conftest.py b/tests/components/sonos/conftest.py index 576c9a80799..4c469028e9a 100644 --- a/tests/components/sonos/conftest.py +++ b/tests/components/sonos/conftest.py @@ -494,6 +494,6 @@ def zgs_event_fixture(hass: HomeAssistant, soco: SoCo, zgs_discovery: str): subscription: SonosMockSubscribe = soco.zoneGroupTopology.subscribe.return_value sub_callback = await subscription.wait_for_callback_to_be_set() sub_callback(event) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) return _wrapper diff --git a/tests/components/sonos/test_sensor.py b/tests/components/sonos/test_sensor.py index 1f4ba8d22cd..45068c01bc0 100644 --- a/tests/components/sonos/test_sensor.py +++ b/tests/components/sonos/test_sensor.py @@ -242,7 +242,6 @@ async def test_favorites_sensor( # Trigger subscription callback for speaker discovery await fire_zgs_event() - await hass.async_block_till_done(wait_background_tasks=True) favorites_updated_event = SonosMockEvent( soco, service, {"favorites_update_id": "2", "container_update_i_ds": "FV:2,2"} diff --git a/tests/components/sonos/test_switch.py b/tests/components/sonos/test_switch.py index 771045c13c1..eb31d991a3a 100644 --- a/tests/components/sonos/test_switch.py +++ b/tests/components/sonos/test_switch.py @@ -117,12 +117,11 @@ async def test_switch_attributes( hass, dt_util.utcnow() + timedelta(seconds=RELOAD_AFTER_UPDATE_DELAY + 1), ) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert m.called # Trigger subscription callback for speaker discovery await fire_zgs_event() - await hass.async_block_till_done(wait_background_tasks=True) status_light_state = hass.states.get(status_light.entity_id) assert status_light_state.state == STATE_ON