Skip to content

Use Plex websocket payloads to reduce overhead#42332

Merged
frenck merged 19 commits intohome-assistant:devfrom
jjlawren:plex_use_websocket_payloads
Dec 2, 2020
Merged

Use Plex websocket payloads to reduce overhead#42332
frenck merged 19 commits intohome-assistant:devfrom
jjlawren:plex_use_websocket_payloads

Conversation

@jjlawren
Copy link
Copy Markdown
Contributor

@jjlawren jjlawren commented Oct 25, 2020

Proposed change

Plex websockets were previously only used to trigger updates from a central polling update method. Recently, #40773 started to pass the actual websocket payloads to the integration. This PR uses those payloads directly to avoid making unnecessary requests to the Plex server as most of the necessary information was already provided.

To make this possible, the active playback sessions are now maintained centrally with a dedicated class and pushed to the appropriate media_player and sensor entities as needed.

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)
  • 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
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@jjlawren
Copy link
Copy Markdown
Contributor Author

I think the session handling should be centralized more. Going to close this while I work on the refactoring.

@jjlawren jjlawren closed this Oct 26, 2020
@jjlawren jjlawren reopened this Oct 31, 2020
@jjlawren jjlawren marked this pull request as ready for review October 31, 2020 05:01
@jjlawren jjlawren force-pushed the plex_use_websocket_payloads branch from 64286c3 to 88c4393 Compare November 2, 2020 15:50
@jjlawren jjlawren force-pushed the plex_use_websocket_payloads branch from de1b27d to 40ec701 Compare November 30, 2020 04:40
@jjlawren
Copy link
Copy Markdown
Contributor Author

Coverage is low, but adding tests in the current framework would be overly complex. I'm reworking the tests to make this easier but that will need to be in a separate PR.

Comment thread homeassistant/components/plex/media_player.py Outdated
Comment thread homeassistant/components/plex/media_player.py Outdated
self._state = STATE_PAUSED
elif state == "stopped":
self.session = None
self.force_idle()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You are calling force_idle from a property setter, which then calls dispatcher_send. Please do not use a property setter if you have side effects.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was thinking dispatcher_send just sent an event to another listener instead of handling the scheduling itself. This can be moved out of the setter.

Copy link
Copy Markdown
Member

@balloob balloob Dec 2, 2020

Choose a reason for hiding this comment

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

Drop using X.setter. state is an inherited property and it is always confusing what is happening if other propertie are also updated because of it.

@jjlawren jjlawren force-pushed the plex_use_websocket_payloads branch from 6528317 to ae85cad Compare November 30, 2020 21:16
Copy link
Copy Markdown
Contributor

@shocklateboy92 shocklateboy92 left a comment

Choose a reason for hiding this comment

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

This looks good overall to my (unfamiliar) eyes. I've heard python's type checking system is quite rudimentary compared to languages I'm used to, but I think having some sort of annotation will help with following the code (especially to a newcomer).

It also looks like you're not doing any throttling of state updates this time round. Is that because plex only sends updates via the websocket on a periodic basis?

"""Return representation of the session."""
return f"<{self.session_key}:{self.sensor_title}>"

def update_media(self, media):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Adding types would really help with understanding this code for newcomers.
I believe media here is a plexapi.audio.Audio | plexapi.video.Video, right? (I don't know how to do union types in python, but that would be the syntax for typescript)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I figured it out. The syntax is:

Suggested change
def update_media(self, media):
def update_media(self, media: Union[Movie, Episode, Clip, Track]):

And of course, above you'd have to:

from plexapi.audio import Track
from plexapi.video import Clip, Episode, Movie

@jjlawren
Copy link
Copy Markdown
Contributor Author

jjlawren commented Dec 2, 2020

Agreed, some parts are difficult to follow and more hints would help.

Throttling is mostly handled by the plexwebsocket library, which only triggers on significant changes (new media/session, seek, state change).

Comment thread homeassistant/components/plex/media_player.py Outdated
Copy link
Copy Markdown
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

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

One last comment. After that looks good.

@frenck frenck merged commit f2f9355 into home-assistant:dev Dec 2, 2020
@jjlawren jjlawren deleted the plex_use_websocket_payloads branch December 2, 2020 18:05
Thomas55555 pushed a commit to Thomas55555/core that referenced this pull request Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants