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."""
|
"""The SiteSage Emonitor integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from aioemonitor import Emonitor
|
from aioemonitor import Emonitor
|
||||||
|
from aioemonitor.monitor import EmonitorStatus
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_HOST, Platform
|
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 import aiohttp_client
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||||
|
|
||||||
from .const import DOMAIN
|
type EmonitorConfigEntry = ConfigEntry[DataUpdateCoordinator[EmonitorStatus]]
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -20,13 +23,12 @@ DEFAULT_UPDATE_RATE = 60
|
|||||||
PLATFORMS = [Platform.SENSOR]
|
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."""
|
"""Set up SiteSage Emonitor from a config entry."""
|
||||||
|
|
||||||
session = aiohttp_client.async_get_clientsession(hass)
|
session = aiohttp_client.async_get_clientsession(hass)
|
||||||
emonitor = Emonitor(entry.data[CONF_HOST], session)
|
emonitor = Emonitor(entry.data[CONF_HOST], session)
|
||||||
|
|
||||||
coordinator: DataUpdateCoordinator = DataUpdateCoordinator(
|
coordinator = DataUpdateCoordinator[EmonitorStatus](
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
name=entry.title,
|
name=entry.title,
|
||||||
@ -36,20 +38,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
)
|
)
|
||||||
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
entry.runtime_data = coordinator
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = 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: EmonitorConfigEntry) -> 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
|
|
||||||
|
|
||||||
|
|
||||||
def name_short_mac(short_mac):
|
def name_short_mac(short_mac):
|
||||||
|
@ -10,7 +10,6 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import UnitOfPower
|
from homeassistant.const import UnitOfPower
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
@ -21,8 +20,7 @@ from homeassistant.helpers.update_coordinator import (
|
|||||||
DataUpdateCoordinator,
|
DataUpdateCoordinator,
|
||||||
)
|
)
|
||||||
|
|
||||||
from . import name_short_mac
|
from . import EmonitorConfigEntry, name_short_mac
|
||||||
from .const import DOMAIN
|
|
||||||
|
|
||||||
SENSORS = (
|
SENSORS = (
|
||||||
SensorEntityDescription(key="inst_power"),
|
SensorEntityDescription(key="inst_power"),
|
||||||
@ -39,11 +37,11 @@ SENSORS = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: EmonitorConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up entry."""
|
"""Set up entry."""
|
||||||
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
coordinator = config_entry.runtime_data
|
||||||
channels = coordinator.data.channels
|
channels = coordinator.data.channels
|
||||||
entities: list[EmonitorPowerSensor] = []
|
entities: list[EmonitorPowerSensor] = []
|
||||||
seen_channels = set()
|
seen_channels = set()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user