Skip to content

Commit

Permalink
Fix progress for Plex media_players (#22224)
Browse files Browse the repository at this point in the history
* Return current position and last updated timestamp needed by UI

* Add throttling to position updating

* Simplify logic, don't clear position when refreshing

* Use seconds

* Update homeassistant/components/plex/media_player.py

Co-Authored-By: jjlawren <jjlawren@users.noreply.github.com>

* Add throttling to position updating

* Simplify logic, don't clear position when refreshing
  • Loading branch information
jjlawren authored and bachya committed Mar 22, 2019
1 parent 1ddc65a commit ce55020
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions homeassistant/components/plex/media_player.py
Expand Up @@ -322,6 +322,7 @@ def __init__(self, config, device, session, plex_sessions,
self._media_image_url = None
self._media_title = None
self._media_position = None
self._media_position_updated_at = None
# Music
self._media_album_artist = None
self._media_album_name = None
Expand Down Expand Up @@ -361,7 +362,6 @@ def _clear_media_details(self):
self._media_duration = None
self._media_image_url = None
self._media_title = None
self._media_position = None
# Music
self._media_album_artist = None
self._media_album_name = None
Expand Down Expand Up @@ -417,7 +417,21 @@ def refresh(self, device, session):
self._make = self._player.device
else:
self._is_player_available = False
self._media_position = self._session.viewOffset

# Calculate throttled position for proper progress display.
position = int(self._session.viewOffset / 1000)
now = dt_util.utcnow()
if self._media_position is not None:
pos_diff = (position - self._media_position)
time_diff = now - self._media_position_updated_at
if (pos_diff != 0 and
abs(time_diff.total_seconds() - pos_diff) > 5):
self._media_position_updated_at = now
self._media_position = position
else:
self._media_position_updated_at = now
self._media_position = position

self._media_content_id = self._session.ratingKey
self._media_content_rating = getattr(
self._session, 'contentRating', None)
Expand All @@ -426,7 +440,7 @@ def refresh(self, device, session):

if self._is_player_active and self._session is not None:
self._session_type = self._session.type
self._media_duration = self._session.duration
self._media_duration = int(self._session.duration / 1000)
# title (movie name, tv episode name, music song name)
self._media_title = self._session.title
# media type
Expand Down Expand Up @@ -621,6 +635,16 @@ def media_duration(self):
"""Return the duration of current playing media in seconds."""
return self._media_duration

@property
def media_position(self):
"""Return the duration of current playing media in seconds."""
return self._media_position

@property
def media_position_updated_at(self):
"""When was the position of the current playing media valid."""
return self._media_position_updated_at

@property
def media_image_url(self):
"""Return the image URL of current playing media."""
Expand Down

0 comments on commit ce55020

Please sign in to comment.