Skip to content

Commit

Permalink
Adjust species lookup for CollectedPostcard
Browse files Browse the repository at this point in the history
  • Loading branch information
jhansche committed Apr 6, 2024
1 parent b0bb4f5 commit a4fae3b
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions custom_components/birdbuddy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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__(
Expand Down Expand Up @@ -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
)
Expand All @@ -200,28 +199,34 @@ 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,
FeedNodeType.SpeciesUnlocked,
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()

Expand All @@ -240,20 +245,13 @@ 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
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


Expand Down

0 comments on commit a4fae3b

Please sign in to comment.