mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 14:27:07 +00:00
Migrate led_ble to use runtime_data (#147337)
This commit is contained in:
parent
b13dd4e6ca
commit
f64533e9e0
@ -10,21 +10,20 @@ from led_ble import BLEAK_EXCEPTIONS, LEDBLE
|
||||
|
||||
from homeassistant.components import bluetooth
|
||||
from homeassistant.components.bluetooth.match import ADDRESS, BluetoothCallbackMatcher
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
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, DOMAIN, UPDATE_SECONDS
|
||||
from .models import LEDBLEData
|
||||
from .const import DEVICE_TIMEOUT, UPDATE_SECONDS
|
||||
from .models import LEDBLEConfigEntry, LEDBLEData
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.LIGHT]
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: LEDBLEConfigEntry) -> bool:
|
||||
"""Set up LED BLE from a config entry."""
|
||||
address: str = entry.data[CONF_ADDRESS]
|
||||
ble_device = bluetooth.async_ble_device_from_address(hass, address.upper(), True)
|
||||
@ -89,9 +88,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
finally:
|
||||
cancel_first_update()
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = LEDBLEData(
|
||||
entry.title, led_ble, coordinator
|
||||
)
|
||||
entry.runtime_data = LEDBLEData(entry.title, led_ble, coordinator)
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
entry.async_on_unload(entry.add_update_listener(_async_update_listener))
|
||||
@ -106,17 +103,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
async def _async_update_listener(hass: HomeAssistant, entry: LEDBLEConfigEntry) -> None:
|
||||
"""Handle options update."""
|
||||
data: LEDBLEData = hass.data[DOMAIN][entry.entry_id]
|
||||
if entry.title != data.title:
|
||||
if entry.title != entry.runtime_data.title:
|
||||
await hass.config_entries.async_reload(entry.entry_id)
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: LEDBLEConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
data: LEDBLEData = hass.data[DOMAIN].pop(entry.entry_id)
|
||||
await data.device.stop()
|
||||
await entry.runtime_data.device.stop()
|
||||
|
||||
return unload_ok
|
||||
|
@ -15,7 +15,6 @@ from homeassistant.components.light import (
|
||||
LightEntity,
|
||||
LightEntityFeature,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
@ -25,17 +24,17 @@ from homeassistant.helpers.update_coordinator import (
|
||||
DataUpdateCoordinator,
|
||||
)
|
||||
|
||||
from .const import DEFAULT_EFFECT_SPEED, DOMAIN
|
||||
from .models import LEDBLEData
|
||||
from .const import DEFAULT_EFFECT_SPEED
|
||||
from .models import LEDBLEConfigEntry
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: LEDBLEConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the light platform for LEDBLE."""
|
||||
data: LEDBLEData = hass.data[DOMAIN][entry.entry_id]
|
||||
data = entry.runtime_data
|
||||
async_add_entities([LEDBLEEntity(data.coordinator, data.device, entry.title)])
|
||||
|
||||
|
||||
|
@ -6,8 +6,11 @@ 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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user