Skip to content

Add OPNSense device tracker#26834

Merged
pvizeli merged 21 commits intohome-assistant:devfrom
mtreinish:opnsense-new
Jan 29, 2020
Merged

Add OPNSense device tracker#26834
pvizeli merged 21 commits intohome-assistant:devfrom
mtreinish:opnsense-new

Conversation

@mtreinish
Copy link
Copy Markdown
Contributor

@mtreinish mtreinish commented Sep 22, 2019

Description:

This commit adds a new component for using an OPNSense router as a
device tracker. It uses pyopnsense to query the api to look at the
arptable for a list of devices on the network.

Pull request with documentation for home-assistant.io (if applicable): home-assistant/home-assistant.io#10427

Example entry for configuration.yaml (if applicable):

opnsense:
   url: http://$router_ip/api
   api_key: api_key
   api_secret: api_secret

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist

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

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

  • The manifest file has all fields filled out correctly. Update and include derived files by running python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

Comment thread homeassistant/components/opnsense/__init__.py Outdated
Comment thread homeassistant/components/opnsense/__init__.py Outdated
"pyopnsense==0.2.0"
],
"dependencies": [],
"codeowners": []
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.

Please add yourself as code owner.

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.

Oh, I just assumed I didn't need to because I'm not listed as a codeowner on any of the components I've contributed in the past. Figured it was a requirement. Fixed in: 914fce5

Comment thread homeassistant/components/opnsense/__init__.py Outdated
Comment thread homeassistant/components/opnsense/device_tracker.py
Comment thread homeassistant/components/opnsense/device_tracker.py Outdated
Comment thread homeassistant/components/opnsense/__init__.py
Comment thread homeassistant/components/opnsense/__init__.py Outdated
Comment thread homeassistant/components/opnsense/__init__.py Outdated
Comment thread homeassistant/components/opnsense/__init__.py Outdated
Comment thread homeassistant/components/opnsense/__init__.py Outdated
Comment thread homeassistant/components/opnsense/__init__.py Outdated
Comment thread homeassistant/components/opnsense/device_tracker.py Outdated
Comment thread homeassistant/components/opnsense/device_tracker.py Outdated
Comment thread homeassistant/components/opnsense/device_tracker.py Outdated
Comment thread homeassistant/components/opnsense/device_tracker.py Outdated
Comment thread homeassistant/components/opnsense/device_tracker.py Outdated
Comment thread tests/components/opnsense/test_device_tracker.py Outdated
Comment thread tests/components/opnsense/test_device_tracker.py Outdated
Comment thread tests/components/opnsense/test_device_tracker.py Outdated
@mtreinish
Copy link
Copy Markdown
Contributor Author

Comment thread tests/components/opnsense/test_device_tracker.py Outdated
Comment thread homeassistant/components/opnsense/__init__.py Outdated
Comment thread tests/components/opnsense/test_device_tracker.py Outdated
)
try:
interfaces_client.get_arp()
except Exception: # pylint: disable=broad-except
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.

Please catch a specific exception raised by the library on credentials or connection failure.

It's good if the library raises different exceptions for different type of problems.

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.

It depends on the failure mode, the library will raise a pyopnsense.exceptions.APIException if the api returns an error, because of invalid credentials for example. But if there is a connection issue or something else that happens at a lower level like requests or urllib3 that will get raised verbatim. Which is why I used a broad catch here. It seemed like it would be better to catch any exception and log it rather than raise an unhandled exception from a lower layer.

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.

Preferably the library should catch RequestException and raise a library specific error.

@MartinHjelmare
Copy link
Copy Markdown
Member

If we need the library in the tests we need to add the library to the list in script/gen_requirements_all.py.

https://github.com/home-assistant/home-assistant/blob/bb45bdd8dd937a9fbbb21def1da93299498a9290/script/gen_requirements_all.py#L44

@frenck
Copy link
Copy Markdown
Member

frenck commented Nov 20, 2019

Hi @mtreinish!

Looks like this PR is pretty close to the finish 🥇
Are you planning to continue to work on this PR?

At this point, I would recommend rebasing the PR onto the latest dev branch, regenerate the requirements, and go from there.

Looking forward to it! 👍

@springstan
Copy link
Copy Markdown
Member

Hello @mtreinish the failed tests seem to be unrelated to this PR, that is why I would recommend you to rebase onto the latest dev branch again. This way the tests have to rerun and hopefully pass 😅 so this can be finally merged ✌

mtreinish and others added 14 commits December 22, 2019 15:20
This commit adds a new component for using an OPNSense router as a
device tracker. It uses pyopnsense to query the api to look at the
arptable for a list of devices on the network.
Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch>
Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch>
This commit updates several issues found in the module during code
review.
Co-Authored-By: Martin Hjelmare <marhje52@kth.se>
This commit fixes several issues from review comments, including
abandoning all the use of async code. This also completely reworks the
tests to be a bit clearer.
This commit adds actual device detection to the unit test for the setup
test. A fake api response is added to mocks for both api clients so that
they will register devices as expected and asserts are added for that.

The pyopnsense import is moved from the module level to be runtime in
the class. This was done because it was the only way to make the
MockDependency() call work as expected.
This commit updates the connection logic to return false if we're unable
to connect to the configured OPNsense API endpoint for any reason.
Previously we would not catch if an endpoint was incorrectly configured
until we first tried to use it. In this case it would raise an unhandled
exception. To handle this more gracefully this adds an api call early in
the setup and catches any exception raised by that so we can return
False to indicate the setup failed.
Since opening the PR originally yet another lint/style checker was added
which failed the PR in CI. This commit makes the adjustments to have
this pass the additional tool's checks.
frenck pushed a commit to mtreinish/home-assistant.io that referenced this pull request Jan 9, 2020
This commit adds documentation for the opnsense device tracker which is
being added in home-assistant/core#26834.
@pvizeli pvizeli merged commit 85dbf1f into home-assistant:dev Jan 29, 2020
@mtreinish mtreinish deleted the opnsense-new branch January 29, 2020 15:54
@lock lock Bot locked and limited conversation to collaborators Jan 30, 2020
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.

7 participants