"""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, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv from homeassistant.helpers.discovery import load_platform from homeassistant.helpers.typing import ConfigType from .const import ( CONF_API_SECRET, CONF_INTERFACE_CLIENT, CONF_TRACKER_INTERFACES, DOMAIN, OPNSENSE_DATA, ) _LOGGER = logging.getLogger(__name__) 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_INTERFACES, default=[]): vol.All( cv.ensure_list, [cv.string] ), } ) }, extra=vol.ALLOW_EXTRA, ) def setup(hass: HomeAssistant, config: ConfigType) -> bool: """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_INTERFACES] interfaces_client = diagnostics.InterfaceClient( api_key, api_secret, url, verify_ssl, timeout=20 ) 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, timeout=20 ) 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] = { CONF_INTERFACE_CLIENT: interfaces_client, CONF_TRACKER_INTERFACES: tracker_interfaces, } load_platform(hass, Platform.DEVICE_TRACKER, DOMAIN, tracker_interfaces, config) return True