1 """Coordinator for Glances integration."""
3 from datetime
import datetime, timedelta
7 from glances_api
import Glances, exceptions
16 from .const
import DEFAULT_SCAN_INTERVAL, DOMAIN
18 _LOGGER = logging.getLogger(__name__)
22 """Get the latest data from Glances api."""
24 config_entry: ConfigEntry
26 def __init__(self, hass: HomeAssistant, entry: ConfigEntry, api: Glances) ->
None:
27 """Initialize the Glances data."""
30 self.host: str = entry.data[CONF_HOST]
35 name=f
"{DOMAIN} - {self.host}",
36 update_interval=DEFAULT_SCAN_INTERVAL,
40 """Get the latest data from the Glances REST API."""
42 data = await self.
apiapi.get_ha_sensor_data()
43 except exceptions.GlancesApiAuthorizationError
as err:
44 raise ConfigEntryAuthFailed
from err
45 except exceptions.GlancesApiError
as err:
46 raise UpdateFailed
from err
48 uptime: datetime |
None =
None
49 up_duration: timedelta |
None =
None
50 if "uptime" in data
and (up_duration :=
parse_duration(data[
"uptime"])):
51 uptime = self.
datadata[
"computed"][
"uptime"]
if self.
datadata
else None
54 if uptime
is None or self.
datadata[
"computed"][
"uptime_duration"] > up_duration:
55 uptime =
utcnow() - up_duration
56 data[
"computed"] = {
"uptime_duration": up_duration,
"uptime": uptime}
dict[str, Any] _async_update_data(self)
None __init__(self, HomeAssistant hass, ConfigEntry entry, Glances api)
dt.timedelta|None parse_duration(str value)