diff --git a/homeassistant/components/fronius/__init__.py b/homeassistant/components/fronius/__init__.py index f8dcb4f4a9c..6202b945d97 100644 --- a/homeassistant/components/fronius/__init__.py +++ b/homeassistant/components/fronius/__init__.py @@ -15,12 +15,13 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import device_registry as dr 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_platform import AddEntitiesCallback from homeassistant.helpers.event import async_track_time_interval from .const import ( DOMAIN, + SOLAR_NET_DISCOVERY_NEW, SOLAR_NET_ID_SYSTEM, SOLAR_NET_RESCAN_TIMER, FroniusDeviceInfo, @@ -34,7 +35,6 @@ from .coordinator import ( FroniusPowerFlowUpdateCoordinator, FroniusStorageUpdateCoordinator, ) -from .sensor import InverterSensor _LOGGER: Final = logging.getLogger(__name__) PLATFORMS: Final = [Platform.SENSOR] @@ -76,7 +76,6 @@ class FroniusSolarNet: self.cleanup_callbacks: list[Callable[[], None]] = [] self.config_entry = entry self.coordinator_lock = asyncio.Lock() - self.sensor_async_add_entities: AddEntitiesCallback | None = None self.fronius = fronius self.host: str = entry.data[CONF_HOST] # 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) # Only for re-scans. Initial setup adds entities through sensor.async_setup_entry - if self.sensor_async_add_entities is not None: - _coordinator.add_entities_for_seen_keys( - self.sensor_async_add_entities, InverterSensor - ) + if self.config_entry.state == ConfigEntryState.LOADED: + dispatcher_send(self.hass, SOLAR_NET_DISCOVERY_NEW, _coordinator) _LOGGER.debug( "New inverter added (UID: %s)", diff --git a/homeassistant/components/fronius/const.py b/homeassistant/components/fronius/const.py index 042773472c5..b65864ee089 100644 --- a/homeassistant/components/fronius/const.py +++ b/homeassistant/components/fronius/const.py @@ -6,6 +6,7 @@ from homeassistant.helpers.entity import DeviceInfo DOMAIN: Final = "fronius" SolarNetId = str +SOLAR_NET_DISCOVERY_NEW: Final = "fronius_discovery_new" SOLAR_NET_ID_POWER_FLOW: SolarNetId = "power_flow" SOLAR_NET_ID_SYSTEM: SolarNetId = "system" SOLAR_NET_RESCAN_TIMER: Final = 60 diff --git a/homeassistant/components/fronius/sensor.py b/homeassistant/components/fronius/sensor.py index d701d0d1860..ff949af0cba 100644 --- a/homeassistant/components/fronius/sensor.py +++ b/homeassistant/components/fronius/sensor.py @@ -24,12 +24,13 @@ from homeassistant.const import ( UnitOfTemperature, ) from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType from homeassistant.helpers.update_coordinator import CoordinatorEntity -from .const import DOMAIN +from .const import DOMAIN, SOLAR_NET_DISCOVERY_NEW if TYPE_CHECKING: from . import FroniusSolarNet @@ -53,7 +54,6 @@ async def async_setup_entry( ) -> None: """Set up Fronius sensor entities based on a config entry.""" 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: inverter_coordinator.add_entities_for_seen_keys( @@ -80,6 +80,19 @@ async def async_setup_entry( 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 class FroniusSensorEntityDescription(SensorEntityDescription):