Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion custom_components/clouding/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -20,7 +21,7 @@
)

if TYPE_CHECKING:
from datetime import timedelta
from datetime import datetime, timedelta

from homeassistant.core import HomeAssistant

Expand Down Expand Up @@ -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.
Expand All @@ -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(
Expand Down
20 changes: 15 additions & 5 deletions custom_components/clouding/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
),
)


Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions custom_components/clouding/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
},
"volume_size_gb": {
"name": "SSD"
},
"last_api_call": {
"name": "Last API Call"
}
}
},
Expand Down
Loading