50 lines
1.7 KiB
Python
50 lines
1.7 KiB
Python
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
from datetime import timedelta
|
|
import aiohttp
|
|
import logging
|
|
from .const import KEY_SCAN_INTERVAL
|
|
from .api import async_get_device_list
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
from .const import DOMAIN
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
session = aiohttp.ClientSession()
|
|
|
|
async def fetch_data():
|
|
try:
|
|
return await async_get_device_list(entry, session)
|
|
except Exception as e:
|
|
logging.error(f"Error fetching data: {e}")
|
|
raise UpdateFailed(f"API error: {e}")
|
|
|
|
update_interval = timedelta(seconds=entry.data.get(KEY_SCAN_INTERVAL, 2))
|
|
coordinator = DataUpdateCoordinator(
|
|
hass,
|
|
_LOGGER,
|
|
name="SunlitSolar Sensoren",
|
|
update_method=fetch_data,
|
|
update_interval=update_interval,
|
|
)
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
|
|
"coordinator": coordinator,
|
|
"session": session
|
|
}
|
|
await hass.config_entries.async_forward_entry_setup(entry, "sensor")
|
|
await hass.config_entries.async_forward_entry_setup(entry, "binary_sensor")
|
|
# hass.async_create_task(
|
|
# hass.config_entries.async_forward_entry_setup(entry, "sensor")
|
|
# )
|
|
|
|
return True
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
await hass.data[DOMAIN][entry.entry_id]["session"].close()
|
|
await hass.config_entries.async_forward_entry_unload(entry, "sensor")
|
|
return await hass.config_entries.async_forward_entry_unload(entry, "binary_sensor")
|