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

Conversation

joostlek
Copy link
Member

Proposed change

Apparently LastFM can return an error while updating a sensor (so at the first verify where we check if the user exists (get_playcount) until the last playcount call). It will throw an exception in the log that it couldn't update the sensor.

In this change I am just throwing everything in the try except to handle this behaviour.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@joostlek
Copy link
Member Author

I am a bit in doubt on how to solve the unavailable backend, as we do 5 calls to update the state for one sensor. If one of those calls now fail we set it to unavailable. Is this a good way to handle unavailable data? Are there better ways?

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

@ScottG489
Copy link
Contributor

I know it's still a draft, but don't forget tests :)

@gjohansson-ST
Copy link
Member

I am a bit in doubt on how to solve the unavailable backend, as we do 5 calls to update the state for one sensor. If one of those calls now fail we set it to unavailable. Is this a good way to handle unavailable data? Are there better ways?

Perhaps it would be good to only set available based on get_playcount() and the others are silently set to None if they don't return proper data or throw and exception. As the first call would verify the api is working.
Maybe two different try-blocks.

@gjohansson-ST
Copy link
Member

and the others are silently set to None

Better leaving them at previous value on second thought.

@joostlek
Copy link
Member Author

Hmm, for some reason the tests doesn't cover the except. (Altho the sensor.py doesn't have SCAN_INTERVAL, so 15 minutes would be too high, but tried locally and that didn't matter). Any ideas what I can try?

@joostlek joostlek marked this pull request as ready for review June 19, 2023 09:19
Comment on lines 113 to 131
try:
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)
self._attr_extra_state_attributes = {
ATTR_LAST_PLAYED: last_played,
ATTR_PLAY_COUNT: play_count,
ATTR_TOP_PLAYED: top_played,
}
except PyLastError:
return
Copy link
Member

@frenck frenck Jun 19, 2023

Choose a reason for hiding this comment

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

This feels like a large try scope? As in, if get_top_tracks fails, a user is left with a partial update? While get_recent_tracks isn't even tried anymore?

The scope of the try should be limited to a single call IMHO; or handle the unavailable state (in which all values become unavailable).

Copy link
Member Author

Choose a reason for hiding this comment

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

When I look at my logs this happened like 20 times in the last 4 days. What should the preferred option be? In my opinion, the best way would be to split the data up into several sensors so we can put it unavailable independently, but that'd be a breaking change. So would putting everything in one try except be the best solution for now?

Copy link
Member

Choose a reason for hiding this comment

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

I think I would implement the available state right now, and make the sensor unavailable. As a follow-up PRs:

  • Implement a data coordinator
  • Add sensor descriptions
  • Add separate sensors for the attributes
  • Keep the attributes for the entity around for a couple of release cycles before removing them from the old sensor entity.

@home-assistant home-assistant bot marked this pull request as draft June 19, 2023 09:49
@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@joostlek joostlek marked this pull request as ready for review June 19, 2023 12:54
@home-assistant home-assistant bot requested a review from frenck June 19, 2023 12:54
@home-assistant home-assistant bot marked this pull request as draft June 19, 2023 18:25
@joostlek joostlek marked this pull request as ready for review June 19, 2023 19:10
@frenck frenck added the smash Indicator this PR is close to finish for merging or closing label Jun 21, 2023
gjohansson-ST
gjohansson-ST previously approved these changes Jun 22, 2023
Copy link
Member

@gjohansson-ST gjohansson-ST left a comment

Choose a reason for hiding this comment

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

LGTM 👍 Thanks

@gjohansson-ST gjohansson-ST added this to the 2023.6.3 milestone Jun 22, 2023
@gjohansson-ST gjohansson-ST dismissed their stale review June 22, 2023 21:39

Tests are failing

@gjohansson-ST
Copy link
Member

@joostlek tests are failing. Can you take a look?

Copy link
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

Thanks, @joostlek 👍

../Frenck

@frenck frenck merged commit 8fd930b into home-assistant:dev Jun 23, 2023
23 checks passed
@joostlek joostlek deleted the lastfm_unavailable branch June 23, 2023 13:37
frenck pushed a commit that referenced this pull request Jun 23, 2023
@frenck frenck mentioned this pull request Jun 23, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jun 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bugfix by-code-owner cherry-picked cla-signed integration: lastfm Quality Scale: No score small-pr PRs with less than 30 lines. smash Indicator this PR is close to finish for merging or closing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants