Enhance get_assets service with flexible filtering and sorting
All checks were successful
Validate / Hassfest (push) Successful in 5s
All checks were successful
Validate / Hassfest (push) Successful in 5s
- Replace filter parameter with independent favorite_only boolean - Add order_by parameter supporting date, rating, and name sorting - Rename count to limit for clarity - Add date range filtering with min_date and max_date parameters - Add asset_type filtering for photos and videos - Update README with language support section and fixed sensor list - Add translations for all new parameters Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -362,18 +362,26 @@ class ImmichAlbumWatcherCoordinator(DataUpdateCoordinator[AlbumData | None]):
|
||||
|
||||
async def async_get_assets(
|
||||
self,
|
||||
count: int = 10,
|
||||
filter: str = "none",
|
||||
limit: int = 10,
|
||||
favorite_only: bool = False,
|
||||
filter_min_rating: int = 1,
|
||||
order_by: str = "date",
|
||||
order: str = "descending",
|
||||
asset_type: str = "all",
|
||||
min_date: str | None = None,
|
||||
max_date: str | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Get assets from the album with optional filtering and ordering.
|
||||
|
||||
Args:
|
||||
count: Maximum number of assets to return (1-100)
|
||||
filter: Filter type - 'none', 'favorite', or 'rating'
|
||||
filter_min_rating: Minimum rating for assets (1-5), used when filter='rating'
|
||||
order: Sort order - 'ascending', 'descending', or 'random'
|
||||
limit: Maximum number of assets to return (1-100)
|
||||
favorite_only: Filter to show only favorite assets
|
||||
filter_min_rating: Minimum rating for assets (1-5)
|
||||
order_by: Field to sort by - 'date', 'rating', or 'name'
|
||||
order: Sort direction - 'ascending', 'descending', or 'random'
|
||||
asset_type: Asset type filter - 'all', 'photo', or 'video'
|
||||
min_date: Filter assets created on or after this date (ISO 8601 format)
|
||||
max_date: Filter assets created on or before this date (ISO 8601 format)
|
||||
|
||||
Returns:
|
||||
List of asset data dictionaries
|
||||
@@ -384,23 +392,54 @@ class ImmichAlbumWatcherCoordinator(DataUpdateCoordinator[AlbumData | None]):
|
||||
# Start with all processed assets only
|
||||
assets = [a for a in self.data.assets.values() if a.is_processed]
|
||||
|
||||
# Apply filtering
|
||||
if filter == "favorite":
|
||||
# Apply favorite filter
|
||||
if favorite_only:
|
||||
assets = [a for a in assets if a.is_favorite]
|
||||
elif filter == "rating":
|
||||
|
||||
# Apply rating filter
|
||||
if filter_min_rating > 1:
|
||||
assets = [a for a in assets if a.rating is not None and a.rating >= filter_min_rating]
|
||||
|
||||
# Apply asset type filtering
|
||||
if asset_type == "photo":
|
||||
assets = [a for a in assets if a.type == ASSET_TYPE_IMAGE]
|
||||
elif asset_type == "video":
|
||||
assets = [a for a in assets if a.type == ASSET_TYPE_VIDEO]
|
||||
|
||||
# Apply date filtering
|
||||
if min_date:
|
||||
assets = [a for a in assets if a.created_at >= min_date]
|
||||
if max_date:
|
||||
assets = [a for a in assets if a.created_at <= max_date]
|
||||
|
||||
# Apply ordering
|
||||
if order == "random":
|
||||
import random
|
||||
random.shuffle(assets)
|
||||
elif order == "ascending":
|
||||
assets = sorted(assets, key=lambda a: a.created_at, reverse=False)
|
||||
else: # descending (default)
|
||||
assets = sorted(assets, key=lambda a: a.created_at, reverse=True)
|
||||
else:
|
||||
# Determine sort key based on order_by
|
||||
if order_by == "rating":
|
||||
# Sort by rating, putting None values last
|
||||
assets = sorted(
|
||||
assets,
|
||||
key=lambda a: (a.rating is None, a.rating if a.rating is not None else 0),
|
||||
reverse=(order == "descending")
|
||||
)
|
||||
elif order_by == "name":
|
||||
assets = sorted(
|
||||
assets,
|
||||
key=lambda a: a.filename.lower(),
|
||||
reverse=(order == "descending")
|
||||
)
|
||||
else: # date (default)
|
||||
assets = sorted(
|
||||
assets,
|
||||
key=lambda a: a.created_at,
|
||||
reverse=(order == "descending")
|
||||
)
|
||||
|
||||
# Limit count
|
||||
assets = assets[:count]
|
||||
# Limit results
|
||||
assets = assets[:limit]
|
||||
|
||||
# Build result with all available asset data (matching event data)
|
||||
result = []
|
||||
|
||||
@@ -8,5 +8,5 @@
|
||||
"iot_class": "cloud_polling",
|
||||
"issue_tracker": "https://github.com/DolgolyovAlexei/haos-hacs-immich-album-watcher/issues",
|
||||
"requirements": [],
|
||||
"version": "2.1.0"
|
||||
"version": "2.2.0"
|
||||
}
|
||||
|
||||
@@ -182,17 +182,25 @@ class ImmichAlbumBaseSensor(CoordinatorEntity[ImmichAlbumWatcherCoordinator], Se
|
||||
|
||||
async def async_get_assets(
|
||||
self,
|
||||
count: int = 10,
|
||||
filter: str = "none",
|
||||
limit: int = 10,
|
||||
favorite_only: bool = False,
|
||||
filter_min_rating: int = 1,
|
||||
order_by: str = "date",
|
||||
order: str = "descending",
|
||||
asset_type: str = "all",
|
||||
min_date: str | None = None,
|
||||
max_date: str | None = None,
|
||||
) -> ServiceResponse:
|
||||
"""Get assets for this album with optional filtering and ordering."""
|
||||
assets = await self.coordinator.async_get_assets(
|
||||
count=count,
|
||||
filter=filter,
|
||||
limit=limit,
|
||||
favorite_only=favorite_only,
|
||||
filter_min_rating=filter_min_rating,
|
||||
order_by=order_by,
|
||||
order=order,
|
||||
asset_type=asset_type,
|
||||
min_date=min_date,
|
||||
max_date=max_date,
|
||||
)
|
||||
return {"assets": assets}
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ get_assets:
|
||||
integration: immich_album_watcher
|
||||
domain: sensor
|
||||
fields:
|
||||
count:
|
||||
name: Count
|
||||
limit:
|
||||
name: Limit
|
||||
description: Maximum number of assets to return (1-100).
|
||||
required: false
|
||||
default: 10
|
||||
@@ -24,23 +24,16 @@ get_assets:
|
||||
min: 1
|
||||
max: 100
|
||||
mode: slider
|
||||
filter:
|
||||
name: Filter
|
||||
description: Filter assets by type (none, favorite, or rating-based).
|
||||
favorite_only:
|
||||
name: Favorite Only
|
||||
description: Filter to show only favorite assets.
|
||||
required: false
|
||||
default: "none"
|
||||
default: false
|
||||
selector:
|
||||
select:
|
||||
options:
|
||||
- label: "None (no filtering)"
|
||||
value: "none"
|
||||
- label: "Favorites only"
|
||||
value: "favorite"
|
||||
- label: "By minimum rating"
|
||||
value: "rating"
|
||||
boolean:
|
||||
filter_min_rating:
|
||||
name: Minimum Rating
|
||||
description: Minimum rating for assets (1-5). Only used when filter is set to 'rating'.
|
||||
description: Minimum rating for assets (1-5). Set to filter by rating.
|
||||
required: false
|
||||
default: 1
|
||||
selector:
|
||||
@@ -48,20 +41,60 @@ get_assets:
|
||||
min: 1
|
||||
max: 5
|
||||
mode: slider
|
||||
order_by:
|
||||
name: Order By
|
||||
description: Field to sort assets by.
|
||||
required: false
|
||||
default: "date"
|
||||
selector:
|
||||
select:
|
||||
options:
|
||||
- label: "Date"
|
||||
value: "date"
|
||||
- label: "Rating"
|
||||
value: "rating"
|
||||
- label: "Name"
|
||||
value: "name"
|
||||
order:
|
||||
name: Order
|
||||
description: Sort order for assets by creation date.
|
||||
description: Sort direction.
|
||||
required: false
|
||||
default: "descending"
|
||||
selector:
|
||||
select:
|
||||
options:
|
||||
- label: "Ascending (oldest first)"
|
||||
- label: "Ascending"
|
||||
value: "ascending"
|
||||
- label: "Descending (newest first)"
|
||||
- label: "Descending"
|
||||
value: "descending"
|
||||
- label: "Random"
|
||||
value: "random"
|
||||
asset_type:
|
||||
name: Asset Type
|
||||
description: Filter assets by type (all, photo, or video).
|
||||
required: false
|
||||
default: "all"
|
||||
selector:
|
||||
select:
|
||||
options:
|
||||
- label: "All (no type filtering)"
|
||||
value: "all"
|
||||
- label: "Photos only"
|
||||
value: "photo"
|
||||
- label: "Videos only"
|
||||
value: "video"
|
||||
min_date:
|
||||
name: Minimum Date
|
||||
description: Filter assets created on or after this date (ISO 8601 format, e.g., 2024-01-01 or 2024-01-01T10:30:00).
|
||||
required: false
|
||||
selector:
|
||||
text:
|
||||
max_date:
|
||||
name: Maximum Date
|
||||
description: Filter assets created on or before this date (ISO 8601 format, e.g., 2024-12-31 or 2024-12-31T23:59:59).
|
||||
required: false
|
||||
selector:
|
||||
text:
|
||||
|
||||
send_telegram_notification:
|
||||
name: Send Telegram Notification
|
||||
|
||||
@@ -137,21 +137,37 @@
|
||||
"name": "Get Assets",
|
||||
"description": "Get assets from the targeted album with optional filtering and ordering.",
|
||||
"fields": {
|
||||
"count": {
|
||||
"name": "Count",
|
||||
"limit": {
|
||||
"name": "Limit",
|
||||
"description": "Maximum number of assets to return (1-100)."
|
||||
},
|
||||
"filter": {
|
||||
"name": "Filter",
|
||||
"description": "Filter assets by type (none, favorite, or rating-based)."
|
||||
"favorite_only": {
|
||||
"name": "Favorite Only",
|
||||
"description": "Filter to show only favorite assets."
|
||||
},
|
||||
"filter_min_rating": {
|
||||
"name": "Minimum Rating",
|
||||
"description": "Minimum rating for assets (1-5). Only used when filter is set to 'rating'."
|
||||
"description": "Minimum rating for assets (1-5)."
|
||||
},
|
||||
"order_by": {
|
||||
"name": "Order By",
|
||||
"description": "Field to sort assets by (date, rating, or name)."
|
||||
},
|
||||
"order": {
|
||||
"name": "Order",
|
||||
"description": "Sort order for assets by creation date."
|
||||
"description": "Sort direction (ascending, descending, or random)."
|
||||
},
|
||||
"asset_type": {
|
||||
"name": "Asset Type",
|
||||
"description": "Filter assets by type (all, photo, or video)."
|
||||
},
|
||||
"min_date": {
|
||||
"name": "Minimum Date",
|
||||
"description": "Filter assets created on or after this date (ISO 8601 format)."
|
||||
},
|
||||
"max_date": {
|
||||
"name": "Maximum Date",
|
||||
"description": "Filter assets created on or before this date (ISO 8601 format)."
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -137,21 +137,37 @@
|
||||
"name": "Получить файлы",
|
||||
"description": "Получить файлы из выбранного альбома с возможностью фильтрации и сортировки.",
|
||||
"fields": {
|
||||
"count": {
|
||||
"name": "Количество",
|
||||
"limit": {
|
||||
"name": "Лимит",
|
||||
"description": "Максимальное количество возвращаемых файлов (1-100)."
|
||||
},
|
||||
"filter": {
|
||||
"name": "Фильтр",
|
||||
"description": "Фильтровать файлы по типу (none - без фильтра, favorite - только избранные, rating - по рейтингу)."
|
||||
"favorite_only": {
|
||||
"name": "Только избранные",
|
||||
"description": "Фильтр для отображения только избранных файлов."
|
||||
},
|
||||
"filter_min_rating": {
|
||||
"name": "Минимальный рейтинг",
|
||||
"description": "Минимальный рейтинг для файлов (1-5). Используется только при filter='rating'."
|
||||
"description": "Минимальный рейтинг для файлов (1-5)."
|
||||
},
|
||||
"order_by": {
|
||||
"name": "Сортировать по",
|
||||
"description": "Поле для сортировки файлов (date - дата, rating - рейтинг, name - имя)."
|
||||
},
|
||||
"order": {
|
||||
"name": "Порядок",
|
||||
"description": "Порядок сортировки файлов по дате создания."
|
||||
"description": "Направление сортировки (ascending - по возрастанию, descending - по убыванию, random - случайный)."
|
||||
},
|
||||
"asset_type": {
|
||||
"name": "Тип файла",
|
||||
"description": "Фильтровать файлы по типу (all - все, photo - только фото, video - только видео)."
|
||||
},
|
||||
"min_date": {
|
||||
"name": "Минимальная дата",
|
||||
"description": "Фильтровать файлы, созданные в эту дату или после (формат ISO 8601)."
|
||||
},
|
||||
"max_date": {
|
||||
"name": "Максимальная дата",
|
||||
"description": "Фильтровать файлы, созданные в эту дату или до (формат ISO 8601)."
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user