2019-10-21 08:17:21 +00:00
|
|
|
"""The Glances component."""
|
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from glances_api import Glances, exceptions
|
|
|
|
|
2022-05-30 13:25:02 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2019-10-21 08:17:21 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
CONF_VERIFY_SSL,
|
2021-12-04 12:26:40 +00:00
|
|
|
Platform,
|
2019-10-21 08:17:21 +00:00
|
|
|
)
|
2021-05-24 17:23:16 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-10-21 08:17:21 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|
|
|
from homeassistant.helpers.event import async_track_time_interval
|
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-05-30 13:25:02 +00:00
|
|
|
from .const import DATA_UPDATED, DEFAULT_SCAN_INTERVAL, DOMAIN
|
2019-10-21 08:17:21 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
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."""
|
|
|
|
client = GlancesData(hass, config_entry)
|
|
|
|
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = client
|
|
|
|
if not await client.async_setup():
|
|
|
|
return False
|
|
|
|
|
|
|
|
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."""
|
2021-04-27 14:09:59 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
return unload_ok
|
2019-10-21 08:17:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GlancesData:
|
|
|
|
"""Get the latest data from Glances api."""
|
|
|
|
|
|
|
|
def __init__(self, hass, config_entry):
|
|
|
|
"""Initialize the Glances data."""
|
|
|
|
self.hass = hass
|
|
|
|
self.config_entry = config_entry
|
|
|
|
self.api = None
|
|
|
|
self.unsub_timer = None
|
|
|
|
self.available = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def host(self):
|
|
|
|
"""Return client host."""
|
|
|
|
return self.config_entry.data[CONF_HOST]
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Get the latest data from the Glances REST API."""
|
|
|
|
try:
|
2022-01-17 08:32:54 +00:00
|
|
|
await self.api.get_data("all")
|
2019-10-21 08:17:21 +00:00
|
|
|
self.available = True
|
|
|
|
except exceptions.GlancesApiError:
|
|
|
|
_LOGGER.error("Unable to fetch data from Glances")
|
|
|
|
self.available = False
|
|
|
|
_LOGGER.debug("Glances data updated")
|
|
|
|
async_dispatcher_send(self.hass, DATA_UPDATED)
|
|
|
|
|
|
|
|
async def async_setup(self):
|
|
|
|
"""Set up the Glances client."""
|
|
|
|
try:
|
|
|
|
self.api = get_api(self.hass, self.config_entry.data)
|
2022-01-17 08:32:54 +00:00
|
|
|
await self.api.get_data("all")
|
2019-10-21 08:17:21 +00:00
|
|
|
self.available = True
|
|
|
|
_LOGGER.debug("Successfully connected to Glances")
|
|
|
|
|
2020-08-28 11:50:32 +00:00
|
|
|
except exceptions.GlancesApiConnectionError as err:
|
2019-10-21 08:17:21 +00:00
|
|
|
_LOGGER.debug("Can not connect to Glances")
|
2020-08-28 11:50:32 +00:00
|
|
|
raise ConfigEntryNotReady from err
|
2019-10-21 08:17:21 +00:00
|
|
|
|
|
|
|
self.add_options()
|
|
|
|
self.set_scan_interval(self.config_entry.options[CONF_SCAN_INTERVAL])
|
2021-04-27 14:09:59 +00:00
|
|
|
self.config_entry.async_on_unload(
|
|
|
|
self.config_entry.add_update_listener(self.async_options_updated)
|
2019-10-21 08:17:21 +00:00
|
|
|
)
|
2021-04-27 14:09:59 +00:00
|
|
|
|
|
|
|
self.hass.config_entries.async_setup_platforms(self.config_entry, PLATFORMS)
|
|
|
|
|
2019-10-21 08:17:21 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def add_options(self):
|
|
|
|
"""Add options for Glances integration."""
|
|
|
|
if not self.config_entry.options:
|
|
|
|
options = {CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL}
|
|
|
|
self.hass.config_entries.async_update_entry(
|
|
|
|
self.config_entry, options=options
|
|
|
|
)
|
|
|
|
|
|
|
|
def set_scan_interval(self, scan_interval):
|
|
|
|
"""Update scan interval."""
|
|
|
|
|
|
|
|
async def refresh(event_time):
|
|
|
|
"""Get the latest data from Glances api."""
|
|
|
|
await self.async_update()
|
|
|
|
|
|
|
|
if self.unsub_timer is not None:
|
|
|
|
self.unsub_timer()
|
|
|
|
self.unsub_timer = async_track_time_interval(
|
|
|
|
self.hass, refresh, timedelta(seconds=scan_interval)
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
2022-02-02 09:36:04 +00:00
|
|
|
async def async_options_updated(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
2019-10-21 08:17:21 +00:00
|
|
|
"""Triggered by config entry options updates."""
|
|
|
|
hass.data[DOMAIN][entry.entry_id].set_scan_interval(
|
|
|
|
entry.options[CONF_SCAN_INTERVAL]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get_api(hass, entry):
|
|
|
|
"""Return the api from glances_api."""
|
|
|
|
params = entry.copy()
|
|
|
|
params.pop(CONF_NAME)
|
2022-01-17 08:32:54 +00:00
|
|
|
verify_ssl = params.pop(CONF_VERIFY_SSL, True)
|
|
|
|
httpx_client = get_async_client(hass, verify_ssl=verify_ssl)
|
|
|
|
return Glances(httpx_client=httpx_client, **params)
|