2024-07-10 21:05:31 +00:00
|
|
|
"""The Autarco integration."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
2024-12-04 10:17:39 +00:00
|
|
|
from autarco import Autarco, AutarcoConnectionError
|
2024-07-10 21:05:31 +00:00
|
|
|
|
|
|
|
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform
|
|
|
|
from homeassistant.core import HomeAssistant
|
2024-12-04 10:17:39 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2024-07-10 21:05:31 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
|
2025-02-07 18:39:21 +00:00
|
|
|
from .coordinator import AutarcoConfigEntry, AutarcoDataUpdateCoordinator
|
2024-07-10 21:05:31 +00:00
|
|
|
|
|
|
|
PLATFORMS: list[Platform] = [Platform.SENSOR]
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: AutarcoConfigEntry) -> bool:
|
|
|
|
"""Set up Autarco from a config entry."""
|
|
|
|
client = Autarco(
|
|
|
|
email=entry.data[CONF_EMAIL],
|
|
|
|
password=entry.data[CONF_PASSWORD],
|
|
|
|
session=async_get_clientsession(hass),
|
|
|
|
)
|
2024-12-04 10:17:39 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
account_sites = await client.get_account()
|
|
|
|
except AutarcoConnectionError as err:
|
|
|
|
await client.close()
|
|
|
|
raise ConfigEntryNotReady from err
|
2024-07-10 21:05:31 +00:00
|
|
|
|
|
|
|
coordinators: list[AutarcoDataUpdateCoordinator] = [
|
2025-02-07 18:39:21 +00:00
|
|
|
AutarcoDataUpdateCoordinator(hass, entry, client, site)
|
|
|
|
for site in account_sites
|
2024-07-10 21:05:31 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
await asyncio.gather(
|
|
|
|
*[
|
|
|
|
coordinator.async_config_entry_first_refresh()
|
|
|
|
for coordinator in coordinators
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
entry.runtime_data = coordinators
|
|
|
|
|
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: AutarcoConfigEntry) -> bool:
|
|
|
|
"""Unload a config entry."""
|
|
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|