Use a background task for LIFX discovery (#88820)

This commit is contained in:
Paulus Schoutsen 2023-02-26 22:42:17 -05:00 committed by GitHub
parent b5223e1196
commit 0d25eef19c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,10 +17,9 @@ from homeassistant.const import (
CONF_HOST,
CONF_PORT,
EVENT_HOMEASSISTANT_STARTED,
EVENT_HOMEASSISTANT_STOP,
Platform,
)
from homeassistant.core import CALLBACK_TYPE, Event, HomeAssistant, callback
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import async_call_later, async_track_time_interval
@ -167,15 +166,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
We do not want the discovery task to block startup.
"""
task = asyncio.create_task(discovery_manager.async_discovery())
@callback
def _async_stop(_: Event) -> None:
if not task.done():
task.cancel()
# Task must be shut down when home assistant is closing
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_stop)
hass.async_create_background_task(
discovery_manager.async_discovery(), "lifx-discovery"
)
# Let the system settle a bit before starting discovery
# to reduce the risk we miss devices because the event