Convert solaredge to use entry.runtime_data (#121642)

pull/121651/head
J. Nick Koston 2024-07-09 23:22:44 -07:00 committed by GitHub
parent 72c3c0eb53
commit 4245357403
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 14 deletions

View File

@ -7,21 +7,21 @@ import socket
from aiohttp import ClientError
from aiosolaredge import SolarEdge
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from .const import CONF_SITE_ID, DATA_API_CLIENT, DOMAIN, LOGGER
from .const import CONF_SITE_ID, DOMAIN, LOGGER
from .types import SolarEdgeConfigEntry, SolarEdgeData
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
PLATFORMS = [Platform.SENSOR]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: SolarEdgeConfigEntry) -> bool:
"""Set up SolarEdge from a config entry."""
session = async_get_clientsession(hass)
api = SolarEdge(entry.data[CONF_API_KEY], session)
@ -40,14 +40,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
LOGGER.error("SolarEdge site is not active")
return False
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {DATA_API_CLIENT: api}
entry.runtime_data = SolarEdgeData(api_client=api)
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: SolarEdgeConfigEntry) -> bool:
"""Unload SolarEdge config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
del hass.data[DOMAIN][entry.entry_id]
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

View File

@ -2,12 +2,13 @@
from datetime import timedelta
import logging
from typing import Final
DOMAIN = "solaredge"
LOGGER = logging.getLogger(__package__)
DATA_API_CLIENT = "api_client"
DATA_API_CLIENT: Final = "api_client"
# Config for solaredge monitoring api requests.
CONF_SITE_ID = "site_id"

View File

@ -13,7 +13,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, UnitOfEnergy, UnitOfPower
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
@ -32,6 +31,7 @@ from .coordinator import (
SolarEdgeOverviewDataService,
SolarEdgePowerFlowDataService,
)
from .types import SolarEdgeConfigEntry
@dataclass(frozen=True, kw_only=True)
@ -200,13 +200,12 @@ SENSOR_TYPES = [
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: SolarEdgeConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Add an solarEdge entry."""
# Add the needed sensors to hass
api: SolarEdge = hass.data[DOMAIN][entry.entry_id][DATA_API_CLIENT]
api = entry.runtime_data[DATA_API_CLIENT]
sensor_factory = SolarEdgeSensorFactory(hass, entry.data[CONF_SITE_ID], api)
for service in sensor_factory.all_services:
service.async_setup()

View File

@ -0,0 +1,17 @@
"""Typing for the SolarEdge Monitoring API."""
from __future__ import annotations
from typing import TypedDict
from aiosolaredge import SolarEdge
from homeassistant.config_entries import ConfigEntry
type SolarEdgeConfigEntry = ConfigEntry[SolarEdgeData]
class SolarEdgeData(TypedDict):
"""Data for the solaredge integration."""
api_client: SolarEdge