diff --git a/.coveragerc b/.coveragerc index 1ff145e2de3..433a43bea10 100644 --- a/.coveragerc +++ b/.coveragerc @@ -39,6 +39,7 @@ omit = homeassistant/components/device_tracker/thomson.py homeassistant/components/device_tracker/tomato.py homeassistant/components/device_tracker/tplink.py + homeassistant/components/device_tracker/snmp.py homeassistant/components/discovery.py homeassistant/components/downloader.py homeassistant/components/keyboard.py diff --git a/README.md b/README.md index 6b1b1353392..6d1baa5c50f 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Check out [the website](https://home-assistant.io) for [a demo][demo], installat Examples of devices it can interface it: - * Monitoring connected devices to a wireless router: [OpenWrt](https://openwrt.org/), [Tomato](http://www.polarcloud.com/tomato), [Netgear](http://netgear.com), [DD-WRT](http://www.dd-wrt.com/site/index), [TPLink](http://www.tp-link.us/), and [ASUSWRT](http://event.asus.com/2013/nw/ASUSWRT/) + * Monitoring connected devices to a wireless router: [OpenWrt](https://openwrt.org/), [Tomato](http://www.polarcloud.com/tomato), [Netgear](http://netgear.com), [DD-WRT](http://www.dd-wrt.com/site/index), [TPLink](http://www.tp-link.us/), [ASUSWRT](http://event.asus.com/2013/nw/ASUSWRT/) and any SNMP capable WAP/WRT * [Philips Hue](http://meethue.com) lights, [WeMo](http://www.belkin.com/us/Products/home-automation/c/wemo-home-automation/) switches, [Edimax](http://www.edimax.com/) switches, [Efergy](https://efergy.com) energy monitoring, RFXtrx sensors, and [Tellstick](http://www.telldus.se/products/tellstick) devices and sensors * [Google Chromecasts](http://www.google.com/intl/en/chrome/devices/chromecast), [Music Player Daemon](http://www.musicpd.org/), [Logitech Squeezebox](https://en.wikipedia.org/wiki/Squeezebox_%28network_music_player%29), [Kodi (XBMC)](http://kodi.tv/), and iTunes (by way of [itunes-api](https://github.com/maddox/itunes-api)) * Support for [ISY994](https://www.universal-devices.com/residential/isy994i-series/) (Insteon and X10 devices), [Z-Wave](http://www.z-wave.com/), [Nest Thermostats](https://nest.com/), [Arduino](https://www.arduino.cc/), [Raspberry Pi](https://www.raspberrypi.org/), and [Modbus](http://www.modbus.org/) diff --git a/homeassistant/components/device_tracker/snmp.py b/homeassistant/components/device_tracker/snmp.py index e03fe134c8d..c170bedf79e 100644 --- a/homeassistant/components/device_tracker/snmp.py +++ b/homeassistant/components/device_tracker/snmp.py @@ -36,6 +36,7 @@ import logging from datetime import timedelta import threading import binascii +from pysnmp.entity.rfc3413.oneliner import cmdgen from homeassistant.const import CONF_HOST, CONF_COMMUNITY, CONF_BASEOID from homeassistant.helpers import validate_config @@ -48,10 +49,13 @@ MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10) _LOGGER = logging.getLogger(__name__) REQUIREMENTS = ['pysnmp'] + # pylint: disable=unused-argument def get_scanner(hass, config): """ Validates config and returns an snmp scanner """ - if not validate_config(config, {DOMAIN: [CONF_HOST, CONF_COMMUNITY, CONF_BASEOID]}, _LOGGER): + if not validate_config(config, + {DOMAIN: [CONF_HOST, CONF_COMMUNITY, CONF_BASEOID]}, + _LOGGER): return None scanner = SnmpScanner(config[DOMAIN]) @@ -60,7 +64,9 @@ def get_scanner(hass, config): class SnmpScanner(object): - """ This class queries any SNMP capable Acces Point for connected devices. """ + """ + This class queries any SNMP capable Acces Point for connected devices. + """ def __init__(self, config): self.host = config[CONF_HOST] self.community = config[CONF_COMMUNITY] @@ -105,26 +111,24 @@ class SnmpScanner(object): def get_snmp_data(self): """ Fetch mac addresses from WAP via SNMP. """ - from pysnmp.entity.rfc3413.oneliner import cmdgen devices = [] cmdGen = cmdgen.CommandGenerator() - errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( - cmdgen.CommunityData( self.community ), - cmdgen.UdpTransportTarget( ( self.host , 161) ), - cmdgen.MibVariable( self.baseoid ) + errIndication, errStatus, errIndex, varBindTable = cmdGen.nextCmd( + cmdgen.CommunityData(self.community), + cmdgen.UdpTransportTarget((self.host, 161)), + cmdgen.MibVariable(self.baseoid) ) - if errorIndication: - _LOGGER.exception( "SNMPLIB error: {}".format( errorIndication ) ) + if errIndication: + _LOGGER.exception("SNMPLIB error: {}".format(errIndication)) return - if errorStatus: - _LOGGER.exception( "SNMP error: {} at {}".format( errorStatus.prettyPrint(), errorIndex and varBindTable[-1][int(errorIndex)-1] or '?' ) ) + if errStatus: + _LOGGER.exception("SNMP error: {} at {}".format(errStatus.prettyPrint(), errIndex and varBindTable[-1][int(errIndex)-1] or '?')) return for varBindTableRow in varBindTable: - for key,val in varBindTableRow: - mac = binascii.hexlify( val.asOctets() ).decode('utf-8') - mac = ':'.join( [ mac[i:i+2] for i in range( 0, len(mac), 2 ) ] ) - devices.append( { 'mac' : mac } ) + for val in varBindTableRow.values(): + mac = binascii.hexlify(val.asOctets()).decode('utf-8') + mac = ':'.join([mac[i:i+2] for i in range(0, len(mac), 2)]) + devices.append({'mac': mac}) return devices - diff --git a/requirements_all.txt b/requirements_all.txt index 2b7074d91cd..14ba1985c7e 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -137,3 +137,7 @@ SoCo==0.11.1 # PlexAPI (media_player.plex) https://github.com/adrienbrault/python-plexapi/archive/df2d0847e801d6d5cda920326d693cf75f304f1a.zip#python-plexapi==1.0.2 + +# python-pysnmp (device_tracker.snmp) +pysnmp +