Limit Google Photos media source to Home Assistant created albums (#126653)
This commit is contained in:
parent
264927926e
commit
437bbe5c6e
2 changed files with 5 additions and 46 deletions
|
@ -1,9 +1,9 @@
|
||||||
"""Media source for Google Photos."""
|
"""Media source for Google Photos."""
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from enum import Enum, StrEnum
|
from enum import StrEnum
|
||||||
import logging
|
import logging
|
||||||
from typing import Any, Self, cast
|
from typing import Self, cast
|
||||||
|
|
||||||
from google_photos_library_api.exceptions import GooglePhotosApiError
|
from google_photos_library_api.exceptions import GooglePhotosApiError
|
||||||
from google_photos_library_api.model import Album, MediaItem
|
from google_photos_library_api.model import Album, MediaItem
|
||||||
|
@ -30,29 +30,6 @@ THUMBNAIL_SIZE = 256
|
||||||
LARGE_IMAGE_SIZE = 2160
|
LARGE_IMAGE_SIZE = 2160
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class SpecialAlbumDetails:
|
|
||||||
"""Details for a Special album."""
|
|
||||||
|
|
||||||
path: str
|
|
||||||
title: str
|
|
||||||
list_args: dict[str, Any]
|
|
||||||
|
|
||||||
|
|
||||||
class SpecialAlbum(Enum):
|
|
||||||
"""Special Album types."""
|
|
||||||
|
|
||||||
UPLOADED = SpecialAlbumDetails("uploaded", "Uploaded", {})
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def of(cls, path: str) -> Self | None:
|
|
||||||
"""Parse a PhotosIdentifierType by string value."""
|
|
||||||
for enum in cls:
|
|
||||||
if enum.value.path == path:
|
|
||||||
return enum
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
# The PhotosIdentifier can be in the following forms:
|
# The PhotosIdentifier can be in the following forms:
|
||||||
# config-entry-id
|
# config-entry-id
|
||||||
# config-entry-id/a/album-media-id
|
# config-entry-id/a/album-media-id
|
||||||
|
@ -194,18 +171,8 @@ class GooglePhotosMediaSource(MediaSource):
|
||||||
|
|
||||||
source = _build_account(entry, identifier)
|
source = _build_account(entry, identifier)
|
||||||
if identifier.id_type is None:
|
if identifier.id_type is None:
|
||||||
source.children = [
|
|
||||||
_build_album(
|
|
||||||
special_album.value.title,
|
|
||||||
PhotosIdentifier.album(
|
|
||||||
identifier.config_entry_id, special_album.value.path
|
|
||||||
),
|
|
||||||
)
|
|
||||||
for special_album in SpecialAlbum
|
|
||||||
]
|
|
||||||
|
|
||||||
albums = await coordinator.list_albums()
|
albums = await coordinator.list_albums()
|
||||||
source.children.extend(
|
source.children = [
|
||||||
_build_album(
|
_build_album(
|
||||||
album.title,
|
album.title,
|
||||||
PhotosIdentifier.album(
|
PhotosIdentifier.album(
|
||||||
|
@ -215,7 +182,7 @@ class GooglePhotosMediaSource(MediaSource):
|
||||||
_cover_photo_url(album, THUMBNAIL_SIZE),
|
_cover_photo_url(album, THUMBNAIL_SIZE),
|
||||||
)
|
)
|
||||||
for album in albums
|
for album in albums
|
||||||
)
|
]
|
||||||
return source
|
return source
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
@ -224,16 +191,10 @@ class GooglePhotosMediaSource(MediaSource):
|
||||||
):
|
):
|
||||||
raise BrowseError(f"Unsupported identifier: {identifier}")
|
raise BrowseError(f"Unsupported identifier: {identifier}")
|
||||||
|
|
||||||
list_args: dict[str, Any]
|
|
||||||
if special_album := SpecialAlbum.of(identifier.media_id):
|
|
||||||
list_args = special_album.value.list_args
|
|
||||||
else:
|
|
||||||
list_args = {"album_id": identifier.media_id}
|
|
||||||
|
|
||||||
media_items: list[MediaItem] = []
|
media_items: list[MediaItem] = []
|
||||||
try:
|
try:
|
||||||
async for media_item_result in await client.list_media_items(
|
async for media_item_result in await client.list_media_items(
|
||||||
**list_args, page_size=MEDIA_ITEMS_PAGE_SIZE
|
album_id=identifier.media_id, page_size=MEDIA_ITEMS_PAGE_SIZE
|
||||||
):
|
):
|
||||||
media_items.extend(media_item_result.media_items)
|
media_items.extend(media_item_result.media_items)
|
||||||
except GooglePhotosApiError as err:
|
except GooglePhotosApiError as err:
|
||||||
|
|
|
@ -66,7 +66,6 @@ async def test_no_read_scopes(
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("album_path", "expected_album_title"),
|
("album_path", "expected_album_title"),
|
||||||
[
|
[
|
||||||
(f"{CONFIG_ENTRY_ID}/a/uploaded", "Uploaded Photos"),
|
|
||||||
(f"{CONFIG_ENTRY_ID}/a/album-media-id-1", "Album title"),
|
(f"{CONFIG_ENTRY_ID}/a/album-media-id-1", "Album title"),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
@ -108,7 +107,6 @@ async def test_browse_albums(
|
||||||
assert browse.identifier == CONFIG_ENTRY_ID
|
assert browse.identifier == CONFIG_ENTRY_ID
|
||||||
assert browse.title == "Account Name"
|
assert browse.title == "Account Name"
|
||||||
assert [(child.identifier, child.title) for child in browse.children] == [
|
assert [(child.identifier, child.title) for child in browse.children] == [
|
||||||
(f"{CONFIG_ENTRY_ID}/a/uploaded", "Uploaded"),
|
|
||||||
(f"{CONFIG_ENTRY_ID}/a/album-media-id-1", "Album title"),
|
(f"{CONFIG_ENTRY_ID}/a/album-media-id-1", "Album title"),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue