Fixed roku exception when device is powered off or looses connection (#2173)

pull/2182/head
Brent 2016-05-29 16:24:06 -05:00 committed by Paulus Schoutsen
parent 4b0df51b40
commit e886303f08
1 changed files with 38 additions and 20 deletions

View File

@ -66,13 +66,18 @@ class RokuDevice(MediaPlayerDevice):
def update(self): def update(self):
"""Retrieve latest state.""" """Retrieve latest state."""
self.roku_name = "roku_" + self.roku.device_info.sernum import requests.exceptions
self.ip_address = self.roku.host
self.channels = self.get_source_list()
if self.roku.current_app is not None: try:
self.current_app = self.roku.current_app self.roku_name = "roku_" + self.roku.device_info.sernum
else: self.ip_address = self.roku.host
self.channels = self.get_source_list()
if self.roku.current_app is not None:
self.current_app = self.roku.current_app
else:
self.current_app = None
except requests.exceptions.ConnectionError:
self.current_app = None self.current_app = None
def get_source_list(self): def get_source_list(self):
@ -92,6 +97,9 @@ class RokuDevice(MediaPlayerDevice):
@property @property
def state(self): def state(self):
"""Return the state of the device.""" """Return the state of the device."""
if self.current_app is None:
return STATE_UNKNOWN
if self.current_app.name in ["Power Saver", "Default screensaver"]: if self.current_app.name in ["Power Saver", "Default screensaver"]:
return STATE_IDLE return STATE_IDLE
elif self.current_app.name == "Roku": elif self.current_app.name == "Roku":
@ -137,17 +145,20 @@ class RokuDevice(MediaPlayerDevice):
@property @property
def app_name(self): def app_name(self):
"""Name of the current running app.""" """Name of the current running app."""
return self.current_app.name if self.current_app is not None:
return self.current_app.name
@property @property
def app_id(self): def app_id(self):
"""Return the ID of the current running app.""" """Return the ID of the current running app."""
return self.current_app.id if self.current_app is not None:
return self.current_app.id
@property @property
def source(self): def source(self):
"""Return the current input source.""" """Return the current input source."""
return self.current_app.name if self.current_app is not None:
return self.current_app.name
@property @property
def source_list(self): def source_list(self):
@ -156,32 +167,39 @@ class RokuDevice(MediaPlayerDevice):
def media_play_pause(self): def media_play_pause(self):
"""Send play/pause command.""" """Send play/pause command."""
self.roku.play() if self.current_app is not None:
self.roku.play()
def media_previous_track(self): def media_previous_track(self):
"""Send previous track command.""" """Send previous track command."""
self.roku.reverse() if self.current_app is not None:
self.roku.reverse()
def media_next_track(self): def media_next_track(self):
"""Send next track command.""" """Send next track command."""
self.roku.forward() if self.current_app is not None:
self.roku.forward()
def mute_volume(self, mute): def mute_volume(self, mute):
"""Mute the volume.""" """Mute the volume."""
self.roku.volume_mute() if self.current_app is not None:
self.roku.volume_mute()
def volume_up(self): def volume_up(self):
"""Volume up media player.""" """Volume up media player."""
self.roku.volume_up() if self.current_app is not None:
self.roku.volume_up()
def volume_down(self): def volume_down(self):
"""Volume down media player.""" """Volume down media player."""
self.roku.volume_down() if self.current_app is not None:
self.roku.volume_down()
def select_source(self, source): def select_source(self, source):
"""Select input source.""" """Select input source."""
if source == "Home": if self.current_app is not None:
self.roku.home() if source == "Home":
else: self.roku.home()
channel = self.roku[source] else:
channel.launch() channel = self.roku[source]
channel.launch()