Explicitly pass in the config_entry in solaredge coordinator (#137941)

explicitly pass in the config_entry in coordinator
This commit is contained in:
Michael 2025-02-08 21:28:48 +01:00 committed by GitHub
parent beb5c3b838
commit 12072b625c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 13 deletions

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from abc import ABC, abstractmethod
from datetime import date, datetime, timedelta
from typing import Any
from typing import TYPE_CHECKING, Any
from aiosolaredge import SolarEdge
from stringcase import snakecase
@ -21,13 +21,22 @@ from .const import (
POWER_FLOW_UPDATE_DELAY,
)
if TYPE_CHECKING:
from .types import SolarEdgeConfigEntry
class SolarEdgeDataService(ABC):
"""Get and update the latest data."""
coordinator: DataUpdateCoordinator[None]
def __init__(self, hass: HomeAssistant, api: SolarEdge, site_id: str) -> None:
def __init__(
self,
hass: HomeAssistant,
config_entry: SolarEdgeConfigEntry,
api: SolarEdge,
site_id: str,
) -> None:
"""Initialize the data object."""
self.api = api
self.site_id = site_id
@ -36,6 +45,7 @@ class SolarEdgeDataService(ABC):
self.attributes: dict[str, Any] = {}
self.hass = hass
self.config_entry = config_entry
@callback
def async_setup(self) -> None:
@ -43,6 +53,7 @@ class SolarEdgeDataService(ABC):
self.coordinator = DataUpdateCoordinator(
self.hass,
LOGGER,
config_entry=self.config_entry,
name=str(self),
update_method=self.async_update_data,
update_interval=self.update_interval,
@ -174,9 +185,15 @@ class SolarEdgeInventoryDataService(SolarEdgeDataService):
class SolarEdgeEnergyDetailsService(SolarEdgeDataService):
"""Get and update the latest power flow data."""
def __init__(self, hass: HomeAssistant, api: SolarEdge, site_id: str) -> None:
def __init__(
self,
hass: HomeAssistant,
config_entry: SolarEdgeConfigEntry,
api: SolarEdge,
site_id: str,
) -> None:
"""Initialize the power flow data service."""
super().__init__(hass, api, site_id)
super().__init__(hass, config_entry, api, site_id)
self.unit = None
@ -234,9 +251,15 @@ class SolarEdgeEnergyDetailsService(SolarEdgeDataService):
class SolarEdgePowerFlowDataService(SolarEdgeDataService):
"""Get and update the latest power flow data."""
def __init__(self, hass: HomeAssistant, api: SolarEdge, site_id: str) -> None:
def __init__(
self,
hass: HomeAssistant,
config_entry: SolarEdgeConfigEntry,
api: SolarEdge,
site_id: str,
) -> None:
"""Initialize the power flow data service."""
super().__init__(hass, api, site_id)
super().__init__(hass, config_entry, api, site_id)
self.unit = None

View File

@ -206,7 +206,7 @@ async def async_setup_entry(
"""Add an solarEdge entry."""
# Add the needed sensors to hass
api = entry.runtime_data[DATA_API_CLIENT]
sensor_factory = SolarEdgeSensorFactory(hass, entry.data[CONF_SITE_ID], api)
sensor_factory = SolarEdgeSensorFactory(hass, entry, entry.data[CONF_SITE_ID], api)
for service in sensor_factory.all_services:
service.async_setup()
await service.coordinator.async_refresh()
@ -222,14 +222,20 @@ async def async_setup_entry(
class SolarEdgeSensorFactory:
"""Factory which creates sensors based on the sensor_key."""
def __init__(self, hass: HomeAssistant, site_id: str, api: SolarEdge) -> None:
def __init__(
self,
hass: HomeAssistant,
config_entry: SolarEdgeConfigEntry,
site_id: str,
api: SolarEdge,
) -> None:
"""Initialize the factory."""
details = SolarEdgeDetailsDataService(hass, api, site_id)
overview = SolarEdgeOverviewDataService(hass, api, site_id)
inventory = SolarEdgeInventoryDataService(hass, api, site_id)
flow = SolarEdgePowerFlowDataService(hass, api, site_id)
energy = SolarEdgeEnergyDetailsService(hass, api, site_id)
details = SolarEdgeDetailsDataService(hass, config_entry, api, site_id)
overview = SolarEdgeOverviewDataService(hass, config_entry, api, site_id)
inventory = SolarEdgeInventoryDataService(hass, config_entry, api, site_id)
flow = SolarEdgePowerFlowDataService(hass, config_entry, api, site_id)
energy = SolarEdgeEnergyDetailsService(hass, config_entry, api, site_id)
self.all_services = (details, overview, inventory, flow, energy)