2019-10-21 08:17:21 +00:00
|
|
|
"""The Glances component."""
|
2022-11-03 09:02:25 +00:00
|
|
|
from typing import Any
|
2019-10-21 08:17:21 +00:00
|
|
|
|
2022-11-03 09:02:25 +00:00
|
|
|
from glances_api import Glances
|
2019-10-21 08:17:21 +00:00
|
|
|
|
2022-05-30 13:25:02 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-11-03 09:02:25 +00:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_VERIFY_SSL, Platform
|
2021-05-24 17:23:16 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-10-21 08:17:21 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-17 08:32:54 +00:00
|
|
|
from homeassistant.helpers.httpx_client import get_async_client
|
2019-10-21 08:17:21 +00:00
|
|
|
|
2022-11-03 09:02:25 +00:00
|
|
|
from .const import DOMAIN
|
|
|
|
from .coordinator import GlancesDataUpdateCoordinator
|
2019-10-21 08:17:21 +00:00
|
|
|
|
2021-12-04 12:26:40 +00:00
|
|
|
PLATFORMS = [Platform.SENSOR]
|
2021-04-27 14:09:59 +00:00
|
|
|
|
2022-05-30 13:25:02 +00:00
|
|
|
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
2019-10-21 08:17:21 +00:00
|
|
|
|
|
|
|
|
2022-01-01 21:38:11 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
2019-10-21 08:17:21 +00:00
|
|
|
"""Set up Glances from config entry."""
|
2022-11-03 09:02:25 +00:00
|
|
|
api = get_api(hass, dict(config_entry.data))
|
|
|
|
coordinator = GlancesDataUpdateCoordinator(hass, config_entry, api)
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
|
|
|
|
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = coordinator
|
|
|
|
|
|
|
|
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
2019-10-21 08:17:21 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-01-01 21:38:11 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2019-10-21 08:17:21 +00:00
|
|
|
"""Unload a config entry."""
|
2022-11-03 09:02:25 +00:00
|
|
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
2021-04-27 14:09:59 +00:00
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
2022-11-03 09:02:25 +00:00
|
|
|
if not hass.data[DOMAIN]:
|
|
|
|
del hass.data[DOMAIN]
|
2021-04-27 14:09:59 +00:00
|
|
|
return unload_ok
|
2019-10-21 08:17:21 +00:00
|
|
|
|
|
|
|
|
2022-11-03 09:02:25 +00:00
|
|
|
def get_api(hass: HomeAssistant, entry_data: dict[str, Any]) -> Glances:
|
2019-10-21 08:17:21 +00:00
|
|
|
"""Return the api from glances_api."""
|
2022-11-03 09:02:25 +00:00
|
|
|
entry_data.pop(CONF_NAME, None)
|
|
|
|
httpx_client = get_async_client(hass, verify_ssl=entry_data[CONF_VERIFY_SSL])
|
|
|
|
return Glances(httpx_client=httpx_client, **entry_data)
|