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

Handle LastFM unavailable #94456

Merged
merged 21 commits into from
Jun 23, 2023
Merged
Changes from 1 commit
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
39 changes: 19 additions & 20 deletions homeassistant/components/lastfm/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import hashlib

from pylast import LastFMNetwork, Track, User, WSError
from pylast import LastFMNetwork, Track, User, WSError, PyLastError

Check warning on line 6 in homeassistant/components/lastfm/sensor.py

View workflow job for this annotation

GitHub Actions / Check pylint

W0611: Unused WSError imported from pylast (unused-import)
import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
Expand Down Expand Up @@ -105,25 +105,24 @@
def update(self) -> None:
"""Update device state."""
try:
self._user.get_playcount()
except WSError as exc:
self._attr_entity_picture = self._user.get_image()
if now_playing := self._user.get_now_playing():
self._attr_native_value = format_track(now_playing)
else:
self._attr_native_value = STATE_NOT_SCROBBLING
top_played = None
if top_tracks := self._user.get_top_tracks(limit=1):
top_played = format_track(top_tracks[0].item)
last_played = None
if last_tracks := self._user.get_recent_tracks(limit=1):
last_played = format_track(last_tracks[0].track)
play_count = self._user.get_playcount()
self._attr_extra_state_attributes = {
gjohansson-ST marked this conversation as resolved.
Show resolved Hide resolved
ATTR_LAST_PLAYED: last_played,
ATTR_PLAY_COUNT: play_count,
ATTR_TOP_PLAYED: top_played,
}
except PyLastError as exc:
self._attr_available = False
LOGGER.error("Failed to load LastFM user `%s`: %r", self._user.name, exc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could rethrow this with some kind of HA core error? I think it would better indicate to HA that there's a problem and I think it'll do retries for us?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously we used get_playcount to verify a user still exists. So if a user got deleted it would become unavailable. But now I want to handle what happens if LastFM is having connection issues. So throwing a Home Assistant error wouldn't be fitting as it's an error originating from LastFM

return
self._attr_entity_picture = self._user.get_image()
if now_playing := self._user.get_now_playing():
self._attr_native_value = format_track(now_playing)
else:
self._attr_native_value = STATE_NOT_SCROBBLING
top_played = None
if top_tracks := self._user.get_top_tracks(limit=1):
top_played = format_track(top_tracks[0].item)
last_played = None
if last_tracks := self._user.get_recent_tracks(limit=1):
last_played = format_track(last_tracks[0].track)
play_count = self._user.get_playcount()
self._attr_extra_state_attributes = {
ATTR_LAST_PLAYED: last_played,
ATTR_PLAY_COUNT: play_count,
ATTR_TOP_PLAYED: top_played,
}