Add media position support and trailer type to Emby (#4792)

* Add media position support and trailer type to Emby

* Adjustments to mitigate TypeError

* Simplify media_position property

* Update handling when data isn't available

* Update emby.py
This commit is contained in:
John Mihalic 2016-12-12 00:43:53 -05:00 committed by Paulus Schoutsen
parent 2a7fa5afc3
commit d7ccf07922
2 changed files with 37 additions and 6 deletions

View File

@ -20,12 +20,15 @@ from homeassistant.const import (
STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING, STATE_UNKNOWN) STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING, STATE_UNKNOWN)
from homeassistant.helpers.event import (track_utc_time_change) from homeassistant.helpers.event import (track_utc_time_change)
from homeassistant.util import Throttle from homeassistant.util import Throttle
import homeassistant.util.dt as dt_util
REQUIREMENTS = ['pyemby==0.1'] REQUIREMENTS = ['pyemby==0.2']
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10) MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1) MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1)
MEDIA_TYPE_TRAILER = 'trailer'
DEFAULT_PORT = 8096 DEFAULT_PORT = 8096
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -119,6 +122,8 @@ class EmbyClient(MediaPlayerDevice):
self.update_sessions = update_sessions self.update_sessions = update_sessions
self.client = client self.client = client
self.set_device(device) self.set_device(device)
self.media_status_last_position = None
self.media_status_received = None
def set_device(self, device): def set_device(self, device):
"""Set the device property.""" """Set the device property."""
@ -178,6 +183,17 @@ class EmbyClient(MediaPlayerDevice):
"""Get the latest details.""" """Get the latest details."""
self.update_devices(no_throttle=True) self.update_devices(no_throttle=True)
self.update_sessions(no_throttle=True) self.update_sessions(no_throttle=True)
# Check if we should update progress
try:
position = self.session['PlayState']['PositionTicks']
except (KeyError, TypeError):
self.media_status_last_position = None
self.media_status_received = None
else:
position = int(position) / 10000000
if position != self.media_status_last_position:
self.media_status_last_position = position
self.media_status_received = dt_util.utcnow()
def play_percent(self): def play_percent(self):
"""Return current media percent complete.""" """Return current media percent complete."""
@ -220,6 +236,8 @@ class EmbyClient(MediaPlayerDevice):
return MEDIA_TYPE_TVSHOW return MEDIA_TYPE_TVSHOW
elif media_type == 'Movie': elif media_type == 'Movie':
return MEDIA_TYPE_VIDEO return MEDIA_TYPE_VIDEO
elif media_type == 'Trailer':
return MEDIA_TYPE_TRAILER
return None return None
except KeyError: except KeyError:
return None return None
@ -233,19 +251,32 @@ class EmbyClient(MediaPlayerDevice):
except KeyError: except KeyError:
return None return None
@property
def media_position(self):
"""Position of current playing media in seconds."""
return self.media_status_last_position
@property
def media_position_updated_at(self):
"""
When was the position of the current playing media valid.
Returns value from homeassistant.util.dt.utcnow().
"""
return self.media_status_received
@property @property
def media_image_url(self): def media_image_url(self):
"""Image url of current playing media.""" """Image url of current playing media."""
if self.now_playing_item is not None: if self.now_playing_item is not None:
try: try:
return self.client.get_image( return self.client.get_image(
self.now_playing_item['ThumbItemId'], 'Thumb', self.now_playing_item['ThumbItemId'], 'Thumb', 0)
self.play_percent())
except KeyError: except KeyError:
try: try:
return self.client.get_image( return self.client.get_image(
self.now_playing_item['PrimaryImageItemId'], 'Primary', self.now_playing_item[
self.play_percent()) 'PrimaryImageItemId'], 'Primary', 0)
except KeyError: except KeyError:
return None return None

View File

@ -390,7 +390,7 @@ pycmus==0.1.0
pydispatcher==2.0.5 pydispatcher==2.0.5
# homeassistant.components.media_player.emby # homeassistant.components.media_player.emby
pyemby==0.1 pyemby==0.2
# homeassistant.components.envisalink # homeassistant.components.envisalink
pyenvisalink==1.9 pyenvisalink==1.9