Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh media URL when it expired #23

Merged
merged 1 commit into from
Jan 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions custom_components/birdbuddy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Any

from birdbuddy.birds import PostcardSighting
from birdbuddy.media import is_media_expired

from homeassistant.components.sensor import (
RestoreSensor,
Expand Down Expand Up @@ -179,20 +180,23 @@ async def _update_latest_visitor(self) -> None:
return

latest_media = max(
media.values(),
(m for m in media.values() if not m.is_video),
default=None,
key=lambda x: x.created_at,
)
if latest_media:
if latest_media and not latest_media.is_expired:
self._attr_entity_picture = latest_media.content_url
# self._attr_extra_state_attributes["last_visit"] = collection.last_visit
# self._attr_extra_state_attributes["total_visits"] = collection.total_visits

@property
def entity_picture(self) -> str | None:
if picture := super().entity_picture:
# Postcard listener will set the attribute directly
return picture
if not is_media_expired(picture):
# Postcard listener will set the attribute directly
return picture
# Media URL is expired, try to refresh it
self._attr_entity_picture = None
if collections := self.coordinator.client.collections:
# If not set by a postcard, we should get the most recent collection
# but also get the most recent media within that collection.
Expand Down