mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Migrate emonitor to entry.runtime_data (#121645)
This commit is contained in:
parent
0aa6a17da8
commit
bf09cee66f
@ -1,9 +1,12 @@
|
||||
"""The SiteSage Emonitor integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from aioemonitor import Emonitor
|
||||
from aioemonitor.monitor import EmonitorStatus
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_HOST, Platform
|
||||
@ -11,7 +14,7 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import aiohttp_client
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from .const import DOMAIN
|
||||
type EmonitorConfigEntry = ConfigEntry[DataUpdateCoordinator[EmonitorStatus]]
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -20,13 +23,12 @@ DEFAULT_UPDATE_RATE = 60
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: EmonitorConfigEntry) -> bool:
|
||||
"""Set up SiteSage Emonitor from a config entry."""
|
||||
|
||||
session = aiohttp_client.async_get_clientsession(hass)
|
||||
emonitor = Emonitor(entry.data[CONF_HOST], session)
|
||||
|
||||
coordinator: DataUpdateCoordinator = DataUpdateCoordinator(
|
||||
coordinator = DataUpdateCoordinator[EmonitorStatus](
|
||||
hass,
|
||||
_LOGGER,
|
||||
name=entry.title,
|
||||
@ -36,20 +38,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
)
|
||||
|
||||
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: EmonitorConfigEntry) -> 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)
|
||||
|
||||
|
||||
def name_short_mac(short_mac):
|
||||
|
@ -10,7 +10,6 @@ from homeassistant.components.sensor import (
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import UnitOfPower
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
@ -21,8 +20,7 @@ from homeassistant.helpers.update_coordinator import (
|
||||
DataUpdateCoordinator,
|
||||
)
|
||||
|
||||
from . import name_short_mac
|
||||
from .const import DOMAIN
|
||||
from . import EmonitorConfigEntry, name_short_mac
|
||||
|
||||
SENSORS = (
|
||||
SensorEntityDescription(key="inst_power"),
|
||||
@ -39,11 +37,11 @@ SENSORS = (
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: EmonitorConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up entry."""
|
||||
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||
coordinator = config_entry.runtime_data
|
||||
channels = coordinator.data.channels
|
||||
entities: list[EmonitorPowerSensor] = []
|
||||
seen_channels = set()
|
||||
|
Loading…
x
Reference in New Issue
Block a user