mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 06:47:09 +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 import bluetooth
|
||||||
from homeassistant.components.bluetooth.match import ADDRESS, BluetoothCallbackMatcher
|
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.const import CONF_ADDRESS, EVENT_HOMEASSISTANT_STOP, Platform
|
||||||
from homeassistant.core import Event, HomeAssistant, callback
|
from homeassistant.core import Event, HomeAssistant, callback
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
|
||||||
from .const import DEVICE_TIMEOUT, DOMAIN, UPDATE_SECONDS
|
from .const import DEVICE_TIMEOUT, UPDATE_SECONDS
|
||||||
from .models import LEDBLEData
|
from .models import LEDBLEConfigEntry, LEDBLEData
|
||||||
|
|
||||||
PLATFORMS: list[Platform] = [Platform.LIGHT]
|
PLATFORMS: list[Platform] = [Platform.LIGHT]
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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."""
|
"""Set up LED BLE from a config entry."""
|
||||||
address: str = entry.data[CONF_ADDRESS]
|
address: str = entry.data[CONF_ADDRESS]
|
||||||
ble_device = bluetooth.async_ble_device_from_address(hass, address.upper(), True)
|
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:
|
finally:
|
||||||
cancel_first_update()
|
cancel_first_update()
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = LEDBLEData(
|
entry.runtime_data = LEDBLEData(entry.title, led_ble, coordinator)
|
||||||
entry.title, led_ble, coordinator
|
|
||||||
)
|
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
entry.async_on_unload(entry.add_update_listener(_async_update_listener))
|
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
|
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."""
|
"""Handle options update."""
|
||||||
data: LEDBLEData = hass.data[DOMAIN][entry.entry_id]
|
if entry.title != entry.runtime_data.title:
|
||||||
if entry.title != data.title:
|
|
||||||
await hass.config_entries.async_reload(entry.entry_id)
|
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."""
|
"""Unload a config entry."""
|
||||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||||
data: LEDBLEData = hass.data[DOMAIN].pop(entry.entry_id)
|
await entry.runtime_data.device.stop()
|
||||||
await data.device.stop()
|
|
||||||
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
@ -15,7 +15,6 @@ from homeassistant.components.light import (
|
|||||||
LightEntity,
|
LightEntity,
|
||||||
LightEntityFeature,
|
LightEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
@ -25,17 +24,17 @@ from homeassistant.helpers.update_coordinator import (
|
|||||||
DataUpdateCoordinator,
|
DataUpdateCoordinator,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .const import DEFAULT_EFFECT_SPEED, DOMAIN
|
from .const import DEFAULT_EFFECT_SPEED
|
||||||
from .models import LEDBLEData
|
from .models import LEDBLEConfigEntry
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: LEDBLEConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the light platform for LEDBLE."""
|
"""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)])
|
async_add_entities([LEDBLEEntity(data.coordinator, data.device, entry.title)])
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,8 +6,11 @@ from dataclasses import dataclass
|
|||||||
|
|
||||||
from led_ble import LEDBLE
|
from led_ble import LEDBLE
|
||||||
|
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||||
|
|
||||||
|
type LEDBLEConfigEntry = ConfigEntry[LEDBLEData]
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class LEDBLEData:
|
class LEDBLEData:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user