55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
"""SolarEdge Optimizers integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import timedelta
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import CONF_URL, Platform
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from .api import SolarEdgeOptimizerApiClient
|
|
from .const import CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
|
|
from .coordinator import SolarEdgeOptimizerCoordinator
|
|
|
|
PLATFORMS = [Platform.SENSOR]
|
|
type SolarEdgeOptimizerConfigEntry = ConfigEntry[SolarEdgeOptimizerCoordinator]
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant, entry: SolarEdgeOptimizerConfigEntry
|
|
) -> bool:
|
|
"""Set up SolarEdge Optimizers from a config entry."""
|
|
scan_seconds = entry.data.get(
|
|
CONF_SCAN_INTERVAL, int(DEFAULT_SCAN_INTERVAL.total_seconds())
|
|
)
|
|
client = SolarEdgeOptimizerApiClient(
|
|
entry.data[CONF_URL], async_get_clientsession(hass)
|
|
)
|
|
coordinator = SolarEdgeOptimizerCoordinator(
|
|
hass,
|
|
client,
|
|
timedelta(seconds=scan_seconds),
|
|
)
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
entry.runtime_data = coordinator
|
|
entry.async_on_unload(entry.add_update_listener(_async_reload_entry))
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(
|
|
hass: HomeAssistant, entry: SolarEdgeOptimizerConfigEntry
|
|
) -> bool:
|
|
"""Unload a config entry."""
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
|
|
|
|
async def _async_reload_entry(
|
|
hass: HomeAssistant, entry: SolarEdgeOptimizerConfigEntry
|
|
) -> None:
|
|
"""Reload after the entry is reconfigured."""
|
|
await hass.config_entries.async_reload(entry.entry_id)
|