Use runtime_data in eufylife_ble (#136705)

pull/131848/head
epenet 2025-01-28 11:52:10 +01:00 committed by GitHub
parent 8b738c919c
commit cd9abacdb2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 19 deletions

View File

@ -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)

View File

@ -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:

View File

@ -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),