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
This commit is contained in:
parent
739ed6a6c8
commit
328eda044a
12 changed files with 279 additions and 339 deletions
42
homeassistant/components/glances/coordinator.py
Normal file
42
homeassistant/components/glances/coordinator.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
"""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
|
Loading…
Add table
Add a link
Reference in a new issue