Fix flux_led blocking startup by waiting for discovery (#111787)

* Avoid blocking startup by waiting for discovery in flux_led

* remove started discovery
This commit is contained in:
J. Nick Koston 2024-02-28 16:16:38 -10:00 committed by GitHub
parent b40978597c
commit b11e97e132
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 20 deletions

View file

@ -11,7 +11,7 @@ from flux_led.const import ATTR_ID, WhiteChannelType
from flux_led.scanner import FluxLEDDiscovery from flux_led.scanner import FluxLEDDiscovery
from homeassistant.config_entries import ConfigEntry 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.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import ( from homeassistant.helpers import (
@ -37,7 +37,6 @@ from .const import (
FLUX_LED_DISCOVERY_SIGNAL, FLUX_LED_DISCOVERY_SIGNAL,
FLUX_LED_EXCEPTIONS, FLUX_LED_EXCEPTIONS,
SIGNAL_STATE_UPDATED, SIGNAL_STATE_UPDATED,
STARTUP_SCAN_TIMEOUT,
) )
from .coordinator import FluxLedUpdateCoordinator from .coordinator import FluxLedUpdateCoordinator
from .discovery import ( from .discovery import (
@ -89,24 +88,21 @@ def async_wifi_bulb_for_host(
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the flux_led component.""" """Set up the flux_led component."""
domain_data = hass.data.setdefault(DOMAIN, {}) domain_data = hass.data.setdefault(DOMAIN, {})
domain_data[FLUX_LED_DISCOVERY] = await async_discover_devices( domain_data[FLUX_LED_DISCOVERY] = []
hass, STARTUP_SCAN_TIMEOUT
)
@callback @callback
def _async_start_background_discovery(*_: Any) -> None: def _async_start_background_discovery(*_: Any) -> None:
"""Run discovery in the background.""" """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 def _async_discovery(*_: Any) -> None:
async_trigger_discovery( async_trigger_discovery(
hass, await async_discover_devices(hass, DISCOVER_SCAN_TIMEOUT) hass, await async_discover_devices(hass, DISCOVER_SCAN_TIMEOUT)
) )
async_trigger_discovery(hass, domain_data[FLUX_LED_DISCOVERY]) _async_start_background_discovery()
hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STARTED, _async_start_background_discovery
)
async_track_time_interval( async_track_time_interval(
hass, hass,
_async_start_background_discovery, _async_start_background_discovery,

View file

@ -19,7 +19,6 @@ from homeassistant.const import (
ATTR_FRIENDLY_NAME, ATTR_FRIENDLY_NAME,
CONF_HOST, CONF_HOST,
CONF_NAME, CONF_NAME,
EVENT_HOMEASSISTANT_STARTED,
STATE_ON, STATE_ON,
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
) )
@ -57,13 +56,10 @@ async def test_configuring_flux_led_causes_discovery(hass: HomeAssistant) -> Non
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(scan.mock_calls) == 1 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) async_fire_time_changed(hass, utcnow() + flux_led.DISCOVERY_INTERVAL)
await hass.async_block_till_done() 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") @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] discover.return_value = [FLUX_DISCOVERY]
await async_setup_component(hass, flux_led.DOMAIN, {flux_led.DOMAIN: {}}) await async_setup_component(hass, flux_led.DOMAIN, {flux_led.DOMAIN: {}})
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(scan.mock_calls) == 2 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) async_fire_time_changed(hass, utcnow() + flux_led.DISCOVERY_INTERVAL)
await hass.async_block_till_done() 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: async def test_config_entry_reload(hass: HomeAssistant) -> None: