39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
"""The World Air Quality Index (WAQI) integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from aiowaqi import WAQIClient
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import CONF_API_KEY, Platform
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from .const import DOMAIN
|
|
from .coordinator import WAQIDataUpdateCoordinator
|
|
|
|
PLATFORMS: list[Platform] = [Platform.SENSOR]
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Set up World Air Quality Index (WAQI) from a config entry."""
|
|
|
|
client = WAQIClient(session=async_get_clientsession(hass))
|
|
client.authenticate(entry.data[CONF_API_KEY])
|
|
|
|
waqi_coordinator = WAQIDataUpdateCoordinator(hass, client)
|
|
await waqi_coordinator.async_config_entry_first_refresh()
|
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = waqi_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
|