Fix flux_led blocking startup by waiting for discovery (#111787)
* Avoid blocking startup by waiting for discovery in flux_led * remove started discoverypull/111791/head
parent
b40978597c
commit
b11e97e132
|
@ -11,7 +11,7 @@ from flux_led.const import ATTR_ID, WhiteChannelType
|
|||
from flux_led.scanner import FluxLEDDiscovery
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_HOST, EVENT_HOMEASSISTANT_STARTED, Platform
|
||||
from homeassistant.const import CONF_HOST, Platform
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers import (
|
||||
|
@ -37,7 +37,6 @@ from .const import (
|
|||
FLUX_LED_DISCOVERY_SIGNAL,
|
||||
FLUX_LED_EXCEPTIONS,
|
||||
SIGNAL_STATE_UPDATED,
|
||||
STARTUP_SCAN_TIMEOUT,
|
||||
)
|
||||
from .coordinator import FluxLedUpdateCoordinator
|
||||
from .discovery import (
|
||||
|
@ -89,24 +88,21 @@ def async_wifi_bulb_for_host(
|
|||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Set up the flux_led component."""
|
||||
domain_data = hass.data.setdefault(DOMAIN, {})
|
||||
domain_data[FLUX_LED_DISCOVERY] = await async_discover_devices(
|
||||
hass, STARTUP_SCAN_TIMEOUT
|
||||
)
|
||||
domain_data[FLUX_LED_DISCOVERY] = []
|
||||
|
||||
@callback
|
||||
def _async_start_background_discovery(*_: Any) -> None:
|
||||
"""Run discovery in the background."""
|
||||
hass.async_create_background_task(_async_discovery(), "flux_led-discovery")
|
||||
hass.async_create_background_task(
|
||||
_async_discovery(), "flux_led-discovery", eager_start=True
|
||||
)
|
||||
|
||||
async def _async_discovery(*_: Any) -> None:
|
||||
async_trigger_discovery(
|
||||
hass, await async_discover_devices(hass, DISCOVER_SCAN_TIMEOUT)
|
||||
)
|
||||
|
||||
async_trigger_discovery(hass, domain_data[FLUX_LED_DISCOVERY])
|
||||
hass.bus.async_listen_once(
|
||||
EVENT_HOMEASSISTANT_STARTED, _async_start_background_discovery
|
||||
)
|
||||
_async_start_background_discovery()
|
||||
async_track_time_interval(
|
||||
hass,
|
||||
_async_start_background_discovery,
|
||||
|
|
|
@ -19,7 +19,6 @@ from homeassistant.const import (
|
|||
ATTR_FRIENDLY_NAME,
|
||||
CONF_HOST,
|
||||
CONF_NAME,
|
||||
EVENT_HOMEASSISTANT_STARTED,
|
||||
STATE_ON,
|
||||
STATE_UNAVAILABLE,
|
||||
)
|
||||
|
@ -57,13 +56,10 @@ async def test_configuring_flux_led_causes_discovery(hass: HomeAssistant) -> Non
|
|||
await hass.async_block_till_done()
|
||||
|
||||
assert len(scan.mock_calls) == 1
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
||||
await hass.async_block_till_done()
|
||||
assert len(scan.mock_calls) == 2
|
||||
|
||||
async_fire_time_changed(hass, utcnow() + flux_led.DISCOVERY_INTERVAL)
|
||||
await hass.async_block_till_done()
|
||||
assert len(scan.mock_calls) == 3
|
||||
assert len(scan.mock_calls) == 2
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_multiple_broadcast_addresses")
|
||||
|
@ -79,15 +75,11 @@ async def test_configuring_flux_led_causes_discovery_multiple_addresses(
|
|||
discover.return_value = [FLUX_DISCOVERY]
|
||||
await async_setup_component(hass, flux_led.DOMAIN, {flux_led.DOMAIN: {}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(scan.mock_calls) == 2
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
||||
await hass.async_block_till_done()
|
||||
assert len(scan.mock_calls) == 4
|
||||
|
||||
async_fire_time_changed(hass, utcnow() + flux_led.DISCOVERY_INTERVAL)
|
||||
await hass.async_block_till_done()
|
||||
assert len(scan.mock_calls) == 6
|
||||
assert len(scan.mock_calls) == 4
|
||||
|
||||
|
||||
async def test_config_entry_reload(hass: HomeAssistant) -> None:
|
||||
|
|
Loading…
Reference in New Issue