Use runtime_data in rainforest_raven (#128517)
This commit is contained in:
parent
f9509d2b38
commit
66395d5fe5
4 changed files with 19 additions and 27 deletions
|
@ -2,29 +2,23 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import RAVEnDataCoordinator
|
||||
from .coordinator import RAVEnConfigEntry, RAVEnDataCoordinator
|
||||
|
||||
PLATFORMS = (Platform.SENSOR,)
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: RAVEnConfigEntry) -> bool:
|
||||
"""Set up Rainforest RAVEn device from a config entry."""
|
||||
coordinator = RAVEnDataCoordinator(hass, entry)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
||||
entry.runtime_data = coordinator
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: RAVEnConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
|
|
@ -20,6 +20,8 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
|
|||
|
||||
from .const import DOMAIN
|
||||
|
||||
type RAVEnConfigEntry = ConfigEntry[RAVEnDataCoordinator]
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
@ -67,11 +69,10 @@ class RAVEnDataCoordinator(DataUpdateCoordinator):
|
|||
|
||||
_raven_device: RAVEnSerialDevice | None = None
|
||||
_device_info: RAVEnDeviceInfo | None = None
|
||||
config_entry: RAVEnConfigEntry
|
||||
|
||||
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
def __init__(self, hass: HomeAssistant, entry: RAVEnConfigEntry) -> None:
|
||||
"""Initialize the data object."""
|
||||
self.entry = entry
|
||||
|
||||
super().__init__(
|
||||
hass,
|
||||
_LOGGER,
|
||||
|
@ -143,7 +144,7 @@ class RAVEnDataCoordinator(DataUpdateCoordinator):
|
|||
try:
|
||||
device = await self._get_device()
|
||||
async with asyncio.timeout(5):
|
||||
return await _get_all_data(device, self.entry.data[CONF_MAC])
|
||||
return await _get_all_data(device, self.config_entry.data[CONF_MAC])
|
||||
except RAVEnConnectionError as err:
|
||||
await self._cleanup_device()
|
||||
raise UpdateFailed(f"RAVEnConnectionError: {err}") from err
|
||||
|
@ -160,7 +161,7 @@ class RAVEnDataCoordinator(DataUpdateCoordinator):
|
|||
if self._raven_device is not None:
|
||||
return self._raven_device
|
||||
|
||||
device = RAVEnSerialDevice(self.entry.data[CONF_DEVICE])
|
||||
device = RAVEnSerialDevice(self.config_entry.data[CONF_DEVICE])
|
||||
|
||||
try:
|
||||
async with asyncio.timeout(5):
|
||||
|
|
|
@ -6,12 +6,10 @@ from collections.abc import Mapping
|
|||
from typing import Any
|
||||
|
||||
from homeassistant.components.diagnostics import async_redact_data
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_MAC
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import RAVEnDataCoordinator
|
||||
from .coordinator import RAVEnConfigEntry
|
||||
|
||||
TO_REDACT_CONFIG = {CONF_MAC}
|
||||
TO_REDACT_DATA = {"device_mac_id", "meter_mac_id"}
|
||||
|
@ -31,14 +29,13 @@ def async_redact_meter_macs(data: dict) -> dict:
|
|||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry
|
||||
hass: HomeAssistant, config_entry: RAVEnConfigEntry
|
||||
) -> Mapping[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
coordinator: RAVEnDataCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||
|
||||
return {
|
||||
"config_entry": async_redact_data(config_entry.as_dict(), TO_REDACT_CONFIG),
|
||||
"data": async_redact_meter_macs(
|
||||
async_redact_data(coordinator.data, TO_REDACT_DATA)
|
||||
async_redact_data(config_entry.runtime_data.data, TO_REDACT_DATA)
|
||||
),
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ from homeassistant.components.sensor import (
|
|||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONF_MAC,
|
||||
PERCENTAGE,
|
||||
|
@ -24,8 +23,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||
from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import RAVEnDataCoordinator
|
||||
from .coordinator import RAVEnConfigEntry, RAVEnDataCoordinator
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
|
@ -80,10 +78,12 @@ DIAGNOSTICS = (
|
|||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
hass: HomeAssistant,
|
||||
entry: RAVEnConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up a config entry."""
|
||||
coordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator = entry.runtime_data
|
||||
entities: list[RAVEnSensor] = [
|
||||
RAVEnSensor(coordinator, description) for description in DIAGNOSTICS
|
||||
]
|
||||
|
|
Loading…
Add table
Reference in a new issue