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
|
|
|
"""
|
2014-11-12 05:39:17 +00:00
|
|
|
import logging
|
|
|
|
import threading
|
2016-02-19 05:27:50 +00:00
|
|
|
from datetime import timedelta
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2016-09-05 17:37:36 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-01-02 19:50:42 +00:00
|
|
|
from homeassistant.components.device_tracker import (
|
|
|
|
DOMAIN, PLATFORM_SCHEMA, DeviceScanner)
|
2016-09-05 17:37:36 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST, CONF_PASSWORD, CONF_USERNAME, CONF_PORT)
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.util import Throttle
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
REQUIREMENTS = ['pynetgear==0.3.3']
|
2014-11-12 05:39:17 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2017-04-30 05:04:49 +00:00
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=5)
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2016-09-05 17:37:36 +00:00
|
|
|
DEFAULT_HOST = 'routerlogin.net'
|
|
|
|
DEFAULT_USER = 'admin'
|
|
|
|
DEFAULT_PORT = 5000
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_USERNAME, default=DEFAULT_USER): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port
|
|
|
|
})
|
|
|
|
|
2014-11-12 05:39:17 +00:00
|
|
|
|
|
|
|
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)
|
2016-04-10 03:29:06 +00:00
|
|
|
port = info.get(CONF_PORT)
|
2015-08-24 00:20:09 +00:00
|
|
|
|
2016-04-10 03:29:06 +00:00
|
|
|
scanner = NetgearDeviceScanner(host, username, password, port)
|
2014-11-12 05:39:17 +00:00
|
|
|
|
|
|
|
return scanner if scanner.success_init else None
|
|
|
|
|
|
|
|
|
2017-01-02 19:50:42 +00:00
|
|
|
class NetgearDeviceScanner(DeviceScanner):
|
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
|
|
|
|
2016-04-10 03:29:06 +00:00
|
|
|
def __init__(self, host, username, password, port):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Initialize the scanner."""
|
2015-07-20 06:44:32 +00:00
|
|
|
import pynetgear
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2015-07-20 06:44:32 +00:00
|
|
|
self.last_results = []
|
2014-11-12 05:39:17 +00:00
|
|
|
self.lock = threading.Lock()
|
|
|
|
|
2016-09-05 17:37:36 +00:00
|
|
|
self._api = pynetgear.Netgear(password, host, username, port)
|
2015-08-24 00:20:09 +00:00
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.info("Logging in")
|
2014-11-12 05:39:17 +00:00
|
|
|
|
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
|
2014-11-12 05:39:17 +00:00
|
|
|
else:
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Failed to Login")
|
2014-11-12 05:39:17 +00:00
|
|
|
|
|
|
|
def scan_devices(self):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Scan for new devices and return a list with found device IDs."""
|
2014-11-12 05:39:17 +00:00
|
|
|
self._update_info()
|
|
|
|
|
2015-01-17 21:49:22 +00:00
|
|
|
return (device.mac for device in self.last_results)
|
2014-11-12 05:39:17 +00:00
|
|
|
|
|
|
|
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:
|
2014-11-12 05:39:17 +00:00
|
|
|
return None
|
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
@Throttle(MIN_TIME_BETWEEN_SCANS)
|
2014-11-12 05:39:17 +00:00
|
|
|
def _update_info(self):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Retrieve latest information from the Netgear router.
|
|
|
|
|
2015-09-07 17:19:11 +00:00
|
|
|
Returns boolean if scanning successful.
|
|
|
|
"""
|
2014-11-12 05:39:17 +00:00
|
|
|
if not self.success_init:
|
|
|
|
return
|
|
|
|
|
|
|
|
with self.lock:
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.info("Scanning")
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2016-03-15 04:12:42 +00:00
|
|
|
results = self._api.get_attached_devices()
|
|
|
|
|
|
|
|
if results is None:
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.warning("Error scanning devices")
|
2016-03-15 04:12:42 +00:00
|
|
|
|
|
|
|
self.last_results = results or []
|