core/homeassistant/components/device_tracker/netgear.py

93 lines
2.7 KiB
Python
Raw Normal View History

2015-05-11 16:05:58 +00:00
"""
2016-03-07 17:12:06 +00:00
Support for Netgear routers.
2015-05-11 16:05:58 +00:00
2015-10-13 18:54:48 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/device_tracker.netgear/
2015-05-11 16:05:58 +00:00
"""
import logging
import threading
2016-02-19 05:27:50 +00:00
from datetime import timedelta
from homeassistant.components.device_tracker import DOMAIN
2016-02-19 05:27:50 +00:00
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.util import Throttle
2016-03-07 17:12:06 +00:00
# Return cached results if last scan was less then this time ago.
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=5)
_LOGGER = logging.getLogger(__name__)
2016-01-24 17:43:06 +00:00
REQUIREMENTS = ['pynetgear==0.3.2']
def get_scanner(hass, config):
2016-03-07 20:18:53 +00:00
"""Validate the configuration and returns a Netgear scanner."""
2015-01-17 21:49:22 +00:00
info = config[DOMAIN]
2015-08-24 00:20:09 +00:00
host = info.get(CONF_HOST)
username = info.get(CONF_USERNAME)
password = info.get(CONF_PASSWORD)
if password is not None and host is None:
_LOGGER.warning('Found username or password but no host')
return None
2015-01-17 21:49:22 +00:00
2015-08-25 06:31:54 +00:00
scanner = NetgearDeviceScanner(host, username, password)
return scanner if scanner.success_init else None
class NetgearDeviceScanner(object):
2016-03-07 17:12:06 +00:00
"""Queries a Netgear wireless router using the SOAP-API."""
2016-03-07 20:18:53 +00:00
2015-01-17 21:49:22 +00:00
def __init__(self, host, username, password):
2016-03-07 20:18:53 +00:00
"""Initialize the scanner."""
import pynetgear
self.last_results = []
self.lock = threading.Lock()
2015-08-24 00:20:09 +00:00
if host is None:
self._api = pynetgear.Netgear()
elif username is None:
self._api = pynetgear.Netgear(password, host)
else:
self._api = pynetgear.Netgear(password, host, username)
_LOGGER.info("Logging in")
2015-08-24 00:20:09 +00:00
results = self._api.get_attached_devices()
self.success_init = results is not None
2015-01-17 21:49:22 +00:00
if self.success_init:
2015-08-24 00:20:09 +00:00
self.last_results = results
else:
_LOGGER.error("Failed to Login")
def scan_devices(self):
2016-03-07 20:18:53 +00:00
"""Scan for new devices and return a list with found device IDs."""
self._update_info()
2015-01-17 21:49:22 +00:00
return (device.mac for device in self.last_results)
def get_device_name(self, mac):
2016-03-07 20:18:53 +00:00
"""Return the name of the given device or None if we don't know."""
2015-01-17 21:49:22 +00:00
try:
return next(device.name for device in self.last_results
if device.mac == mac)
except StopIteration:
return None
@Throttle(MIN_TIME_BETWEEN_SCANS)
def _update_info(self):
2016-03-07 20:18:53 +00:00
"""Retrieve latest information from the Netgear router.
Returns boolean if scanning successful.
"""
if not self.success_init:
return
with self.lock:
_LOGGER.info("Scanning")
2015-01-17 21:49:22 +00:00
self.last_results = self._api.get_attached_devices() or []