Use entry.runtime_data in Fronius (#116604)
parent
63a45035dd
commit
70ae082281
|
@ -3,7 +3,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Callable
|
||||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
from typing import Final, TypeVar
|
||||
|
@ -42,28 +41,24 @@ PLATFORMS: Final = [Platform.SENSOR]
|
|||
|
||||
_FroniusCoordinatorT = TypeVar("_FroniusCoordinatorT", bound=FroniusCoordinatorBase)
|
||||
|
||||
FroniusConfigEntry = ConfigEntry["FroniusSolarNet"]
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: FroniusConfigEntry) -> bool:
|
||||
"""Set up fronius from a config entry."""
|
||||
host = entry.data[CONF_HOST]
|
||||
fronius = Fronius(async_get_clientsession(hass), host)
|
||||
solar_net = FroniusSolarNet(hass, entry, fronius)
|
||||
await solar_net.init_devices()
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = solar_net
|
||||
entry.runtime_data = solar_net
|
||||
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: FroniusConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if unload_ok:
|
||||
solar_net = hass.data[DOMAIN].pop(entry.entry_id)
|
||||
while solar_net.cleanup_callbacks:
|
||||
solar_net.cleanup_callbacks.pop()()
|
||||
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
|
||||
async def async_remove_config_entry_device(
|
||||
|
@ -81,7 +76,6 @@ class FroniusSolarNet:
|
|||
) -> None:
|
||||
"""Initialize FroniusSolarNet class."""
|
||||
self.hass = hass
|
||||
self.cleanup_callbacks: list[Callable[[], None]] = []
|
||||
self.config_entry = entry
|
||||
self.coordinator_lock = asyncio.Lock()
|
||||
self.fronius = fronius
|
||||
|
@ -151,7 +145,7 @@ class FroniusSolarNet:
|
|||
)
|
||||
|
||||
# Setup periodic re-scan
|
||||
self.cleanup_callbacks.append(
|
||||
self.config_entry.async_on_unload(
|
||||
async_track_time_interval(
|
||||
self.hass,
|
||||
self._init_devices_inverter,
|
||||
|
|
|
@ -121,7 +121,7 @@ class FroniusCoordinatorBase(
|
|||
async_add_entities(new_entities)
|
||||
|
||||
_add_entities_for_unregistered_descriptors()
|
||||
self.solar_net.cleanup_callbacks.append(
|
||||
self.solar_net.config_entry.async_on_unload(
|
||||
self.async_add_listener(_add_entities_for_unregistered_descriptors)
|
||||
)
|
||||
|
||||
|
|
|
@ -12,7 +12,6 @@ from homeassistant.components.sensor import (
|
|||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
PERCENTAGE,
|
||||
POWER_VOLT_AMPERE_REACTIVE,
|
||||
|
@ -44,7 +43,7 @@ from .const import (
|
|||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from . import FroniusSolarNet
|
||||
from . import FroniusConfigEntry
|
||||
from .coordinator import (
|
||||
FroniusCoordinatorBase,
|
||||
FroniusInverterUpdateCoordinator,
|
||||
|
@ -60,11 +59,11 @@ ENERGY_VOLT_AMPERE_REACTIVE_HOUR: Final = "varh"
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: FroniusConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Fronius sensor entities based on a config entry."""
|
||||
solar_net: FroniusSolarNet = hass.data[DOMAIN][config_entry.entry_id]
|
||||
solar_net = config_entry.runtime_data
|
||||
|
||||
for inverter_coordinator in solar_net.inverter_coordinators:
|
||||
inverter_coordinator.add_entities_for_seen_keys(
|
||||
|
|
Loading…
Reference in New Issue