1
mirror of https://github.com/home-assistant/core synced 2024-09-12 15:16:21 +02:00

Fix dangling task for cast (#88285)

This commit is contained in:
Paulus Schoutsen 2023-02-17 12:43:40 -05:00 committed by GitHub
parent da046f93f4
commit f465561536
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 12 deletions

View File

@ -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:

View File

@ -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(