diff --git a/.gitignore b/.gitignore index afa9529..6291566 100644 --- a/.gitignore +++ b/.gitignore @@ -144,6 +144,10 @@ dmypy.json # Cython debug symbols cython_debug/ +# Local-only files and directories (not versioned) +*.local +**/*.local + # Developper settings (vscode) **/.vscode/settings.json .orcommit.json diff --git a/custom_components/clouding/coordinator.py b/custom_components/clouding/coordinator.py index 202cf74..4f20bf4 100644 --- a/custom_components/clouding/coordinator.py +++ b/custom_components/clouding/coordinator.py @@ -10,6 +10,7 @@ from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed +from homeassistant.util import dt as dt_util from .const import DOMAIN from .pythonclouding import ( @@ -20,7 +21,7 @@ ) if TYPE_CHECKING: - from datetime import timedelta + from datetime import datetime, timedelta from homeassistant.core import HomeAssistant @@ -53,6 +54,7 @@ def __init__(self, hass: HomeAssistant, config_entry: CloudingConfigEntry, updat ) session = async_get_clientsession(hass) self.api = Clouding(session, config_entry.data[CONF_API_KEY]) + self.last_api_call: datetime | None = None async def _async_update_data(self) -> dict[str, CloudingServer]: """Fetch the latest data from Clouding.io. @@ -67,6 +69,7 @@ async def _async_update_data(self) -> dict[str, CloudingServer]: """ try: + self.last_api_call = dt_util.utcnow() servers: dict[str, CloudingServer] = await self.api.get_servers() except CloudingAuthenticationError as e: raise ConfigEntryAuthFailed( diff --git a/custom_components/clouding/sensor.py b/custom_components/clouding/sensor.py index 13f6ea9..dad77c8 100644 --- a/custom_components/clouding/sensor.py +++ b/custom_components/clouding/sensor.py @@ -33,6 +33,7 @@ class EnumCloudingSensor(StrEnum): SERVER_FLAVOR = "flavor" SERVER_HOSTNAME = "hostname" + SERVER_LAST_API_CALL = "last_api_call" SERVER_PRIVATE_ID = "private_ip" SERVER_RAM_GB = "ram_gb" SERVER_CREATED_AT = "created_at" @@ -115,6 +116,12 @@ class CloudingSensorEntityDescription(SensorEntityDescription): suggested_display_precision=0, device_class=SensorDeviceClass.DATA_SIZE, ), + CloudingSensorEntityDescription( + key=EnumCloudingSensor.SERVER_LAST_API_CALL, + translation_key=EnumCloudingSensor.SERVER_LAST_API_CALL, + name_suffix="Last API Call", + device_class=SensorDeviceClass.TIMESTAMP, + ), ) @@ -204,9 +211,9 @@ def _handle_coordinator_update(self) -> None: """ prev_value = self._attr_native_value - prev_icon = self._attr_icon + self._update_attr() - if self._attr_native_value != prev_value or self._attr_icon != prev_icon: + if self._attr_native_value != prev_value: super()._handle_coordinator_update() @callback @@ -218,9 +225,12 @@ def _update_attr(self) -> None: """ try: - self._attr_native_value = getattr( - self.coordinator.api.servers[self._server_unique_id], "attr_" + self.entity_description.key - ) + if self.entity_description.key == EnumCloudingSensor.SERVER_LAST_API_CALL: + self._attr_native_value = self.coordinator.last_api_call + else: + self._attr_native_value = getattr( + self.coordinator.api.servers[self._server_unique_id], "attr_" + self.entity_description.key + ) if self.entity_description.key == EnumCloudingSensor.SERVER_STATUS: value: str = str(self._attr_native_value).lower() diff --git a/custom_components/clouding/translations/en.json b/custom_components/clouding/translations/en.json index 9272710..c46fa74 100644 --- a/custom_components/clouding/translations/en.json +++ b/custom_components/clouding/translations/en.json @@ -95,6 +95,9 @@ }, "volume_size_gb": { "name": "SSD" + }, + "last_api_call": { + "name": "Last API Call" } } },