mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Store runtime data inside the config entry in Proximity (#116533)
This commit is contained in:
parent
ad61e5f237
commit
a4139f1a61
@ -38,7 +38,7 @@ from .const import (
|
||||
DOMAIN,
|
||||
UNITS,
|
||||
)
|
||||
from .coordinator import ProximityDataUpdateCoordinator
|
||||
from .coordinator import ProximityConfigEntry, ProximityDataUpdateCoordinator
|
||||
from .helpers import entity_used_in
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -65,7 +65,9 @@ CONFIG_SCHEMA = vol.Schema(
|
||||
|
||||
|
||||
async def _async_setup_legacy(
|
||||
hass: HomeAssistant, entry: ConfigEntry, coordinator: ProximityDataUpdateCoordinator
|
||||
hass: HomeAssistant,
|
||||
entry: ProximityConfigEntry,
|
||||
coordinator: ProximityDataUpdateCoordinator,
|
||||
) -> None:
|
||||
"""Legacy proximity entity handling, can be removed in 2024.8."""
|
||||
friendly_name = entry.data[CONF_NAME]
|
||||
@ -133,12 +135,10 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ProximityConfigEntry) -> bool:
|
||||
"""Set up Proximity from a config entry."""
|
||||
_LOGGER.debug("setup %s with config:%s", entry.title, entry.data)
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
|
||||
coordinator = ProximityDataUpdateCoordinator(hass, entry.title, dict(entry.data))
|
||||
|
||||
entry.async_on_unload(
|
||||
@ -158,7 +158,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
)
|
||||
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
hass.data[DOMAIN][entry.entry_id] = coordinator
|
||||
entry.runtime_data = coordinator
|
||||
|
||||
if entry.source == SOURCE_IMPORT:
|
||||
await _async_setup_legacy(hass, entry, coordinator)
|
||||
@ -170,13 +170,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(
|
||||
entry, [Platform.SENSOR]
|
||||
)
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, [Platform.SENSOR])
|
||||
|
||||
|
||||
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
|
@ -45,6 +45,8 @@ from .const import (
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ProximityConfigEntry = ConfigEntry["ProximityDataUpdateCoordinator"]
|
||||
|
||||
|
||||
@dataclass
|
||||
class StateChangedData:
|
||||
@ -73,7 +75,7 @@ DEFAULT_PROXIMITY_DATA: dict[str, str | int | None] = {
|
||||
class ProximityDataUpdateCoordinator(DataUpdateCoordinator[ProximityData]):
|
||||
"""Proximity data update coordinator."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
config_entry: ProximityConfigEntry
|
||||
|
||||
def __init__(
|
||||
self, hass: HomeAssistant, friendly_name: str, config: ConfigType
|
||||
|
@ -8,7 +8,6 @@ from homeassistant.components.device_tracker import ATTR_GPS, ATTR_IP, ATTR_MAC
|
||||
from homeassistant.components.diagnostics import REDACTED, async_redact_data
|
||||
from homeassistant.components.person import ATTR_USER_ID
|
||||
from homeassistant.components.zone import DOMAIN as ZONE_DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
ATTR_LATITUDE,
|
||||
ATTR_LONGITUDE,
|
||||
@ -19,8 +18,7 @@ from homeassistant.const import (
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import ProximityDataUpdateCoordinator
|
||||
from .coordinator import ProximityConfigEntry
|
||||
|
||||
TO_REDACT = {
|
||||
ATTR_GPS,
|
||||
@ -35,10 +33,10 @@ TO_REDACT = {
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, entry: ConfigEntry
|
||||
hass: HomeAssistant, entry: ProximityConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
coordinator: ProximityDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
diag_data = {
|
||||
"entry": entry.as_dict(),
|
||||
|
@ -9,7 +9,6 @@ from homeassistant.components.sensor import (
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import UnitOfLength
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
@ -25,7 +24,7 @@ from .const import (
|
||||
ATTR_NEAREST_DIST_TO,
|
||||
DOMAIN,
|
||||
)
|
||||
from .coordinator import ProximityDataUpdateCoordinator
|
||||
from .coordinator import ProximityConfigEntry, ProximityDataUpdateCoordinator
|
||||
|
||||
DIRECTIONS = ["arrived", "away_from", "stationary", "towards"]
|
||||
|
||||
@ -81,11 +80,13 @@ def _device_info(coordinator: ProximityDataUpdateCoordinator) -> DeviceInfo:
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
hass: HomeAssistant,
|
||||
entry: ProximityConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the proximity sensors."""
|
||||
|
||||
coordinator: ProximityDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
entities: list[ProximitySensor | ProximityTrackedEntitySensor] = [
|
||||
ProximitySensor(description, coordinator)
|
||||
|
Loading…
x
Reference in New Issue
Block a user