Limit Google Photos media source to Home Assistant created albums (#126653)

This commit is contained in:
Allen Porter 2024-09-24 08:22:24 -07:00 committed by GitHub
parent 264927926e
commit 437bbe5c6e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 46 deletions

View file

@ -1,9 +1,9 @@
"""Media source for Google Photos."""
from dataclasses import dataclass
from enum import Enum, StrEnum
from enum import StrEnum
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.model import Album, MediaItem
@ -30,29 +30,6 @@ THUMBNAIL_SIZE = 256
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:
# config-entry-id
# config-entry-id/a/album-media-id
@ -194,18 +171,8 @@ class GooglePhotosMediaSource(MediaSource):
source = _build_account(entry, identifier)
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()
source.children.extend(
source.children = [
_build_album(
album.title,
PhotosIdentifier.album(
@ -215,7 +182,7 @@ class GooglePhotosMediaSource(MediaSource):
_cover_photo_url(album, THUMBNAIL_SIZE),
)
for album in albums
)
]
return source
if (
@ -224,16 +191,10 @@ class GooglePhotosMediaSource(MediaSource):
):
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] = []
try:
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)
except GooglePhotosApiError as err:

View file

@ -66,7 +66,6 @@ async def test_no_read_scopes(
@pytest.mark.parametrize(
("album_path", "expected_album_title"),
[
(f"{CONFIG_ENTRY_ID}/a/uploaded", "Uploaded Photos"),
(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.title == "Account Name"
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"),
]