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

Conversation

i-am-shodan
Copy link
Contributor

Catches foscam exceptions that otherwise pollute the log. Many of these exception can safely be ignored

Description:

The Python Foscam library correctly sets motion detection on my Foscam FI9800P but throws an exception while doing so. I've 'fixed' this by adding an exception handler/logging. This will then allow people to suppress the messages via an entry in their configuration.

The upstream maintainer of the Python Foscam library is AWOL and hasn't responded to the actual problem: quatanium/foscam-python-lib#22

In time Python Foscam should be replaced, it's pretty bad and doesn't support many devices.

Related issue (if applicable): fixes #9484

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.github.io#<home-assistant.github.io PR number goes here>

Example entry for configuration.yaml (if applicable):

Checklist:

  • [Y] The code change is tested and works locally.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • [n/a] Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • [n/a] New dependencies have been added to the REQUIREMENTS variable (example).
  • [n/a] New dependencies are only imported inside functions that use them (example).
  • [n/a] New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • [n/a] New files were added to .coveragerc.

If the code does not interact with devices:

  • [y] Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • [n/a] Tests have been added to verify that the new code works.

Catches foscam exceptions that otherwise pollute the log. Many of these exception can safely be ignored
@homeassistant
Copy link
Contributor

Hi @i-am-shodan,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

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

Choose a reason for hiding this comment

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

line too long (91 > 79 characters)

try:
ret, err = self._foscam_session.disable_motion_detection()
if ret == FOSCAM_COMM_ERROR:
_LOGGER.debug("Unable to communicate with Foscam Camera: %s", err)

Choose a reason for hiding this comment

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

line too long (82 > 79 characters)

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

Choose a reason for hiding this comment

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

line too long (91 > 79 characters)

try:
ret, err = self._foscam_session.enable_motion_detection()
if ret == FOSCAM_COMM_ERROR:
_LOGGER.debug("Unable to communicate with Foscam Camera: %s", err)

Choose a reason for hiding this comment

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

line too long (82 > 79 characters)

Fixed line length
@homeassistant
Copy link
Contributor

Hi @gavinlam,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

else:
try:
ret, err = self._foscam_session.enable_motion_detection()
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.

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

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.

@i-am-shodan
Copy link
Contributor Author

This whole module is a mess. The bigger question is why it raises an exception when the command actually works.

The goal of this patch is not to save this module from itself but to fix long standing bugs in it.

@balloob
Copy link
Member

balloob commented Jan 18, 2018

Oh I remember this lib now, it broke a release of ours. One day should indeed be replaced.

changed logging and catched only the TypeError effecting the library
Copy link
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

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

Ok to merge when tests pass

@gavinlam
Copy link

gavinlam commented Jan 19, 2018 via email

Removed var
@i-am-shodan
Copy link
Contributor Author

Can we ignore the shorter if statement. It will result in a long line that hound will throw up

@balloob
Copy link
Member

balloob commented Jan 21, 2018

Long line? This seems well below 80 chars:

            self._motion_status = ret == FOSCAM_COMM_ERROR

@i-am-shodan
Copy link
Contributor Author

If I was to implement the lint warning
************* Module homeassistant.components.camera.foscam
R: 80,12: The if statement can be replaced with 'var = bool(test)' (simplifiable-if-statement)
R: 93,12: The if statement can be replaced with 'var = bool(test)' (simplifiable-if-statement)

it would make the line over 80 chars.
Also 80 char line length is pretty old fashioned.

@tschmidty69
Copy link
Contributor

80 character length is pretty standard and not a required part of the pep standards but in any case that is what the project uses.

baloob gave you a perfect answer to replace 4 lines of code with one nice short line.

@fabaff fabaff merged commit 502ebd2 into home-assistant:dev Jan 25, 2018
@balloob balloob mentioned this pull request Jan 26, 2018
matemaciek pushed a commit to matemaciek/home-assistant that referenced this pull request Jan 27, 2018
* upstream/master: (465 commits)
  Update pyhomematic to 0.1.38 (home-assistant#11936)
  Implement Alexa temperature sensors (home-assistant#11930)
  Fixed rfxtrx binary_sensor KeyError on missing optional device_class (home-assistant#11925)
  Allow setting climate devices to AUTO mode via Google Assistant (home-assistant#11923)
  fixes home-assistant#11848 (home-assistant#11915)
  Add "write" service to system_log (home-assistant#11901)
  Update frontend to 20180126.0
  Version 0.62
  Allow separate command and state OIDs and payloads in SNMP switch (home-assistant#11075)
  Add ERC20 tokens to etherscan.io sensor (home-assistant#11916)
  Report scripts and groups as scenes to Alexa (home-assistant#11900)
  Minor fix to configuration validation and related log line. (home-assistant#11898)
  Multi-Room Support for Greenwave Reality (home-assistant#11364)
  Added Xeoma camera platform (home-assistant#11619)
  Improve foscam library exception support (home-assistant#11701)
  Iota wallet (home-assistant#11398)
  New venstar climate component (home-assistant#11639)
  Update python-wink version and multiple wink fixes/updates. (home-assistant#11833)
  Use API to discover Hue if no bridges specified (home-assistant#11909)
  Clarify emulated hue warning (home-assistant#11910)
  ...
@home-assistant home-assistant locked and limited conversation to collaborators May 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exception raised when enabling/disabling motion detection on Foscam. Functionality works correctly.
7 participants