diff --git a/homeassistant/components/cast/media_player.py b/homeassistant/components/cast/media_player.py index 29aab64b714..0540380bc99 100644 --- a/homeassistant/components/cast/media_player.py +++ b/homeassistant/components/cast/media_player.py @@ -1,7 +1,6 @@ """Provide functionality to interact with Cast devices on the network.""" from __future__ import annotations -import asyncio from collections.abc import Callable from contextlib import suppress from datetime import datetime @@ -186,10 +185,11 @@ class CastDevice: self.hass, SIGNAL_CAST_REMOVED, self._async_cast_removed ) self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self._async_stop) - # asyncio.create_task is used to avoid delaying startup wrapup if the device + # async_create_background_task is used to avoid delaying startup wrapup if the device # is discovered already during startup but then fails to respond - asyncio.create_task( - async_create_catching_coro(self._async_connect_to_chromecast()) + self.hass.async_create_background_task( + async_create_catching_coro(self._async_connect_to_chromecast()), + "cast-connect", ) async def _async_tear_down(self) -> None: diff --git a/tests/components/cast/test_media_player.py b/tests/components/cast/test_media_player.py index 6aedab9d17c..eea8c050888 100644 --- a/tests/components/cast/test_media_player.py +++ b/tests/components/cast/test_media_player.py @@ -579,15 +579,16 @@ async def test_discover_dynamic_group( tasks = [] real_create_task = asyncio.create_task - def create_task(*args, **kwargs): - tasks.append(real_create_task(*args, **kwargs)) + def create_task(coroutine, name): + tasks.append(real_create_task(coroutine)) # Discover cast service with patch( "homeassistant.components.cast.discovery.ChromeCastZeroconf.get_zeroconf", return_value=zconf_1, - ), patch( - "homeassistant.components.cast.media_player.asyncio.create_task", + ), patch.object( + hass, + "async_create_background_task", wraps=create_task, ): discover_cast( @@ -611,8 +612,9 @@ async def test_discover_dynamic_group( with patch( "homeassistant.components.cast.discovery.ChromeCastZeroconf.get_zeroconf", return_value=zconf_2, - ), patch( - "homeassistant.components.cast.media_player.asyncio.create_task", + ), patch.object( + hass, + "async_create_background_task", wraps=create_task, ): discover_cast( @@ -636,8 +638,9 @@ async def test_discover_dynamic_group( with patch( "homeassistant.components.cast.discovery.ChromeCastZeroconf.get_zeroconf", return_value=zconf_1, - ), patch( - "homeassistant.components.cast.media_player.asyncio.create_task", + ), patch.object( + hass, + "async_create_background_task", wraps=create_task, ): discover_cast(