Use runtime_data in eufylife_ble (#136705)
parent
8b738c919c
commit
cd9abacdb2
|
@ -6,17 +6,15 @@ from eufylife_ble_client import EufyLifeBLEDevice
|
|||
|
||||
from homeassistant.components import bluetooth
|
||||
from homeassistant.components.bluetooth.match import ADDRESS, BluetoothCallbackMatcher
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_MODEL, EVENT_HOMEASSISTANT_STOP, Platform
|
||||
from homeassistant.core import Event, HomeAssistant, callback
|
||||
|
||||
from .const import DOMAIN
|
||||
from .models import EufyLifeData
|
||||
from .models import EufyLifeConfigEntry, EufyLifeData
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: EufyLifeConfigEntry) -> bool:
|
||||
"""Set up EufyLife device from a config entry."""
|
||||
address = entry.unique_id
|
||||
assert address is not None
|
||||
|
@ -45,11 +43,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
)
|
||||
)
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = EufyLifeData(
|
||||
address,
|
||||
model,
|
||||
client,
|
||||
)
|
||||
entry.runtime_data = EufyLifeData(address, model, client)
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
|
@ -63,9 +57,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: EufyLifeConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
|
|
@ -6,6 +6,10 @@ from dataclasses import dataclass
|
|||
|
||||
from eufylife_ble_client import EufyLifeBLEDevice
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
|
||||
type EufyLifeConfigEntry = ConfigEntry[EufyLifeData]
|
||||
|
||||
|
||||
@dataclass
|
||||
class EufyLifeData:
|
||||
|
|
|
@ -6,7 +6,6 @@ from typing import Any
|
|||
|
||||
from eufylife_ble_client import MODEL_TO_NAME
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.bluetooth import async_address_present
|
||||
from homeassistant.components.sensor import (
|
||||
RestoreSensor,
|
||||
|
@ -20,19 +19,18 @@ from homeassistant.helpers.device_registry import DeviceInfo
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
|
||||
|
||||
from .const import DOMAIN
|
||||
from .models import EufyLifeData
|
||||
from .models import EufyLifeConfigEntry, EufyLifeData
|
||||
|
||||
IGNORED_STATES = {STATE_UNAVAILABLE, STATE_UNKNOWN}
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: config_entries.ConfigEntry,
|
||||
entry: EufyLifeConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the EufyLife sensors."""
|
||||
data: EufyLifeData = hass.data[DOMAIN][entry.entry_id]
|
||||
data = entry.runtime_data
|
||||
|
||||
entities = [
|
||||
EufyLifeWeightSensorEntity(data),
|
||||
|
|
Loading…
Reference in New Issue