diff --git a/homeassistant/components/flux_led/__init__.py b/homeassistant/components/flux_led/__init__.py index 48115f41f5f..25f8b2554ea 100644 --- a/homeassistant/components/flux_led/__init__.py +++ b/homeassistant/components/flux_led/__init__.py @@ -14,11 +14,9 @@ from homeassistant.const import CONF_HOST, EVENT_HOMEASSISTANT_STARTED, Platform from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import device_registry as dr -from homeassistant.helpers.debounce import Debouncer from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.event import async_track_time_interval from homeassistant.helpers.typing import ConfigType -from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from .const import ( DISCOVER_SCAN_TIMEOUT, @@ -28,6 +26,7 @@ from .const import ( SIGNAL_STATE_UPDATED, STARTUP_SCAN_TIMEOUT, ) +from .coordinator import FluxLedUpdateCoordinator from .discovery import ( async_clear_discovery_cache, async_discover_device, @@ -134,32 +133,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: del hass.data[DOMAIN][entry.entry_id] await device.async_stop() return unload_ok - - -class FluxLedUpdateCoordinator(DataUpdateCoordinator): - """DataUpdateCoordinator to gather data for a specific flux_led device.""" - - def __init__( - self, hass: HomeAssistant, device: AIOWifiLedBulb, entry: ConfigEntry - ) -> None: - """Initialize DataUpdateCoordinator to gather data for specific device.""" - self.device = device - self.entry = entry - super().__init__( - hass, - _LOGGER, - name=self.device.ipaddr, - update_interval=timedelta(seconds=10), - # We don't want an immediate refresh since the device - # takes a moment to reflect the state change - request_refresh_debouncer=Debouncer( - hass, _LOGGER, cooldown=REQUEST_REFRESH_DELAY, immediate=False - ), - ) - - async def _async_update_data(self) -> None: - """Fetch all device and sensor data from api.""" - try: - await self.device.async_update() - except FLUX_LED_EXCEPTIONS as ex: - raise UpdateFailed(ex) from ex diff --git a/homeassistant/components/flux_led/coordinator.py b/homeassistant/components/flux_led/coordinator.py new file mode 100644 index 00000000000..dc2f9ca0fce --- /dev/null +++ b/homeassistant/components/flux_led/coordinator.py @@ -0,0 +1,49 @@ +"""The Flux LED/MagicLight integration coordinator.""" +from __future__ import annotations + +from datetime import timedelta +import logging +from typing import Final + +from flux_led.aio import AIOWifiLedBulb + +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.debounce import Debouncer +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed + +from .const import FLUX_LED_EXCEPTIONS + +_LOGGER = logging.getLogger(__name__) + + +REQUEST_REFRESH_DELAY: Final = 1.5 + + +class FluxLedUpdateCoordinator(DataUpdateCoordinator): + """DataUpdateCoordinator to gather data for a specific flux_led device.""" + + def __init__( + self, hass: HomeAssistant, device: AIOWifiLedBulb, entry: ConfigEntry + ) -> None: + """Initialize DataUpdateCoordinator to gather data for specific device.""" + self.device = device + self.entry = entry + super().__init__( + hass, + _LOGGER, + name=self.device.ipaddr, + update_interval=timedelta(seconds=10), + # We don't want an immediate refresh since the device + # takes a moment to reflect the state change + request_refresh_debouncer=Debouncer( + hass, _LOGGER, cooldown=REQUEST_REFRESH_DELAY, immediate=False + ), + ) + + async def _async_update_data(self) -> None: + """Fetch all device and sensor data from api.""" + try: + await self.device.async_update() + except FLUX_LED_EXCEPTIONS as ex: + raise UpdateFailed(ex) from ex diff --git a/homeassistant/components/flux_led/entity.py b/homeassistant/components/flux_led/entity.py index 35ec8087dc4..1589bd0fb3a 100644 --- a/homeassistant/components/flux_led/entity.py +++ b/homeassistant/components/flux_led/entity.py @@ -14,8 +14,8 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import DeviceInfo, Entity from homeassistant.helpers.update_coordinator import CoordinatorEntity -from . import FluxLedUpdateCoordinator from .const import CONF_MINOR_VERSION, CONF_MODEL, SIGNAL_STATE_UPDATED +from .coordinator import FluxLedUpdateCoordinator def _async_device_info( diff --git a/homeassistant/components/flux_led/light.py b/homeassistant/components/flux_led/light.py index ba454092d42..a3811d0c4e3 100644 --- a/homeassistant/components/flux_led/light.py +++ b/homeassistant/components/flux_led/light.py @@ -48,7 +48,6 @@ from homeassistant.util.color import ( color_temperature_mired_to_kelvin, ) -from . import FluxLedUpdateCoordinator from .const import ( CONF_AUTOMATIC_ADD, CONF_COLORS, @@ -69,6 +68,7 @@ from .const import ( TRANSITION_JUMP, TRANSITION_STROBE, ) +from .coordinator import FluxLedUpdateCoordinator from .entity import FluxOnOffEntity from .util import _effect_brightness, _flux_color_mode_to_hass, _hass_color_modes diff --git a/homeassistant/components/flux_led/number.py b/homeassistant/components/flux_led/number.py index 28007181e5c..b19e6b0e048 100644 --- a/homeassistant/components/flux_led/number.py +++ b/homeassistant/components/flux_led/number.py @@ -11,8 +11,8 @@ from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity -from . import FluxLedUpdateCoordinator from .const import DOMAIN, EFFECT_SPEED_SUPPORT_MODES +from .coordinator import FluxLedUpdateCoordinator from .entity import FluxEntity from .util import _effect_brightness, _hass_color_modes diff --git a/homeassistant/components/flux_led/switch.py b/homeassistant/components/flux_led/switch.py index 8f499374b82..8dc5079d7ec 100644 --- a/homeassistant/components/flux_led/switch.py +++ b/homeassistant/components/flux_led/switch.py @@ -14,13 +14,13 @@ from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity -from . import FluxLedUpdateCoordinator from .const import ( CONF_REMOTE_ACCESS_ENABLED, CONF_REMOTE_ACCESS_HOST, CONF_REMOTE_ACCESS_PORT, DOMAIN, ) +from .coordinator import FluxLedUpdateCoordinator from .discovery import async_clear_discovery_cache from .entity import FluxBaseEntity, FluxOnOffEntity