Use dispatcher helper to add new Fronius inverter entities (#96782)

Using dispatcher to add new entities for inverter
pull/88048/head^2
Björn Weitzel 2023-07-18 23:18:02 +02:00 committed by GitHub
parent 4fefbf0408
commit 4b2cbbe8c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 9 deletions

View File

@ -15,12 +15,13 @@ from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.dispatcher import dispatcher_send
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_time_interval from homeassistant.helpers.event import async_track_time_interval
from .const import ( from .const import (
DOMAIN, DOMAIN,
SOLAR_NET_DISCOVERY_NEW,
SOLAR_NET_ID_SYSTEM, SOLAR_NET_ID_SYSTEM,
SOLAR_NET_RESCAN_TIMER, SOLAR_NET_RESCAN_TIMER,
FroniusDeviceInfo, FroniusDeviceInfo,
@ -34,7 +35,6 @@ from .coordinator import (
FroniusPowerFlowUpdateCoordinator, FroniusPowerFlowUpdateCoordinator,
FroniusStorageUpdateCoordinator, FroniusStorageUpdateCoordinator,
) )
from .sensor import InverterSensor
_LOGGER: Final = logging.getLogger(__name__) _LOGGER: Final = logging.getLogger(__name__)
PLATFORMS: Final = [Platform.SENSOR] PLATFORMS: Final = [Platform.SENSOR]
@ -76,7 +76,6 @@ class FroniusSolarNet:
self.cleanup_callbacks: list[Callable[[], None]] = [] 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.sensor_async_add_entities: AddEntitiesCallback | None = None
self.fronius = fronius self.fronius = fronius
self.host: str = entry.data[CONF_HOST] self.host: str = entry.data[CONF_HOST]
# entry.unique_id is either logger uid or first inverter uid if no logger available # entry.unique_id is either logger uid or first inverter uid if no logger available
@ -204,10 +203,8 @@ class FroniusSolarNet:
self.inverter_coordinators.append(_coordinator) self.inverter_coordinators.append(_coordinator)
# Only for re-scans. Initial setup adds entities through sensor.async_setup_entry # Only for re-scans. Initial setup adds entities through sensor.async_setup_entry
if self.sensor_async_add_entities is not None: if self.config_entry.state == ConfigEntryState.LOADED:
_coordinator.add_entities_for_seen_keys( dispatcher_send(self.hass, SOLAR_NET_DISCOVERY_NEW, _coordinator)
self.sensor_async_add_entities, InverterSensor
)
_LOGGER.debug( _LOGGER.debug(
"New inverter added (UID: %s)", "New inverter added (UID: %s)",

View File

@ -6,6 +6,7 @@ from homeassistant.helpers.entity import DeviceInfo
DOMAIN: Final = "fronius" DOMAIN: Final = "fronius"
SolarNetId = str SolarNetId = str
SOLAR_NET_DISCOVERY_NEW: Final = "fronius_discovery_new"
SOLAR_NET_ID_POWER_FLOW: SolarNetId = "power_flow" SOLAR_NET_ID_POWER_FLOW: SolarNetId = "power_flow"
SOLAR_NET_ID_SYSTEM: SolarNetId = "system" SOLAR_NET_ID_SYSTEM: SolarNetId = "system"
SOLAR_NET_RESCAN_TIMER: Final = 60 SOLAR_NET_RESCAN_TIMER: Final = 60

View File

@ -24,12 +24,13 @@ from homeassistant.const import (
UnitOfTemperature, UnitOfTemperature,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN from .const import DOMAIN, SOLAR_NET_DISCOVERY_NEW
if TYPE_CHECKING: if TYPE_CHECKING:
from . import FroniusSolarNet from . import FroniusSolarNet
@ -53,7 +54,6 @@ async def async_setup_entry(
) -> 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: FroniusSolarNet = hass.data[DOMAIN][config_entry.entry_id]
solar_net.sensor_async_add_entities = async_add_entities
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(
@ -80,6 +80,19 @@ async def async_setup_entry(
async_add_entities, StorageSensor async_add_entities, StorageSensor
) )
@callback
def async_add_new_entities(coordinator: FroniusInverterUpdateCoordinator) -> None:
"""Add newly found inverter entities."""
coordinator.add_entities_for_seen_keys(async_add_entities, InverterSensor)
config_entry.async_on_unload(
async_dispatcher_connect(
hass,
SOLAR_NET_DISCOVERY_NEW,
async_add_new_entities,
)
)
@dataclass @dataclass
class FroniusSensorEntityDescription(SensorEntityDescription): class FroniusSensorEntityDescription(SensorEntityDescription):