ha-core/homeassistant/components/ecoforest/__init__.py

60 lines
1.8 KiB
Python

"""The Ecoforest integration."""
from __future__ import annotations
import logging
import httpx
from pyecoforest.api import EcoforestApi
from pyecoforest.exceptions import (
EcoforestAuthenticationRequired,
EcoforestConnectionError,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from .const import DOMAIN
from .coordinator import EcoforestCoordinator
PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.NUMBER, Platform.SWITCH]
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Ecoforest from a config entry."""
host = entry.data[CONF_HOST]
auth = httpx.BasicAuth(entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD])
api = EcoforestApi(host, auth)
try:
device = await api.get()
_LOGGER.debug("Ecoforest: %s", device)
except EcoforestAuthenticationRequired:
_LOGGER.error("Authentication on device %s failed", host)
return False
except EcoforestConnectionError as err:
_LOGGER.error("Error communicating with device %s", host)
raise ConfigEntryNotReady from err
coordinator = EcoforestCoordinator(hass, api)
await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(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."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok