mirror of
https://github.com/home-assistant/core.git
synced 2026-04-02 08:26:41 +01:00
Move LED BLE coordinator to separate module (#164749)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,25 +3,20 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from led_ble import BLEAK_EXCEPTIONS, LEDBLE
|
||||
from led_ble import LEDBLE
|
||||
|
||||
from homeassistant.components import bluetooth
|
||||
from homeassistant.components.bluetooth.match import ADDRESS, BluetoothCallbackMatcher
|
||||
from homeassistant.const import CONF_ADDRESS, EVENT_HOMEASSISTANT_STOP, Platform
|
||||
from homeassistant.core import Event, HomeAssistant, callback
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .const import DEVICE_TIMEOUT, UPDATE_SECONDS
|
||||
from .models import LEDBLEConfigEntry, LEDBLEData
|
||||
from .const import DEVICE_TIMEOUT
|
||||
from .coordinator import LEDBLEConfigEntry, LEDBLECoordinator, LEDBLEData
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.LIGHT]
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: LEDBLEConfigEntry) -> bool:
|
||||
"""Set up LED BLE from a config entry."""
|
||||
@@ -53,23 +48,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: LEDBLEConfigEntry) -> bo
|
||||
)
|
||||
)
|
||||
|
||||
async def _async_update() -> None:
|
||||
"""Update the device state."""
|
||||
try:
|
||||
await led_ble.update()
|
||||
except BLEAK_EXCEPTIONS as ex:
|
||||
raise UpdateFailed(str(ex)) from ex
|
||||
|
||||
startup_event = asyncio.Event()
|
||||
cancel_first_update = led_ble.register_callback(lambda *_: startup_event.set())
|
||||
coordinator = DataUpdateCoordinator(
|
||||
hass,
|
||||
_LOGGER,
|
||||
config_entry=entry,
|
||||
name=led_ble.name,
|
||||
update_method=_async_update,
|
||||
update_interval=timedelta(seconds=UPDATE_SECONDS),
|
||||
)
|
||||
coordinator = LEDBLECoordinator(hass, entry, led_ble)
|
||||
|
||||
try:
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
58
homeassistant/components/led_ble/coordinator.py
Normal file
58
homeassistant/components/led_ble/coordinator.py
Normal file
@@ -0,0 +1,58 @@
|
||||
"""The LED BLE coordinator."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from led_ble import BLEAK_EXCEPTIONS, LEDBLE
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .const import UPDATE_SECONDS
|
||||
|
||||
type LEDBLEConfigEntry = ConfigEntry[LEDBLEData]
|
||||
|
||||
|
||||
@dataclass
|
||||
class LEDBLEData:
|
||||
"""Data for the led ble integration."""
|
||||
|
||||
title: str
|
||||
device: LEDBLE
|
||||
coordinator: LEDBLECoordinator
|
||||
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class LEDBLECoordinator(DataUpdateCoordinator[None]):
|
||||
"""Class to manage fetching LED BLE data."""
|
||||
|
||||
config_entry: LEDBLEConfigEntry
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
entry: LEDBLEConfigEntry,
|
||||
led_ble: LEDBLE,
|
||||
) -> None:
|
||||
"""Initialize the coordinator."""
|
||||
super().__init__(
|
||||
hass,
|
||||
_LOGGER,
|
||||
config_entry=entry,
|
||||
name=led_ble.name,
|
||||
update_interval=timedelta(seconds=UPDATE_SECONDS),
|
||||
)
|
||||
self.led_ble = led_ble
|
||||
|
||||
async def _async_update_data(self) -> None:
|
||||
"""Update the device state."""
|
||||
try:
|
||||
await self.led_ble.update()
|
||||
except BLEAK_EXCEPTIONS as ex:
|
||||
raise UpdateFailed(str(ex)) from ex
|
||||
@@ -19,13 +19,10 @@ from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import (
|
||||
CoordinatorEntity,
|
||||
DataUpdateCoordinator,
|
||||
)
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DEFAULT_EFFECT_SPEED
|
||||
from .models import LEDBLEConfigEntry
|
||||
from .coordinator import LEDBLEConfigEntry, LEDBLECoordinator
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
@@ -38,7 +35,7 @@ async def async_setup_entry(
|
||||
async_add_entities([LEDBLEEntity(data.coordinator, data.device, entry.title)])
|
||||
|
||||
|
||||
class LEDBLEEntity(CoordinatorEntity[DataUpdateCoordinator[None]], LightEntity):
|
||||
class LEDBLEEntity(CoordinatorEntity[LEDBLECoordinator], LightEntity):
|
||||
"""Representation of LEDBLE device."""
|
||||
|
||||
_attr_supported_color_modes = {ColorMode.RGB, ColorMode.WHITE}
|
||||
@@ -47,7 +44,7 @@ class LEDBLEEntity(CoordinatorEntity[DataUpdateCoordinator[None]], LightEntity):
|
||||
_attr_supported_features = LightEntityFeature.EFFECT
|
||||
|
||||
def __init__(
|
||||
self, coordinator: DataUpdateCoordinator[None], device: LEDBLE, name: str
|
||||
self, coordinator: LEDBLECoordinator, device: LEDBLE, name: str
|
||||
) -> None:
|
||||
"""Initialize an ledble light."""
|
||||
super().__init__(coordinator)
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
"""The led ble integration models."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from led_ble import LEDBLE
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
type LEDBLEConfigEntry = ConfigEntry[LEDBLEData]
|
||||
|
||||
|
||||
@dataclass
|
||||
class LEDBLEData:
|
||||
"""Data for the led ble integration."""
|
||||
|
||||
title: str
|
||||
device: LEDBLE
|
||||
coordinator: DataUpdateCoordinator[None]
|
||||
Reference in New Issue
Block a user