2015-05-11 16:05:58 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.device_tracker.netgear
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Device tracker platform that supports scanning a Netgear router for device
|
|
|
|
presence.
|
|
|
|
|
|
|
|
Configuration:
|
|
|
|
|
|
|
|
To use the Netgear tracker you will need to add something like the following
|
|
|
|
to your config/configuration.yaml
|
|
|
|
|
|
|
|
device_tracker:
|
|
|
|
platform: netgear
|
|
|
|
host: YOUR_ROUTER_IP
|
|
|
|
username: YOUR_ADMIN_USERNAME
|
|
|
|
password: YOUR_ADMIN_PASSWORD
|
|
|
|
|
|
|
|
Variables:
|
|
|
|
|
|
|
|
host
|
|
|
|
*Required
|
|
|
|
The IP address of your router, e.g. 192.168.1.1.
|
|
|
|
|
|
|
|
username
|
|
|
|
*Required
|
|
|
|
The username of an user with administrative privileges, usually 'admin'.
|
|
|
|
|
|
|
|
password
|
|
|
|
*Required
|
|
|
|
The password for your given admin account.
|
|
|
|
"""
|
2014-11-12 05:39:17 +00:00
|
|
|
import logging
|
2014-12-04 09:14:27 +00:00
|
|
|
from datetime import timedelta
|
2014-11-12 05:39:17 +00:00
|
|
|
import threading
|
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_USERNAME, CONF_PASSWORD
|
|
|
|
from homeassistant.helpers import validate_config
|
|
|
|
from homeassistant.util import Throttle
|
2014-11-12 05:39:17 +00:00
|
|
|
from homeassistant.components.device_tracker import DOMAIN
|
|
|
|
|
|
|
|
# Return cached results if last scan was less then this time ago
|
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=5)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-07-20 06:44:32 +00:00
|
|
|
REQUIREMENTS = ['pynetgear>=0.1']
|
2014-11-12 05:39:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_scanner(hass, config):
|
|
|
|
""" Validates config and returns a Netgear scanner. """
|
2014-12-07 07:57:02 +00:00
|
|
|
if not validate_config(config,
|
|
|
|
{DOMAIN: [CONF_HOST, CONF_USERNAME, CONF_PASSWORD]},
|
|
|
|
_LOGGER):
|
2014-11-12 05:39:17 +00:00
|
|
|
return None
|
|
|
|
|
2015-01-17 21:49:22 +00:00
|
|
|
info = config[DOMAIN]
|
|
|
|
|
|
|
|
scanner = NetgearDeviceScanner(
|
|
|
|
info[CONF_HOST], info[CONF_USERNAME], info[CONF_PASSWORD])
|
2014-11-12 05:39:17 +00:00
|
|
|
|
|
|
|
return scanner if scanner.success_init else None
|
|
|
|
|
|
|
|
|
|
|
|
class NetgearDeviceScanner(object):
|
2015-05-11 16:05:58 +00:00
|
|
|
""" This class queries a Netgear wireless router using the SOAP-API. """
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2015-01-17 21:49:22 +00:00
|
|
|
def __init__(self, host, username, password):
|
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._api = pynetgear.Netgear(host, username, password)
|
|
|
|
self.lock = threading.Lock()
|
|
|
|
|
|
|
|
_LOGGER.info("Logging in")
|
|
|
|
|
2015-01-17 21:49:22 +00:00
|
|
|
self.success_init = self._api.login()
|
|
|
|
|
|
|
|
if self.success_init:
|
|
|
|
self._update_info()
|
2014-11-12 05:39:17 +00:00
|
|
|
else:
|
|
|
|
_LOGGER.error("Failed to Login")
|
|
|
|
|
|
|
|
def scan_devices(self):
|
|
|
|
""" Scans for new devices and return a
|
|
|
|
list containing found device ids. """
|
|
|
|
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):
|
|
|
|
""" Returns 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):
|
|
|
|
""" Retrieves latest information from the Netgear router.
|
|
|
|
Returns boolean if scanning successful. """
|
|
|
|
if not self.success_init:
|
|
|
|
return
|
|
|
|
|
|
|
|
with self.lock:
|
2014-12-04 09:14:27 +00:00
|
|
|
_LOGGER.info("Scanning")
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2015-01-17 21:49:22 +00:00
|
|
|
self.last_results = self._api.get_attached_devices() or []
|