Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve foscam library exception support #11701

Merged
merged 5 commits into from
Jan 25, 2018
Merged
Changes from 2 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: 22 additions & 10 deletions homeassistant/components/camera/foscam.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,32 @@ def motion_detection_enabled(self):

def enable_motion_detection(self):
"""Enable motion detection in camera."""
ret, err = self._foscam_session.enable_motion_detection()
if ret == FOSCAM_COMM_ERROR:
_LOGGER.debug("Unable to communicate with Foscam Camera: %s", err)
self._motion_status = True
else:
try:
ret, err = self._foscam_session.enable_motion_detection()
Copy link
Member

Choose a reason for hiding this comment

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

What's the use of returning an err code if it can also throw exceptions 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A question to the original author perhaps. This patch is a sticking plaster to fix a long standing bug.

if ret == FOSCAM_COMM_ERROR:
Copy link
Member

Choose a reason for hiding this comment

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

This code makes no sense. If it is an error, log that an error has occurred and then assume the command was successful because we're setting the attribute to True ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you loved this then you’ll love the constructor for this module. Who approved this in the first place???

Copy link
Member

Choose a reason for hiding this comment

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

So if this is supposed to be the "positive" result, we should not log "Unable to communicate"? I know it is not your code, but if you're touching it, let's at least just remove that log statement.

_LOGGER.debug("Unable to communicate "
"with Foscam Camera: %s", err)
self._motion_status = True
else:
self._motion_status = False
except:
Copy link
Member

Choose a reason for hiding this comment

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

Please specify the exceptions that Foscam can raise

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without having the broad range of Foscam devices to test against this isn’t really achievable to fix this bug. There are at least two cases noted in the upstream repo.

Copy link
Member

Choose a reason for hiding this comment

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

You should be able to see what the Python classes are of the exceptions that get raised

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like both the issues upstream result in TypeErrors. I'll add just those

_LOGGER.debug("An exception occured "
"when communicating with the Foscam Camera")
self._motion_status = False

def disable_motion_detection(self):
"""Disable motion detection."""
ret, err = self._foscam_session.disable_motion_detection()
if ret == FOSCAM_COMM_ERROR:
_LOGGER.debug("Unable to communicate with Foscam Camera: %s", err)
self._motion_status = True
else:
try:
ret, err = self._foscam_session.disable_motion_detection()
if ret == FOSCAM_COMM_ERROR:
_LOGGER.debug("Unable to communicate "
"with Foscam Camera: %s", err)
self._motion_status = True
else:
self._motion_status = False
except:
_LOGGER.debug("An exception occured "
"when communicating with the Foscam Camera")
self._motion_status = False

@property
Expand Down