Allow for Netgear router discovery

This commit is contained in:
Paulus Schoutsen 2015-08-23 17:20:09 -07:00
parent e917479fba
commit 35489998df
4 changed files with 34 additions and 16 deletions

View File

@ -66,14 +66,15 @@ def setup(hass, config):
'device_tracker.{}'.format(tracker_type)) 'device_tracker.{}'.format(tracker_type))
if tracker_implementation is None: if tracker_implementation is None:
_LOGGER.error("Unknown device_tracker type specified.") _LOGGER.error("Unknown device_tracker type specified: %s.",
tracker_type)
return False return False
device_scanner = tracker_implementation.get_scanner(hass, config) device_scanner = tracker_implementation.get_scanner(hass, config)
if device_scanner is None: if device_scanner is None:
_LOGGER.error("Failed to initialize device scanner for %s", _LOGGER.error("Failed to initialize device scanner: %s",
tracker_type) tracker_type)
return False return False

View File

@ -35,7 +35,6 @@ from datetime import timedelta
import threading import threading
from homeassistant.const import CONF_HOST, CONF_USERNAME, CONF_PASSWORD from homeassistant.const import CONF_HOST, CONF_USERNAME, CONF_PASSWORD
from homeassistant.helpers import validate_config
from homeassistant.util import Throttle from homeassistant.util import Throttle
from homeassistant.components.device_tracker import DOMAIN from homeassistant.components.device_tracker import DOMAIN
@ -43,20 +42,21 @@ from homeassistant.components.device_tracker import DOMAIN
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=5) MIN_TIME_BETWEEN_SCANS = timedelta(seconds=5)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['pynetgear>=0.1'] REQUIREMENTS = ['pynetgear>=0.3']
def get_scanner(hass, config): def get_scanner(hass, config):
""" Validates config and returns a Netgear scanner. """ """ Validates config and returns a Netgear scanner. """
if not validate_config(config, info = config[DOMAIN]
{DOMAIN: [CONF_HOST, CONF_USERNAME, CONF_PASSWORD]}, host = info.get(CONF_HOST)
_LOGGER): 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 return None
info = config[DOMAIN] scanner = NetgearDeviceScanner(host, password, username)
scanner = NetgearDeviceScanner(
info[CONF_HOST], info[CONF_USERNAME], info[CONF_PASSWORD])
return scanner if scanner.success_init else None return scanner if scanner.success_init else None
@ -68,16 +68,24 @@ class NetgearDeviceScanner(object):
import pynetgear import pynetgear
self.last_results = [] self.last_results = []
self._api = pynetgear.Netgear(host, username, password)
self.lock = threading.Lock() self.lock = threading.Lock()
if host is None:
print("BIER")
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") _LOGGER.info("Logging in")
self.success_init = self._api.login() results = self._api.get_attached_devices()
self.success_init = results is not None
if self.success_init: if self.success_init:
self._update_info() self.last_results = results
else: else:
_LOGGER.error("Failed to Login") _LOGGER.error("Failed to Login")

View File

@ -28,11 +28,13 @@ SCAN_INTERVAL = 300 # seconds
SERVICE_WEMO = 'belkin_wemo' SERVICE_WEMO = 'belkin_wemo'
SERVICE_HUE = 'philips_hue' SERVICE_HUE = 'philips_hue'
SERVICE_CAST = 'google_cast' SERVICE_CAST = 'google_cast'
SERVICE_NETGEAR = 'netgear_router'
SERVICE_HANDLERS = { SERVICE_HANDLERS = {
SERVICE_WEMO: "switch", SERVICE_WEMO: "switch",
SERVICE_CAST: "media_player", SERVICE_CAST: "media_player",
SERVICE_HUE: "light", SERVICE_HUE: "light",
SERVICE_NETGEAR: 'device_tracker',
} }
@ -77,6 +79,13 @@ def setup(hass, config):
if not component: if not component:
return return
# Hack - fix when device_tracker supports discovery
if service == SERVICE_NETGEAR:
bootstrap.setup_component(hass, component, {
'device_tracker': {'platform': 'netgear'}
})
return
# This component cannot be setup. # This component cannot be setup.
if not bootstrap.setup_component(hass, component, config): if not bootstrap.setup_component(hass, component, config):
return return

View File

@ -81,7 +81,7 @@ https://github.com/Danielhiversen/pyRFXtrx/archive/master.zip
https://github.com/theolind/pymysensors/archive/master.zip#egg=pymysensors-0.1 https://github.com/theolind/pymysensors/archive/master.zip#egg=pymysensors-0.1
# Netgear (device_tracker.netgear) # Netgear (device_tracker.netgear)
pynetgear>=0.1 pynetgear>=0.3
# Netdisco (discovery) # Netdisco (discovery)
netdisco>=0.3 netdisco>=0.3