diff --git a/homeassistant/components/flux_led/__init__.py b/homeassistant/components/flux_led/__init__.py index b82fbe97913..b37a3640db3 100644 --- a/homeassistant/components/flux_led/__init__.py +++ b/homeassistant/components/flux_led/__init__.py @@ -1,6 +1,7 @@ """The Flux LED/MagicLight integration.""" from __future__ import annotations +import asyncio from datetime import timedelta import logging from typing import Any, Final, cast @@ -26,6 +27,7 @@ from .const import ( DISCOVER_SCAN_TIMEOUT, DOMAIN, FLUX_LED_DISCOVERY, + FLUX_LED_DISCOVERY_LOCK, FLUX_LED_EXCEPTIONS, SIGNAL_STATE_UPDATED, STARTUP_SCAN_TIMEOUT, @@ -76,16 +78,20 @@ async def async_discover_devices( hass: HomeAssistant, timeout: int, address: str | None = None ) -> list[dict[str, str]]: """Discover flux led devices.""" - scanner = AIOBulbScanner() - try: - discovered: list[dict[str, str]] = await scanner.async_scan( - timeout=timeout, address=address - ) - except OSError as ex: - _LOGGER.debug("Scanning failed with error: %s", ex) - return [] - else: - return discovered + domain_data = hass.data.setdefault(DOMAIN, {}) + if FLUX_LED_DISCOVERY_LOCK not in domain_data: + domain_data[FLUX_LED_DISCOVERY_LOCK] = asyncio.Lock() + async with domain_data[FLUX_LED_DISCOVERY_LOCK]: + scanner = AIOBulbScanner() + try: + discovered: list[dict[str, str]] = await scanner.async_scan( + timeout=timeout, address=address + ) + except OSError as ex: + _LOGGER.debug("Scanning failed with error: %s", ex) + return [] + else: + return discovered async def async_discover_device( @@ -118,7 +124,7 @@ def async_trigger_discovery( async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the flux_led component.""" - domain_data = hass.data[DOMAIN] = {} + domain_data = hass.data.setdefault(DOMAIN, {}) domain_data[FLUX_LED_DISCOVERY] = await async_discover_devices( hass, STARTUP_SCAN_TIMEOUT ) diff --git a/homeassistant/components/flux_led/const.py b/homeassistant/components/flux_led/const.py index 639bac7165e..88c50402a05 100644 --- a/homeassistant/components/flux_led/const.py +++ b/homeassistant/components/flux_led/const.py @@ -39,6 +39,7 @@ DEFAULT_SCAN_INTERVAL: Final = 5 DEFAULT_EFFECT_SPEED: Final = 50 FLUX_LED_DISCOVERY: Final = "flux_led_discovery" +FLUX_LED_DISCOVERY_LOCK: Final = "flux_led_discovery_lock" FLUX_LED_EXCEPTIONS: Final = ( asyncio.TimeoutError,