Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions homeassistant/components/media_player/pjlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,31 @@ def projector(self):

def update(self):
"""Get the latest state from the device."""
from pypjlink.projector import ProjectorError
with self.projector() as projector:
pwstate = projector.get_power()
if pwstate == 'off':
self._pwstate = STATE_OFF
else:
self._pwstate = STATE_ON
self._muted = projector.get_mute()[1]
self._current_source = \
format_input_source(*projector.get_input())
try:
pwstate = projector.get_power()
if pwstate in ('on', 'warm-up'):
self._pwstate = STATE_ON
else:
self._pwstate = STATE_OFF
self._muted = projector.get_mute()[1]
self._current_source = \
format_input_source(*projector.get_input())
except KeyError as err:
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.

When would we get the KeyError? Is it if projector.get_mute() returns a dict instead of a list?

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.

No, the KeyError is also caused by get_power(). At least with my projector. When I turn on the projector and call the method all the time, I get the status warm-up for a while. Then at some point an answer takes about one second longer. This is the KeyError, which appears only once. Immediately afterwards, I get ProjectorError with message "unavailable time" for about 20-30 seconds. After this time, again I get for a short time the status warm-up and then on.

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.

The only thing I'd change right now is to swap if pwstate == 'on' or pwstate == 'warm-up': with if pwstate in {'on', 'warm-up'}:. Seems like my little experience in python isn't enough to solve the problem. Do you have another suggestion?

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.

Yeah, that clean up is good, but use a tuple:

if pwstate in ('on', 'warm-up'):

Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare Feb 1, 2019

Choose a reason for hiding this comment

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

The library shouldn't let KeyError slip through. That's a bug in the library. It could raise a library specific exception if needed.

But it's ok to fix here for now.

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.

If you find the cause, please try to fix this in the library and make a PR to them.

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.

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.

Thanks for your advice.

if str(err) == "'OK'":
self._pwstate = STATE_OFF
self._muted = False
self._current_source = None
else:
raise
except ProjectorError as err:
if str(err) == 'unavailable time':
self._pwstate = STATE_OFF
self._muted = False
self._current_source = None
else:
raise

@property
def name(self):
Expand Down