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 epion import Epion
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_API_KEY, Platform
|
from homeassistant.const import CONF_API_KEY, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import EpionConfigEntry, EpionCoordinator
|
||||||
from .coordinator import EpionCoordinator
|
|
||||||
|
|
||||||
PLATFORMS = [Platform.SENSOR]
|
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."""
|
"""Set up the Epion coordinator from a config entry."""
|
||||||
api = Epion(entry.data[CONF_API_KEY])
|
api = Epion(entry.data[CONF_API_KEY])
|
||||||
coordinator = EpionCoordinator(hass, api)
|
coordinator = EpionCoordinator(hass, entry, api)
|
||||||
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: EpionConfigEntry) -> bool:
|
||||||
"""Unload Epion config entry."""
|
"""Unload Epion 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:
|
|
||||||
del hass.data[DOMAIN][entry.entry_id]
|
|
||||||
return unload_ok
|
|
||||||
|
@ -5,6 +5,7 @@ from typing import Any
|
|||||||
|
|
||||||
from epion import Epion, EpionAuthenticationError, EpionConnectionError
|
from epion import Epion, EpionAuthenticationError, EpionConnectionError
|
||||||
|
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
@ -13,15 +14,20 @@ from .const import REFRESH_INTERVAL
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
type EpionConfigEntry = ConfigEntry[EpionCoordinator]
|
||||||
|
|
||||||
|
|
||||||
class EpionCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
class EpionCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||||
"""Epion data update coordinator."""
|
"""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."""
|
"""Initialize the Epion coordinator."""
|
||||||
super().__init__(
|
super().__init__(
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
|
config_entry=entry,
|
||||||
name="Epion",
|
name="Epion",
|
||||||
update_interval=REFRESH_INTERVAL,
|
update_interval=REFRESH_INTERVAL,
|
||||||
)
|
)
|
||||||
|
@ -10,7 +10,6 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONCENTRATION_PARTS_PER_MILLION,
|
CONCENTRATION_PARTS_PER_MILLION,
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
@ -23,7 +22,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import EpionCoordinator
|
from .coordinator import EpionConfigEntry, EpionCoordinator
|
||||||
|
|
||||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
@ -59,11 +58,11 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: EpionConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Add an Epion entry."""
|
"""Add an Epion entry."""
|
||||||
coordinator: EpionCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator = entry.runtime_data
|
||||||
|
|
||||||
entities = [
|
entities = [
|
||||||
EpionSensor(coordinator, epion_device_id, description)
|
EpionSensor(coordinator, epion_device_id, description)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user