Clean up netgear device tracker (#31861)

* Improve logging

* Clean up login

* Clean docstring

* Clean config access

* Remove default None for port

* Add not working guard

* Remove debug print
pull/31896/head
Martin Hjelmare 2020-02-16 23:27:19 +01:00 committed by GitHub
parent 1d5d56faf8
commit f53ae12bb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 41 deletions

View File

@ -1,5 +1,6 @@
"""Support for Netgear routers.""" """Support for Netgear routers."""
import logging import logging
from pprint import pformat
from pynetgear import Netgear from pynetgear import Netgear
import voluptuous as vol import voluptuous as vol
@ -30,7 +31,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
vol.Optional(CONF_SSL, default=False): cv.boolean, vol.Optional(CONF_SSL, default=False): cv.boolean,
vol.Optional(CONF_USERNAME, default=""): cv.string, vol.Optional(CONF_USERNAME, default=""): cv.string,
vol.Required(CONF_PASSWORD): cv.string, vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_PORT, default=None): vol.Any(None, cv.port), vol.Optional(CONF_PORT): cv.port,
vol.Optional(CONF_DEVICES, default=[]): vol.All(cv.ensure_list, [cv.string]), vol.Optional(CONF_DEVICES, default=[]): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_EXCLUDE, default=[]): vol.All(cv.ensure_list, [cv.string]), vol.Optional(CONF_EXCLUDE, default=[]): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_APS, default=[]): vol.All(cv.ensure_list, [cv.string]), vol.Optional(CONF_APS, default=[]): vol.All(cv.ensure_list, [cv.string]),
@ -41,55 +42,43 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
def get_scanner(hass, config): def get_scanner(hass, config):
"""Validate the configuration and returns a Netgear scanner.""" """Validate the configuration and returns a Netgear scanner."""
info = config[DOMAIN] info = config[DOMAIN]
host = info.get(CONF_HOST) host = info[CONF_HOST]
ssl = info.get(CONF_SSL) ssl = info[CONF_SSL]
username = info.get(CONF_USERNAME) username = info[CONF_USERNAME]
password = info.get(CONF_PASSWORD) password = info[CONF_PASSWORD]
port = info.get(CONF_PORT) port = info.get(CONF_PORT)
devices = info.get(CONF_DEVICES) devices = info[CONF_DEVICES]
excluded_devices = info.get(CONF_EXCLUDE) excluded_devices = info[CONF_EXCLUDE]
accesspoints = info.get(CONF_APS) accesspoints = info[CONF_APS]
scanner = NetgearDeviceScanner( api = Netgear(password, host, username, port, ssl)
host, ssl, username, password, port, devices, excluded_devices, accesspoints scanner = NetgearDeviceScanner(api, devices, excluded_devices, accesspoints)
)
return scanner if scanner.success_init else None _LOGGER.debug("Logging in")
results = scanner.get_attached_devices()
if results is not None:
scanner.last_results = results
else:
_LOGGER.error("Failed to Login")
return None
return scanner
class NetgearDeviceScanner(DeviceScanner): class NetgearDeviceScanner(DeviceScanner):
"""Queries a Netgear wireless router using the SOAP-API.""" """Queries a Netgear wireless router using the SOAP-API."""
def __init__( def __init__(
self, self, api, devices, excluded_devices, accesspoints,
host,
ssl,
username,
password,
port,
devices,
excluded_devices,
accesspoints,
): ):
"""Initialize the scanner.""" """Initialize the scanner."""
self.tracked_devices = devices self.tracked_devices = devices
self.excluded_devices = excluded_devices self.excluded_devices = excluded_devices
self.tracked_accesspoints = accesspoints self.tracked_accesspoints = accesspoints
self.last_results = [] self.last_results = []
self._api = Netgear(password, host, username, port, ssl) self._api = api
_LOGGER.info("Logging in")
results = self.get_attached_devices()
self.success_init = results is not None
if self.success_init:
self.last_results = results
else:
_LOGGER.error("Failed to Login")
def scan_devices(self): def scan_devices(self):
"""Scan for new devices and return a list with found device IDs.""" """Scan for new devices and return a list with found device IDs."""
@ -153,21 +142,20 @@ class NetgearDeviceScanner(DeviceScanner):
Returns boolean if scanning successful. Returns boolean if scanning successful.
""" """
if not self.success_init: _LOGGER.debug("Scanning")
return
_LOGGER.info("Scanning")
results = self.get_attached_devices() results = self.get_attached_devices()
if _LOGGER.isEnabledFor(logging.DEBUG):
_LOGGER.debug("Scan result: \n%s", pformat(results))
if results is None: if results is None:
_LOGGER.warning("Error scanning devices") _LOGGER.warning("Error scanning devices")
self.last_results = results or [] self.last_results = results or []
def get_attached_devices(self): def get_attached_devices(self):
""" """List attached devices with pynetgear.
List attached devices with pynetgear.
The v2 method takes more time and is more heavy on the router The v2 method takes more time and is more heavy on the router
so we only use it if we need connected AP info. so we only use it if we need connected AP info.