2021-05-07 13:59:29 +00:00
|
|
|
"""The Nettigo Air Monitor component."""
|
2024-03-08 14:04:07 +00:00
|
|
|
|
2021-05-07 13:59:29 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
2024-05-05 11:04:01 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2021-05-07 13:59:29 +00:00
|
|
|
|
2021-11-18 01:00:19 +00:00
|
|
|
from aiohttp.client_exceptions import ClientConnectorError, ClientError
|
2021-05-07 13:59:29 +00:00
|
|
|
from nettigo_air_monitor import (
|
|
|
|
ApiError,
|
2023-02-21 23:41:46 +00:00
|
|
|
AuthFailedError,
|
2021-11-18 01:00:19 +00:00
|
|
|
ConnectionOptions,
|
2021-05-07 13:59:29 +00:00
|
|
|
NettigoAirMonitor,
|
|
|
|
)
|
|
|
|
|
2021-06-24 19:58:37 +00:00
|
|
|
from homeassistant.components.air_quality import DOMAIN as AIR_QUALITY_PLATFORM
|
2021-05-07 13:59:29 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-06 03:06:35 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform
|
2021-05-07 13:59:29 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-01-05 14:00:49 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
2024-05-03 11:13:56 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2021-05-07 13:59:29 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2024-05-03 11:13:56 +00:00
|
|
|
|
|
|
|
from .const import ATTR_SDS011, ATTR_SPS30, DOMAIN
|
|
|
|
from .coordinator import NAMDataUpdateCoordinator
|
2021-05-07 13:59:29 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-12-06 03:06:35 +00:00
|
|
|
PLATFORMS = [Platform.BUTTON, Platform.SENSOR]
|
2021-05-07 13:59:29 +00:00
|
|
|
|
2024-05-17 13:42:58 +00:00
|
|
|
type NAMConfigEntry = ConfigEntry[NAMDataUpdateCoordinator]
|
2024-05-03 11:13:56 +00:00
|
|
|
|
2021-05-07 13:59:29 +00:00
|
|
|
|
2024-05-03 11:13:56 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: NAMConfigEntry) -> bool:
|
2021-05-07 13:59:29 +00:00
|
|
|
"""Set up Nettigo as config entry."""
|
2021-05-19 11:20:11 +00:00
|
|
|
host: str = entry.data[CONF_HOST]
|
2021-11-18 01:00:19 +00:00
|
|
|
username: str | None = entry.data.get(CONF_USERNAME)
|
|
|
|
password: str | None = entry.data.get(CONF_PASSWORD)
|
2021-05-07 13:59:29 +00:00
|
|
|
|
|
|
|
websession = async_get_clientsession(hass)
|
|
|
|
|
2021-11-18 01:00:19 +00:00
|
|
|
options = ConnectionOptions(host=host, username=username, password=password)
|
|
|
|
try:
|
|
|
|
nam = await NettigoAirMonitor.create(websession, options)
|
2024-02-05 11:08:18 +00:00
|
|
|
except (ApiError, ClientError, ClientConnectorError, TimeoutError) as err:
|
2021-11-18 01:00:19 +00:00
|
|
|
raise ConfigEntryNotReady from err
|
|
|
|
|
2022-06-07 16:56:11 +00:00
|
|
|
try:
|
|
|
|
await nam.async_check_credentials()
|
2022-10-30 19:01:10 +00:00
|
|
|
except ApiError as err:
|
|
|
|
raise ConfigEntryNotReady from err
|
2023-02-21 23:41:46 +00:00
|
|
|
except AuthFailedError as err:
|
2022-06-07 16:56:11 +00:00
|
|
|
raise ConfigEntryAuthFailed from err
|
|
|
|
|
2024-05-05 11:04:01 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
assert entry.unique_id
|
|
|
|
|
2021-11-18 01:00:19 +00:00
|
|
|
coordinator = NAMDataUpdateCoordinator(hass, nam, entry.unique_id)
|
2021-05-07 13:59:29 +00:00
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
|
2024-05-03 11:13:56 +00:00
|
|
|
entry.runtime_data = coordinator
|
2021-05-07 13:59:29 +00:00
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2021-05-07 13:59:29 +00:00
|
|
|
|
2021-06-24 19:58:37 +00:00
|
|
|
# Remove air_quality entities from registry if they exist
|
2023-03-01 07:02:51 +00:00
|
|
|
ent_reg = er.async_get(hass)
|
2021-07-19 13:57:06 +00:00
|
|
|
for sensor_type in ("sds", ATTR_SDS011, ATTR_SPS30):
|
2021-06-24 19:58:37 +00:00
|
|
|
unique_id = f"{coordinator.unique_id}-{sensor_type}"
|
|
|
|
if entity_id := ent_reg.async_get_entity_id(
|
|
|
|
AIR_QUALITY_PLATFORM, DOMAIN, unique_id
|
|
|
|
):
|
|
|
|
_LOGGER.debug("Removing deprecated air_quality entity %s", entity_id)
|
|
|
|
ent_reg.async_remove(entity_id)
|
|
|
|
|
2021-05-07 13:59:29 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2024-05-03 11:13:56 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: NAMConfigEntry) -> bool:
|
2021-05-07 13:59:29 +00:00
|
|
|
"""Unload a config entry."""
|
2024-05-03 11:13:56 +00:00
|
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|