Add album last update time sensor
This commit is contained in:
@@ -6,9 +6,11 @@ import logging
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
|
SensorDeviceClass,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
|
from datetime import datetime
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
@@ -39,21 +41,18 @@ async def async_setup_entry(
|
|||||||
coordinator: ImmichAlbumWatcherCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator: ImmichAlbumWatcherCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
album_ids = entry.options.get(CONF_ALBUMS, [])
|
album_ids = entry.options.get(CONF_ALBUMS, [])
|
||||||
|
|
||||||
entities = [
|
entities: list[SensorEntity] = []
|
||||||
ImmichAlbumSensor(coordinator, entry, album_id)
|
for album_id in album_ids:
|
||||||
for album_id in album_ids
|
entities.append(ImmichAlbumAssetCountSensor(coordinator, entry, album_id))
|
||||||
]
|
entities.append(ImmichAlbumLastUpdatedSensor(coordinator, entry, album_id))
|
||||||
|
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class ImmichAlbumSensor(CoordinatorEntity[ImmichAlbumWatcherCoordinator], SensorEntity):
|
class ImmichAlbumBaseSensor(CoordinatorEntity[ImmichAlbumWatcherCoordinator], SensorEntity):
|
||||||
"""Sensor representing an Immich album."""
|
"""Base sensor for Immich album."""
|
||||||
|
|
||||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
_attr_has_entity_name = True
|
||||||
_attr_icon = "mdi:image-album"
|
|
||||||
|
|
||||||
_attr_translation_key = "album_asset_count"
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@@ -66,10 +65,6 @@ class ImmichAlbumSensor(CoordinatorEntity[ImmichAlbumWatcherCoordinator], Sensor
|
|||||||
self._album_id = album_id
|
self._album_id = album_id
|
||||||
self._entry = entry
|
self._entry = entry
|
||||||
|
|
||||||
# Entity IDs and names will be set when data is available
|
|
||||||
self._attr_unique_id = f"{entry.entry_id}_{album_id}"
|
|
||||||
self._attr_has_entity_name = True
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _album_data(self) -> AlbumData | None:
|
def _album_data(self) -> AlbumData | None:
|
||||||
"""Get the album data from coordinator."""
|
"""Get the album data from coordinator."""
|
||||||
@@ -84,6 +79,44 @@ class ImmichAlbumSensor(CoordinatorEntity[ImmichAlbumWatcherCoordinator], Sensor
|
|||||||
return {"album_name": self._album_data.name}
|
return {"album_name": self._album_data.name}
|
||||||
return {"album_name": f"Album {self._album_id[:8]}"}
|
return {"album_name": f"Album {self._album_id[:8]}"}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
"""Return if entity is available."""
|
||||||
|
return self.coordinator.last_update_success and self._album_data is not None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_info(self) -> DeviceInfo:
|
||||||
|
"""Return device info."""
|
||||||
|
return DeviceInfo(
|
||||||
|
identifiers={(DOMAIN, self._entry.entry_id)},
|
||||||
|
name="Immich Album Watcher",
|
||||||
|
manufacturer="Immich",
|
||||||
|
entry_type="service",
|
||||||
|
)
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _handle_coordinator_update(self) -> None:
|
||||||
|
"""Handle updated data from the coordinator."""
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
|
||||||
|
class ImmichAlbumAssetCountSensor(ImmichAlbumBaseSensor):
|
||||||
|
"""Sensor representing an Immich album asset count."""
|
||||||
|
|
||||||
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||||
|
_attr_icon = "mdi:image-album"
|
||||||
|
_attr_translation_key = "album_asset_count"
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: ImmichAlbumWatcherCoordinator,
|
||||||
|
entry: ConfigEntry,
|
||||||
|
album_id: str,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the sensor."""
|
||||||
|
super().__init__(coordinator, entry, album_id)
|
||||||
|
self._attr_unique_id = f"{entry.entry_id}_{album_id}_asset_count"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> int | None:
|
def native_value(self) -> int | None:
|
||||||
"""Return the state of the sensor (asset count)."""
|
"""Return the state of the sensor (asset count)."""
|
||||||
@@ -91,11 +124,6 @@ class ImmichAlbumSensor(CoordinatorEntity[ImmichAlbumWatcherCoordinator], Sensor
|
|||||||
return self._album_data.asset_count
|
return self._album_data.asset_count
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self) -> bool:
|
|
||||||
"""Return if entity is available."""
|
|
||||||
return self.coordinator.last_update_success and self._album_data is not None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self) -> dict[str, Any]:
|
def extra_state_attributes(self) -> dict[str, Any]:
|
||||||
"""Return extra state attributes."""
|
"""Return extra state attributes."""
|
||||||
@@ -119,17 +147,30 @@ class ImmichAlbumSensor(CoordinatorEntity[ImmichAlbumWatcherCoordinator], Sensor
|
|||||||
|
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self) -> DeviceInfo:
|
|
||||||
"""Return device info."""
|
|
||||||
return DeviceInfo(
|
|
||||||
identifiers={(DOMAIN, self._entry.entry_id)},
|
|
||||||
name="Immich Album Watcher",
|
|
||||||
manufacturer="Immich",
|
|
||||||
entry_type="service",
|
|
||||||
)
|
|
||||||
|
|
||||||
@callback
|
class ImmichAlbumLastUpdatedSensor(ImmichAlbumBaseSensor):
|
||||||
def _handle_coordinator_update(self) -> None:
|
"""Sensor representing an Immich album last updated time."""
|
||||||
"""Handle updated data from the coordinator."""
|
|
||||||
self.async_write_ha_state()
|
_attr_device_class = SensorDeviceClass.TIMESTAMP
|
||||||
|
_attr_icon = "mdi:clock-outline"
|
||||||
|
_attr_translation_key = "album_last_updated"
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: ImmichAlbumWatcherCoordinator,
|
||||||
|
entry: ConfigEntry,
|
||||||
|
album_id: str,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the sensor."""
|
||||||
|
super().__init__(coordinator, entry, album_id)
|
||||||
|
self._attr_unique_id = f"{entry.entry_id}_{album_id}_last_updated"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_value(self) -> datetime | None:
|
||||||
|
"""Return the state of the sensor (last updated datetime)."""
|
||||||
|
if self._album_data and self._album_data.updated_at:
|
||||||
|
try:
|
||||||
|
return datetime.fromisoformat(self._album_data.updated_at.replace("Z", "+00:00"))
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
return None
|
||||||
|
|||||||
@@ -3,6 +3,9 @@
|
|||||||
"sensor": {
|
"sensor": {
|
||||||
"album_asset_count": {
|
"album_asset_count": {
|
||||||
"name": "{album_name}: Asset Count"
|
"name": "{album_name}: Asset Count"
|
||||||
|
},
|
||||||
|
"album_last_updated": {
|
||||||
|
"name": "{album_name}: Last Updated"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -3,6 +3,9 @@
|
|||||||
"sensor": {
|
"sensor": {
|
||||||
"album_asset_count": {
|
"album_asset_count": {
|
||||||
"name": "{album_name}: Asset Count"
|
"name": "{album_name}: Asset Count"
|
||||||
|
},
|
||||||
|
"album_last_updated": {
|
||||||
|
"name": "{album_name}: Last Updated"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -54,6 +54,9 @@
|
|||||||
"sensor": {
|
"sensor": {
|
||||||
"album_asset_count": {
|
"album_asset_count": {
|
||||||
"name": "{album_name}: Число файлов"
|
"name": "{album_name}: Число файлов"
|
||||||
|
},
|
||||||
|
"album_last_updated": {
|
||||||
|
"name": "{album_name}: Последнее обновление"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user