core/homeassistant/components/poolsense/__init__.py

52 lines
1.5 KiB
Python

"""The PoolSense integration."""
import logging
from poolsense import PoolSense
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import aiohttp_client
from .const import DOMAIN
from .coordinator import PoolSenseDataUpdateCoordinator
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up PoolSense from a config entry."""
poolsense = PoolSense(
aiohttp_client.async_get_clientsession(hass),
entry.data[CONF_EMAIL],
entry.data[CONF_PASSWORD],
)
auth_valid = await poolsense.test_poolsense_credentials()
if not auth_valid:
_LOGGER.error("Invalid authentication")
return False
coordinator = PoolSenseDataUpdateCoordinator(hass, entry)
await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok