hass-core/homeassistant/components/glances/coordinator.py
Rami Mosleh 328eda044a
Use DataUpdateCoordinator for glances (#72748)
* use DataUpdateCoordinator for glances

add tests to increase coverage

fix test_config_flow.py

fix codecov/patch

remove unused const, minor tweaks

remove invalid_auth test as it is not implemented

fix type hints

* change to async_forward_entry_setups

* Use Dataupdatecoordinator for glances

* minor fixex

* minor fixes

* minor fix

* remove support_versions const

* coe cleanup

* address comments

* fix sensor native_value

* Rename entry to entry_data in `get_api`

* Remove whitespace in sensor name
2022-11-03 10:02:25 +01:00

42 lines
1.3 KiB
Python

"""Coordinator for Glances integration."""
from datetime import timedelta
import logging
from typing import Any
from glances_api import Glances, exceptions
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
class GlancesDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
"""Get the latest data from Glances api."""
config_entry: ConfigEntry
def __init__(self, hass: HomeAssistant, entry: ConfigEntry, api: Glances) -> None:
"""Initialize the Glances data."""
self.hass = hass
self.config_entry = entry
self.host: str = entry.data[CONF_HOST]
self.api = api
super().__init__(
hass,
_LOGGER,
name=f"{DOMAIN} - {self.host}",
update_interval=timedelta(seconds=60),
)
async def _async_update_data(self) -> dict[str, Any]:
"""Get the latest data from the Glances REST API."""
try:
await self.api.get_data("all")
except exceptions.GlancesApiError as err:
raise UpdateFailed from err
return self.api.data