mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +00:00
Use runtime_data in epion (#136708)
This commit is contained in:
parent
933aec1027
commit
91ff31a3be
@ -4,30 +4,25 @@ from __future__ import annotations
|
||||
|
||||
from epion import Epion
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_API_KEY, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import EpionCoordinator
|
||||
from .coordinator import EpionConfigEntry, EpionCoordinator
|
||||
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: EpionConfigEntry) -> bool:
|
||||
"""Set up the Epion coordinator from a config entry."""
|
||||
api = Epion(entry.data[CONF_API_KEY])
|
||||
coordinator = EpionCoordinator(hass, api)
|
||||
coordinator = EpionCoordinator(hass, entry, api)
|
||||
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: EpionConfigEntry) -> bool:
|
||||
"""Unload Epion config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if unload_ok:
|
||||
del hass.data[DOMAIN][entry.entry_id]
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
@ -5,6 +5,7 @@ from typing import Any
|
||||
|
||||
from epion import Epion, EpionAuthenticationError, EpionConnectionError
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
@ -13,15 +14,20 @@ from .const import REFRESH_INTERVAL
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
type EpionConfigEntry = ConfigEntry[EpionCoordinator]
|
||||
|
||||
|
||||
class EpionCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||
"""Epion data update coordinator."""
|
||||
|
||||
def __init__(self, hass: HomeAssistant, epion_api: Epion) -> None:
|
||||
def __init__(
|
||||
self, hass: HomeAssistant, entry: EpionConfigEntry, epion_api: Epion
|
||||
) -> None:
|
||||
"""Initialize the Epion coordinator."""
|
||||
super().__init__(
|
||||
hass,
|
||||
_LOGGER,
|
||||
config_entry=entry,
|
||||
name="Epion",
|
||||
update_interval=REFRESH_INTERVAL,
|
||||
)
|
||||
|
@ -10,7 +10,6 @@ from homeassistant.components.sensor import (
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONCENTRATION_PARTS_PER_MILLION,
|
||||
PERCENTAGE,
|
||||
@ -23,7 +22,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import EpionCoordinator
|
||||
from .coordinator import EpionConfigEntry, EpionCoordinator
|
||||
|
||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
SensorEntityDescription(
|
||||
@ -59,11 +58,11 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: EpionConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Add an Epion entry."""
|
||||
coordinator: EpionCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
entities = [
|
||||
EpionSensor(coordinator, epion_device_id, description)
|
||||
|
Loading…
x
Reference in New Issue
Block a user