mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Use runtime_data in rainforest_raven (#128517)
This commit is contained in:
parent
f9509d2b38
commit
66395d5fe5
@ -2,29 +2,23 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import RAVEnConfigEntry, RAVEnDataCoordinator
|
||||||
from .coordinator import RAVEnDataCoordinator
|
|
||||||
|
|
||||||
PLATFORMS = (Platform.SENSOR,)
|
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."""
|
"""Set up Rainforest RAVEn device from a config entry."""
|
||||||
coordinator = RAVEnDataCoordinator(hass, entry)
|
coordinator = RAVEnDataCoordinator(hass, entry)
|
||||||
await coordinator.async_config_entry_first_refresh()
|
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)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
return True
|
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 a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
if unload_ok:
|
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
@ -20,6 +20,8 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
|
|||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
type RAVEnConfigEntry = ConfigEntry[RAVEnDataCoordinator]
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -67,11 +69,10 @@ class RAVEnDataCoordinator(DataUpdateCoordinator):
|
|||||||
|
|
||||||
_raven_device: RAVEnSerialDevice | None = None
|
_raven_device: RAVEnSerialDevice | None = None
|
||||||
_device_info: RAVEnDeviceInfo | 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."""
|
"""Initialize the data object."""
|
||||||
self.entry = entry
|
|
||||||
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
@ -143,7 +144,7 @@ class RAVEnDataCoordinator(DataUpdateCoordinator):
|
|||||||
try:
|
try:
|
||||||
device = await self._get_device()
|
device = await self._get_device()
|
||||||
async with asyncio.timeout(5):
|
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:
|
except RAVEnConnectionError as err:
|
||||||
await self._cleanup_device()
|
await self._cleanup_device()
|
||||||
raise UpdateFailed(f"RAVEnConnectionError: {err}") from err
|
raise UpdateFailed(f"RAVEnConnectionError: {err}") from err
|
||||||
@ -160,7 +161,7 @@ class RAVEnDataCoordinator(DataUpdateCoordinator):
|
|||||||
if self._raven_device is not None:
|
if self._raven_device is not None:
|
||||||
return self._raven_device
|
return self._raven_device
|
||||||
|
|
||||||
device = RAVEnSerialDevice(self.entry.data[CONF_DEVICE])
|
device = RAVEnSerialDevice(self.config_entry.data[CONF_DEVICE])
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with asyncio.timeout(5):
|
async with asyncio.timeout(5):
|
||||||
|
@ -6,12 +6,10 @@ from collections.abc import Mapping
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.diagnostics import async_redact_data
|
from homeassistant.components.diagnostics import async_redact_data
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_MAC
|
from homeassistant.const import CONF_MAC
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import RAVEnConfigEntry
|
||||||
from .coordinator import RAVEnDataCoordinator
|
|
||||||
|
|
||||||
TO_REDACT_CONFIG = {CONF_MAC}
|
TO_REDACT_CONFIG = {CONF_MAC}
|
||||||
TO_REDACT_DATA = {"device_mac_id", "meter_mac_id"}
|
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(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant, config_entry: ConfigEntry
|
hass: HomeAssistant, config_entry: RAVEnConfigEntry
|
||||||
) -> Mapping[str, Any]:
|
) -> Mapping[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
coordinator: RAVEnDataCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"config_entry": async_redact_data(config_entry.as_dict(), TO_REDACT_CONFIG),
|
"config_entry": async_redact_data(config_entry.as_dict(), TO_REDACT_CONFIG),
|
||||||
"data": async_redact_meter_macs(
|
"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,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_MAC,
|
CONF_MAC,
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
@ -24,8 +23,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import RAVEnConfigEntry, RAVEnDataCoordinator
|
||||||
from .coordinator import RAVEnDataCoordinator
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
@ -80,10 +78,12 @@ DIAGNOSTICS = (
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: RAVEnConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up a config entry."""
|
"""Set up a config entry."""
|
||||||
coordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator = entry.runtime_data
|
||||||
entities: list[RAVEnSensor] = [
|
entities: list[RAVEnSensor] = [
|
||||||
RAVEnSensor(coordinator, description) for description in DIAGNOSTICS
|
RAVEnSensor(coordinator, description) for description in DIAGNOSTICS
|
||||||
]
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user