diff --git a/custom_components/birdbuddy/sensor.py b/custom_components/birdbuddy/sensor.py index a55f367..c78e9d1 100644 --- a/custom_components/birdbuddy/sensor.py +++ b/custom_components/birdbuddy/sensor.py @@ -4,8 +4,9 @@ from collections.abc import Mapping from typing import Any +from birdbuddy.birds import Species from birdbuddy.feed import FeedNodeType -from birdbuddy.media import Collection, Media, is_media_expired +from birdbuddy.media import Media, is_media_expired from birdbuddy.sightings import PostcardSighting from homeassistant.components.sensor import ( @@ -115,7 +116,6 @@ class BirdBuddyRecentVisitorEntity(BirdBuddyMixin, RestoreSensor): _attr_name = "Recent Visitor" _attr_extra_state_attributes = {} - _latest_collection: Collection | None = None _latest_media: Media | None = None def __init__( @@ -186,7 +186,6 @@ async def _on_new_postcard(self, event: Event | None = None) -> None: # Else, select one that has a list of suggestions suggested = guessable[0].suggestions[0] self._attr_native_value = suggested.species.name - self._latest_collection = suggested LOGGER.info( "Reporting recent visitor from unrecognized suggestion: %s", suggested ) @@ -200,6 +199,7 @@ async def _on_new_postcard(self, event: Event | None = None) -> None: async def _update_latest_visitor(self) -> None: feed = await self.coordinator.client.feed() + items = feed.filter( of_type=[ FeedNodeType.SpeciesSighting, @@ -207,21 +207,26 @@ async def _update_latest_visitor(self) -> None: FeedNodeType.CollectedPostcard, ], ) + my_items = [ - item + item | {"media": next(iter(medias), None)} for item in items if item - and item.get("media") - and item.get("collection") # collection=None if it's been removed - and (self.feeder.id in item["media"].get("thumbnailUrl", "")) - and (item["collection"].get("species", None)) + and ( + medias := [ + m + for m in item.get("medias") + if m.get("__typename") == "MediaImage" + and self.feeder.id in m.get("thumbnailUrl", "") + ] + ) + and item.get("species", None) ] if latest := max(my_items, default=None, key=lambda x: x.created_at): self._latest_media = Media(latest["media"]) - self._latest_collection = Collection(latest["collection"]) - - self._attr_native_value = self._latest_collection.species.name + species = [Species(s).name for s in latest.get("species", [])] + self._attr_native_value = next(iter(species), None) self._attr_entity_picture = self._latest_media.thumbnail_url self.async_write_ha_state() @@ -240,10 +245,6 @@ def entity_picture(self) -> str | None: return picture self._latest_media = None - if self._latest_collection: - picture = self._latest_collection.cover_media.content_url - if not is_media_expired(picture): - return picture return None @property @@ -251,9 +252,6 @@ def native_value(self) -> str: if attr := super().native_value: # Postcard listener set the attribute directly, use it return attr - if self._latest_collection: - # If not set, get the most recent collection - return self._latest_collection.bird_name return None