hass-core/homeassistant/components/glances/coordinator.py
Rami Mosleh 8cbe394028
Use get_ha_sensor_data method to update glances sensors (#83983)
* Use `get_ha_sensor_data` method to update sensor state

* update tests

* Use `get_ha_sensor_data` to validate connection

* Update test_sensor.py

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
2023-03-31 14:27:37 +02:00

41 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:
return await self.api.get_ha_sensor_data()
except exceptions.GlancesApiError as err:
raise UpdateFailed from err