Use ConfigEntry runtime_data in Discovergy (#116671)
This commit is contained in:
parent
9b4099950c
commit
ce8cea86a2
4 changed files with 14 additions and 22 deletions
|
@ -12,16 +12,15 @@ from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||||
from homeassistant.helpers.httpx_client import get_async_client
|
from homeassistant.helpers.httpx_client import get_async_client
|
||||||
|
|
||||||
from .const import DOMAIN
|
|
||||||
from .coordinator import DiscovergyUpdateCoordinator
|
from .coordinator import DiscovergyUpdateCoordinator
|
||||||
|
|
||||||
PLATFORMS = [Platform.SENSOR]
|
PLATFORMS = [Platform.SENSOR]
|
||||||
|
|
||||||
|
DiscovergyConfigEntry = ConfigEntry[list[DiscovergyUpdateCoordinator]]
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
||||||
|
async def async_setup_entry(hass: HomeAssistant, entry: DiscovergyConfigEntry) -> bool:
|
||||||
"""Set up Discovergy from a config entry."""
|
"""Set up Discovergy from a config entry."""
|
||||||
hass.data.setdefault(DOMAIN, {})
|
|
||||||
|
|
||||||
client = Discovergy(
|
client = Discovergy(
|
||||||
email=entry.data[CONF_EMAIL],
|
email=entry.data[CONF_EMAIL],
|
||||||
password=entry.data[CONF_PASSWORD],
|
password=entry.data[CONF_PASSWORD],
|
||||||
|
@ -53,7 +52,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
coordinators.append(coordinator)
|
coordinators.append(coordinator)
|
||||||
|
|
||||||
hass.data[DOMAIN][entry.entry_id] = coordinators
|
entry.runtime_data = coordinators
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
|
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
|
||||||
|
@ -63,11 +62,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> 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
|
|
||||||
|
|
||||||
|
|
||||||
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||||
|
|
|
@ -6,11 +6,9 @@ from dataclasses import asdict
|
||||||
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.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import DiscovergyConfigEntry
|
||||||
from .coordinator import DiscovergyUpdateCoordinator
|
|
||||||
|
|
||||||
TO_REDACT_METER = {
|
TO_REDACT_METER = {
|
||||||
"serial_number",
|
"serial_number",
|
||||||
|
@ -22,14 +20,13 @@ TO_REDACT_METER = {
|
||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant, entry: ConfigEntry
|
hass: HomeAssistant, entry: DiscovergyConfigEntry
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
flattened_meter: list[dict] = []
|
flattened_meter: list[dict] = []
|
||||||
last_readings: dict[str, dict] = {}
|
last_readings: dict[str, dict] = {}
|
||||||
coordinators: list[DiscovergyUpdateCoordinator] = hass.data[DOMAIN][entry.entry_id]
|
|
||||||
|
|
||||||
for coordinator in coordinators:
|
for coordinator in entry.runtime_data:
|
||||||
# make a dict of meter data and redact some data
|
# make a dict of meter data and redact some data
|
||||||
flattened_meter.append(
|
flattened_meter.append(
|
||||||
async_redact_data(asdict(coordinator.meter), TO_REDACT_METER)
|
async_redact_data(asdict(coordinator.meter), TO_REDACT_METER)
|
||||||
|
|
|
@ -12,7 +12,6 @@ from homeassistant.components.sensor import (
|
||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
EntityCategory,
|
EntityCategory,
|
||||||
UnitOfElectricPotential,
|
UnitOfElectricPotential,
|
||||||
|
@ -25,6 +24,7 @@ from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
|
from . import DiscovergyConfigEntry
|
||||||
from .const import DOMAIN, MANUFACTURER
|
from .const import DOMAIN, MANUFACTURER
|
||||||
from .coordinator import DiscovergyUpdateCoordinator
|
from .coordinator import DiscovergyUpdateCoordinator
|
||||||
|
|
||||||
|
@ -163,13 +163,13 @@ ADDITIONAL_SENSORS: tuple[DiscovergySensorEntityDescription, ...] = (
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: DiscovergyConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Discovergy sensors."""
|
"""Set up the Discovergy sensors."""
|
||||||
coordinators: list[DiscovergyUpdateCoordinator] = hass.data[DOMAIN][entry.entry_id]
|
|
||||||
|
|
||||||
entities: list[DiscovergySensor] = []
|
entities: list[DiscovergySensor] = []
|
||||||
for coordinator in coordinators:
|
for coordinator in entry.runtime_data:
|
||||||
sensors: tuple[DiscovergySensorEntityDescription, ...] = ()
|
sensors: tuple[DiscovergySensorEntityDescription, ...] = ()
|
||||||
|
|
||||||
# select sensor descriptions based on meter type and combine with additional sensors
|
# select sensor descriptions based on meter type and combine with additional sensors
|
||||||
|
|
|
@ -6,7 +6,7 @@ from unittest.mock import AsyncMock, patch
|
||||||
from pydiscovergy.models import Reading
|
from pydiscovergy.models import Reading
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.discovergy import DOMAIN
|
from homeassistant.components.discovergy.const import DOMAIN
|
||||||
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
Loading…
Add table
Reference in a new issue