Use entry.runtime_data in Fronius (#116604)

pull/116617/head
Matthias Alphart 2024-05-02 13:42:48 +02:00 committed by GitHub
parent 63a45035dd
commit 70ae082281
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 18 deletions

View File

@ -3,7 +3,6 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from collections.abc import Callable
from datetime import datetime, timedelta from datetime import datetime, timedelta
import logging import logging
from typing import Final, TypeVar from typing import Final, TypeVar
@ -42,28 +41,24 @@ PLATFORMS: Final = [Platform.SENSOR]
_FroniusCoordinatorT = TypeVar("_FroniusCoordinatorT", bound=FroniusCoordinatorBase) _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.""" """Set up fronius from a config entry."""
host = entry.data[CONF_HOST] host = entry.data[CONF_HOST]
fronius = Fronius(async_get_clientsession(hass), host) fronius = Fronius(async_get_clientsession(hass), host)
solar_net = FroniusSolarNet(hass, entry, fronius) solar_net = FroniusSolarNet(hass, entry, fronius)
await solar_net.init_devices() 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) 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: FroniusConfigEntry) -> 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:
solar_net = hass.data[DOMAIN].pop(entry.entry_id)
while solar_net.cleanup_callbacks:
solar_net.cleanup_callbacks.pop()()
return unload_ok
async def async_remove_config_entry_device( async def async_remove_config_entry_device(
@ -81,7 +76,6 @@ class FroniusSolarNet:
) -> None: ) -> None:
"""Initialize FroniusSolarNet class.""" """Initialize FroniusSolarNet class."""
self.hass = hass self.hass = hass
self.cleanup_callbacks: list[Callable[[], None]] = []
self.config_entry = entry self.config_entry = entry
self.coordinator_lock = asyncio.Lock() self.coordinator_lock = asyncio.Lock()
self.fronius = fronius self.fronius = fronius
@ -151,7 +145,7 @@ class FroniusSolarNet:
) )
# Setup periodic re-scan # Setup periodic re-scan
self.cleanup_callbacks.append( self.config_entry.async_on_unload(
async_track_time_interval( async_track_time_interval(
self.hass, self.hass,
self._init_devices_inverter, self._init_devices_inverter,

View File

@ -121,7 +121,7 @@ class FroniusCoordinatorBase(
async_add_entities(new_entities) async_add_entities(new_entities)
_add_entities_for_unregistered_descriptors() _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) self.async_add_listener(_add_entities_for_unregistered_descriptors)
) )

View File

@ -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 (
PERCENTAGE, PERCENTAGE,
POWER_VOLT_AMPERE_REACTIVE, POWER_VOLT_AMPERE_REACTIVE,
@ -44,7 +43,7 @@ from .const import (
) )
if TYPE_CHECKING: if TYPE_CHECKING:
from . import FroniusSolarNet from . import FroniusConfigEntry
from .coordinator import ( from .coordinator import (
FroniusCoordinatorBase, FroniusCoordinatorBase,
FroniusInverterUpdateCoordinator, FroniusInverterUpdateCoordinator,
@ -60,11 +59,11 @@ ENERGY_VOLT_AMPERE_REACTIVE_HOUR: Final = "varh"
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: FroniusConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up Fronius sensor entities based on a config entry.""" """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: for inverter_coordinator in solar_net.inverter_coordinators:
inverter_coordinator.add_entities_for_seen_keys( inverter_coordinator.add_entities_for_seen_keys(