Skip to content

Commit

Permalink
Merge pull request #52 from Ivieb/secondary-media-metadata
Browse files Browse the repository at this point in the history
added secondary media metadata to entity and moved update to get media data
  • Loading branch information
Daanoz authored Jun 2, 2024
2 parents 7443a81 + e2704f7 commit 4596ebb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
20 changes: 20 additions & 0 deletions custom_components/google_photos/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def _handle_coordinator_update(self) -> None:
CONF_WRITEMETADATA, WRITEMETADATA_DEFAULT_OPTION
)
media = self.coordinator.current_media
media_secondary = self.coordinator.current_secondary_media
if write_metadata:
self._attr_extra_state_attributes["media_filename"] = (
media.get("filename") or ""
Expand All @@ -128,6 +129,25 @@ def _handle_coordinator_update(self) -> None:
self._attr_extra_state_attributes["media_url"] = (
media.get("productUrl") or {}
)
if media_secondary is not None:
self._attr_extra_state_attributes["secondary_media_filename"] = (
media_secondary.get("filename") or ""
)
self._attr_extra_state_attributes["secondary_media_metadata"] = (
media_secondary.get("mediaMetadata") or {}
)
self._attr_extra_state_attributes["secondary_media_contributor_info"] = (
media_secondary.get("contributorInfo") or {}
)
self._attr_extra_state_attributes["secondary_media_url"] = (
media_secondary.get("productUrl") or {}
)
else:
self._attr_extra_state_attributes.pop("secondary_media_filename", None)
self._attr_extra_state_attributes.pop("secondary_media_metadata", None)
self._attr_extra_state_attributes.pop("secondary_media_contributor_info", None)
self._attr_extra_state_attributes.pop("secondary_media_url", None)

self.async_write_ha_state()

async def next_media(self, mode=None):
Expand Down
12 changes: 11 additions & 1 deletion custom_components/google_photos/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ def current_media(self) -> MediaItem | None:
if self.current_media_primary is None:
return None
return self.current_media_primary.media

@property
def current_secondary_media(self) -> MediaItem | None:
"""Get current secondary media item"""
if self.current_media_secondary is None:
return None
return self.current_media_secondary.media

def get_device_info(self) -> DeviceInfo:
"""Fetches device info for coordinator instance"""
Expand Down Expand Up @@ -261,12 +268,15 @@ async def get_media_data(self, width: int | None = None, height: int | None = No
if self.crop_mode == SETTING_CROP_MODE_COMBINED:
result = await self._get_combined_media_data(width, height)
if result is not None:
self.async_update_listeners()
self.current_media_cache[size_str] = result
return self.current_media_cache[size_str]


self.current_media_secondary = None
self.current_media_cache[size_str] = await self.current_media_primary.download(
size_str
)
self.async_update_listeners()
return self.current_media_cache[size_str]

async def _get_combined_media_data(self, width: int, height: int):
Expand Down

0 comments on commit 4596ebb

Please sign in to comment.