Files
core/homeassistant/components/opnsense/__init__.py
Matthew Treinish 85dbf1ffad Add OPNSense device tracker (#26834)
* Add OPNSense device_tracker

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.

* Run black formatting locally to appease azure

* Apply suggestions from code review

Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch>

* Fix issues identified during code review

This commit updates several issues found in the module during code
review.

* Update homeassistant/components/opnsense/__init__.py

Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch>

* Update CODEOWNERS for recent changes

* Fix lint

* Apply suggestions from code review

Co-Authored-By: Martin Hjelmare <marhje52@kth.se>

* More fixes from review comments

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.

* Revert tests to previous format

* Add device detection to opnsense device_tracker test

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.

* Rerun black

* Fix lint

* Move import back to module level

* Return false on configuration errors in setup

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.

* Update tests

* Add pyopnsense to test requirements

* Rerun gen_requirements script

* Fix failing isort lint job step

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.

* Fix comment

* Update manifest.json

Co-authored-by: Fabian Affolter <mail@fabian-affolter.ch>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: Pascal Vizeli <pascal.vizeli@syshack.ch>
2020-01-29 16:20:43 +01:00

78 lines
2.3 KiB
Python

"""Support for OPNSense Routers."""
import logging
from pyopnsense import diagnostics
from pyopnsense.exceptions import APIException
import voluptuous as vol
from homeassistant.const import CONF_API_KEY, CONF_URL, CONF_VERIFY_SSL
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.discovery import load_platform
_LOGGER = logging.getLogger(__name__)
CONF_API_SECRET = "api_secret"
CONF_TRACKER_INTERFACE = "tracker_interfaces"
DOMAIN = "opnsense"
OPNSENSE_DATA = DOMAIN
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_URL): cv.url,
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_API_SECRET): cv.string,
vol.Optional(CONF_VERIFY_SSL, default=False): cv.boolean,
vol.Optional(CONF_TRACKER_INTERFACE, default=[]): vol.All(
cv.ensure_list, [cv.string]
),
}
)
},
extra=vol.ALLOW_EXTRA,
)
def setup(hass, config):
"""Set up the opnsense component."""
conf = config[DOMAIN]
url = conf[CONF_URL]
api_key = conf[CONF_API_KEY]
api_secret = conf[CONF_API_SECRET]
verify_ssl = conf[CONF_VERIFY_SSL]
tracker_interfaces = conf[CONF_TRACKER_INTERFACE]
interfaces_client = diagnostics.InterfaceClient(
api_key, api_secret, url, verify_ssl
)
try:
interfaces_client.get_arp()
except APIException:
_LOGGER.exception("Failure while connecting to OPNsense API endpoint.")
return False
if tracker_interfaces:
# Verify that specified tracker interfaces are valid
netinsight_client = diagnostics.NetworkInsightClient(
api_key, api_secret, url, verify_ssl
)
interfaces = list(netinsight_client.get_interfaces().values())
for interface in tracker_interfaces:
if interface not in interfaces:
_LOGGER.error(
"Specified OPNsense tracker interface %s is not found", interface
)
return False
hass.data[OPNSENSE_DATA] = {
"interfaces": interfaces_client,
CONF_TRACKER_INTERFACE: tracker_interfaces,
}
load_platform(hass, "device_tracker", DOMAIN, tracker_interfaces, config)
return True