From 0fb9e1b16c22662f8b32083040150ca04fc7cc7c Mon Sep 17 00:00:00 2001 From: Tom Duijf Date: Tue, 6 Oct 2015 21:26:32 +0000 Subject: [PATCH 01/12] Initial commit of snmp device tracker --- .../components/device_tracker/snmp.py | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 homeassistant/components/device_tracker/snmp.py diff --git a/homeassistant/components/device_tracker/snmp.py b/homeassistant/components/device_tracker/snmp.py new file mode 100644 index 00000000000..0c4e6bb1ee2 --- /dev/null +++ b/homeassistant/components/device_tracker/snmp.py @@ -0,0 +1,125 @@ +""" +homeassistant.components.device_tracker.demo +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Fetch wifi associations through snmp + +device_tracker: + platform: snmp + host: YOUR_WAP_IP + community: SNMP_COMMUNITY + baseoid: BASE_OID + + +Little help with base oids: + Microtik: 1.3.6.1.4.1.14988.1.1.1.2.1.1 (confirmed) + Aruba: 1.3.6.1.4.1.14823.2.3.3.1.2.4.1.2 (untested) + +""" +import logging +from datetime import timedelta +import threading +import binascii + +from homeassistant.const import CONF_HOST, CONF_COMMUNITY, CONF_BASEOID +from homeassistant.helpers import validate_config +from homeassistant.util import Throttle +from homeassistant.components.device_tracker import DOMAIN + +MIN_TIME_BETWEEN_SCANS = timedelta(seconds=5) + +_LOGGER = logging.getLogger(__name__) +REQUIREMENTS = ['pysnmp'] + +def setup_scanner(hass, config): + """ Setup snmp scanning """ + if not validate_config(config, + {DOMAIN: [CONF_HOST, CONF_COMMUNITY, CONF_BASEOID]}, + _LOGGER): + return None + + scanner = SnmpScanner(config[DOMAIN]) + + return scanner if scanner.success_init else None + + +class SnmpScanner(object): + """ This class queries any SNMP capable Acces Point for connected devices. """ + def __init__(self, config): + self.host = config[CONF_HOST] + self.community = config[CONF_USERNAME] + self.baseoid = config[CONF_BASEOID] + + self.lock = threading.Lock() + + self.last_results = [] + + # Test the router is accessible + data = self.get_snmp_data() + self.success_init = data is not None + + def scan_devices(self): + """ + Scans for new devices and return a list containing found device IDs. + """ + + self._update_info() + return self.last_results + + def get_device_name(self, device): + """ Returns the name of the given device or None if we don't know. """ + return None + + @Throttle(MIN_TIME_BETWEEN_SCANS) + def _update_info(self): + """ + Ensures the information from the WAP is up to date. + Returns boolean if scanning successful. + """ + if not self.success_init: + return False + + with self.lock: + data = self.get_snmp_data() + if not data: + return False + + self.last_results = data + return True + + def get_snmp_data(self): + """ Fetch mac addresses from WAP via SNMP. """ + devices = [] + + + from pysnmp.entity.rfc3413.oneliner import cmdgen + + oid='1.3.6.1.4.1.14988.1.1.1.2.1.1' + cmdGen = cmdgen.CommandGenerator() + + errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( + cmdgen.CommunityData( self.community ), + cmdgen.UdpTransportTarget( ( self.host , 161) ), + cmdgen.MibVariable( self.baseoid ) + ) + + if errorIndication: + _LOGGER.exception( "SNMPLIB error: {}".format( errorIndication ) ) + return + else: + if errorStatus: + _LOGGER.exception( "SNMP error: {} at {}".format( errorStatus.prettyPrint(), errorIndex and varBindTable[-1][int(errorIndex)-1] or '?' ) ) + return + else: + for varBindTableRow in varBindTable: + for val in varBindTableRow.values(): + devices.append( convertMac( val ) ) + return devices + + def convertMac(octect): + ''' Convert a binary mac address to a string ''' + mac = [] + for x in list(octet): + mac.append(binascii.b2a_hex(x)) + return ":".join(mac) + From df7fbf664e70dc180dbd4999e225dd5a5d688038 Mon Sep 17 00:00:00 2001 From: Tom Duijf Date: Tue, 6 Oct 2015 21:27:04 +0000 Subject: [PATCH 02/12] Added constants needed for snmp --- homeassistant/const.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/homeassistant/const.py b/homeassistant/const.py index c256aa921d1..f156a9399d5 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -24,6 +24,8 @@ CONF_USERNAME = "username" CONF_PASSWORD = "password" CONF_API_KEY = "api_key" CONF_ACCESS_TOKEN = "access_token" +CONF_COMMUNITY = "community" +CONF_BASEOID = "baseoid" # #### EVENTS #### EVENT_HOMEASSISTANT_START = "homeassistant_start" From a58382e7630d4bc39f52a1c4f501c922f1421795 Mon Sep 17 00:00:00 2001 From: Tom Duijf Date: Wed, 7 Oct 2015 16:57:01 +0000 Subject: [PATCH 03/12] Fixed b/octet to mac adress conversion --- .../components/device_tracker/snmp.py | 50 +++++++------------ 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/homeassistant/components/device_tracker/snmp.py b/homeassistant/components/device_tracker/snmp.py index 0c4e6bb1ee2..81d9d1750c1 100644 --- a/homeassistant/components/device_tracker/snmp.py +++ b/homeassistant/components/device_tracker/snmp.py @@ -26,19 +26,18 @@ from homeassistant.helpers import validate_config from homeassistant.util import Throttle from homeassistant.components.device_tracker import DOMAIN -MIN_TIME_BETWEEN_SCANS = timedelta(seconds=5) +MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10) _LOGGER = logging.getLogger(__name__) REQUIREMENTS = ['pysnmp'] -def setup_scanner(hass, config): +def setup_scanner(hass, config, see): """ Setup snmp scanning """ - if not validate_config(config, - {DOMAIN: [CONF_HOST, CONF_COMMUNITY, CONF_BASEOID]}, - _LOGGER): - return None + #if not validate_config(config, {DOMAIN: [CONF_HOST, CONF_COMMUNITY, CONF_BASEOID]}, _LOGGER): + # return None - scanner = SnmpScanner(config[DOMAIN]) + #scanner = SnmpScanner(config[DOMAIN]) + scanner = SnmpScanner(config) return scanner if scanner.success_init else None @@ -47,12 +46,12 @@ class SnmpScanner(object): """ This class queries any SNMP capable Acces Point for connected devices. """ def __init__(self, config): self.host = config[CONF_HOST] - self.community = config[CONF_USERNAME] + self.community = config[CONF_COMMUNITY] self.baseoid = config[CONF_BASEOID] self.lock = threading.Lock() - self.last_results = [] + self.last_results = {} # Test the router is accessible data = self.get_snmp_data() @@ -64,7 +63,7 @@ class SnmpScanner(object): """ self._update_info() - return self.last_results + return [client['mac'] for client in self.last_results] def get_device_name(self, device): """ Returns the name of the given device or None if we don't know. """ @@ -89,37 +88,26 @@ class SnmpScanner(object): def get_snmp_data(self): """ Fetch mac addresses from WAP via SNMP. """ - devices = [] - - from pysnmp.entity.rfc3413.oneliner import cmdgen - oid='1.3.6.1.4.1.14988.1.1.1.2.1.1' - cmdGen = cmdgen.CommandGenerator() + devices = {} + cmdGen = cmdgen.CommandGenerator() errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( cmdgen.CommunityData( self.community ), cmdgen.UdpTransportTarget( ( self.host , 161) ), cmdgen.MibVariable( self.baseoid ) ) - if errorIndication: _LOGGER.exception( "SNMPLIB error: {}".format( errorIndication ) ) return - else: - if errorStatus: - _LOGGER.exception( "SNMP error: {} at {}".format( errorStatus.prettyPrint(), errorIndex and varBindTable[-1][int(errorIndex)-1] or '?' ) ) - return - else: - for varBindTableRow in varBindTable: - for val in varBindTableRow.values(): - devices.append( convertMac( val ) ) + if errorStatus: + _LOGGER.exception( "SNMP error: {} at {}".format( errorStatus.prettyPrint(), errorIndex and varBindTable[-1][int(errorIndex)-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[mac] = { 'mac' : mac } return devices - def convertMac(octect): - ''' Convert a binary mac address to a string ''' - mac = [] - for x in list(octet): - mac.append(binascii.b2a_hex(x)) - return ":".join(mac) - From 469f35d25f0012e8a99712aee67067f091b0a7bc Mon Sep 17 00:00:00 2001 From: Tom Duijf Date: Wed, 7 Oct 2015 21:04:34 +0000 Subject: [PATCH 04/12] various fixes, initial working version --- .../components/device_tracker/snmp.py | 44 +++++++++++++------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/device_tracker/snmp.py b/homeassistant/components/device_tracker/snmp.py index 81d9d1750c1..f89c3cb8d5a 100644 --- a/homeassistant/components/device_tracker/snmp.py +++ b/homeassistant/components/device_tracker/snmp.py @@ -1,8 +1,12 @@ """ -homeassistant.components.device_tracker.demo +homeassistant.components.device_tracker.snmp ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Device tracker platform that supports fetching WiFi assiciations +through SNMP -Fetch wifi associations through snmp +This device tracker needs SNMP to be enabled on the WRT or WAP + +Configuration: device_tracker: platform: snmp @@ -10,10 +14,22 @@ device_tracker: community: SNMP_COMMUNITY baseoid: BASE_OID +Variables: + Host + *required + The IP address of the router, e.g. 192.168.1.1 -Little help with base oids: - Microtik: 1.3.6.1.4.1.14988.1.1.1.2.1.1 (confirmed) - Aruba: 1.3.6.1.4.1.14823.2.3.3.1.2.4.1.2 (untested) + community + *Required + The SNMP community. Read-only is fine + + baseoid + *Required + The OID at which WiFi associations can be found + + Little help with base oids: + Microtik: 1.3.6.1.4.1.14988.1.1.1.2.1.1 (confirmed) + Aruba: 1.3.6.1.4.1.14823.2.3.3.1.2.4.1.2 (untested) """ import logging @@ -26,18 +42,19 @@ from homeassistant.helpers import validate_config from homeassistant.util import Throttle 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=10) _LOGGER = logging.getLogger(__name__) REQUIREMENTS = ['pysnmp'] -def setup_scanner(hass, config, see): - """ Setup snmp scanning """ - #if not validate_config(config, {DOMAIN: [CONF_HOST, CONF_COMMUNITY, CONF_BASEOID]}, _LOGGER): - # return None +# 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): + return None - #scanner = SnmpScanner(config[DOMAIN]) - scanner = SnmpScanner(config) + scanner = SnmpScanner(config[DOMAIN]) return scanner if scanner.success_init else None @@ -63,6 +80,7 @@ class SnmpScanner(object): """ self._update_info() + _LOGGER.error( self.last_results ) return [client['mac'] for client in self.last_results] def get_device_name(self, device): @@ -90,7 +108,7 @@ class SnmpScanner(object): """ Fetch mac addresses from WAP via SNMP. """ from pysnmp.entity.rfc3413.oneliner import cmdgen - devices = {} + devices = [] cmdGen = cmdgen.CommandGenerator() errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( @@ -108,6 +126,6 @@ class SnmpScanner(object): 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[mac] = { 'mac' : mac } + devices.append( { 'mac' : mac } ) return devices From 9377b647f551047f4fcab41c2a4bca0351759a74 Mon Sep 17 00:00:00 2001 From: Tom Duijf Date: Wed, 7 Oct 2015 21:05:27 +0000 Subject: [PATCH 05/12] removed debug logging --- homeassistant/components/device_tracker/snmp.py | 1 - 1 file changed, 1 deletion(-) diff --git a/homeassistant/components/device_tracker/snmp.py b/homeassistant/components/device_tracker/snmp.py index f89c3cb8d5a..e03fe134c8d 100644 --- a/homeassistant/components/device_tracker/snmp.py +++ b/homeassistant/components/device_tracker/snmp.py @@ -80,7 +80,6 @@ class SnmpScanner(object): """ self._update_info() - _LOGGER.error( self.last_results ) return [client['mac'] for client in self.last_results] def get_device_name(self, device): From d556e5979a40d88dbef0c95fd5fa072757bab1ea Mon Sep 17 00:00:00 2001 From: Tom Duijf Date: Wed, 7 Oct 2015 21:45:24 +0000 Subject: [PATCH 06/12] Updated misc files and code styling --- .coveragerc | 1 + README.md | 2 +- .../components/device_tracker/snmp.py | 36 ++++++++++--------- requirements_all.txt | 4 +++ 4 files changed, 26 insertions(+), 17 deletions(-) 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 + From 7cb0f805ee90b23c64e0163326784efb3fb1b6c5 Mon Sep 17 00:00:00 2001 From: Tom Duijf Date: Wed, 7 Oct 2015 22:17:49 +0000 Subject: [PATCH 07/12] fixed loop --- homeassistant/components/device_tracker/snmp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/device_tracker/snmp.py b/homeassistant/components/device_tracker/snmp.py index c170bedf79e..0ec0663b7c5 100644 --- a/homeassistant/components/device_tracker/snmp.py +++ b/homeassistant/components/device_tracker/snmp.py @@ -127,7 +127,7 @@ class SnmpScanner(object): _LOGGER.exception("SNMP error: {} at {}".format(errStatus.prettyPrint(), errIndex and varBindTable[-1][int(errIndex)-1] or '?')) return for varBindTableRow in varBindTable: - for val in varBindTableRow.values(): + for _,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}) From ae6f651c7df2239cb00650c4cc16b2eeca6a276b Mon Sep 17 00:00:00 2001 From: Tom Duijf Date: Wed, 7 Oct 2015 23:22:29 +0000 Subject: [PATCH 08/12] styling and version for requirement --- .coveragerc | 1 + homeassistant/components/device_tracker/snmp.py | 8 +++++--- requirements_all.txt | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.coveragerc b/.coveragerc index 433a43bea10..d9b4a29a1b5 100644 --- a/.coveragerc +++ b/.coveragerc @@ -40,6 +40,7 @@ omit = 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/homeassistant/components/device_tracker/snmp.py b/homeassistant/components/device_tracker/snmp.py index 0ec0663b7c5..9113a1ab98d 100644 --- a/homeassistant/components/device_tracker/snmp.py +++ b/homeassistant/components/device_tracker/snmp.py @@ -47,7 +47,7 @@ from homeassistant.components.device_tracker import DOMAIN MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10) _LOGGER = logging.getLogger(__name__) -REQUIREMENTS = ['pysnmp'] +REQUIREMENTS = ['pysnmp<=4.2.5'] # pylint: disable=unused-argument @@ -124,10 +124,12 @@ class SnmpScanner(object): _LOGGER.exception("SNMPLIB error: {}".format(errIndication)) return if errStatus: - _LOGGER.exception("SNMP error: {} at {}".format(errStatus.prettyPrint(), errIndex and varBindTable[-1][int(errIndex)-1] or '?')) + _LOGGER.exception("SNMP error: {} at {}".format( + errStatus.prettyPrint(), + errIndex and varBindTable[-1][int(errIndex)-1] or '?')) return for varBindTableRow in varBindTable: - for _,val in varBindTableRow: + for _, 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}) diff --git a/requirements_all.txt b/requirements_all.txt index 14ba1985c7e..1c59b62fabd 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -139,5 +139,5 @@ SoCo==0.11.1 https://github.com/adrienbrault/python-plexapi/archive/df2d0847e801d6d5cda920326d693cf75f304f1a.zip#python-plexapi==1.0.2 # python-pysnmp (device_tracker.snmp) -pysnmp +pysnmp>=4.2.5 From 213a1fe4ba26984be3bc1aff8392991b0cfb79a3 Mon Sep 17 00:00:00 2001 From: Tom Duijf Date: Thu, 8 Oct 2015 08:00:30 +0000 Subject: [PATCH 09/12] Various fixes, CI validation --- .coveragerc | 1 - .../components/device_tracker/snmp.py | 34 ++++++++++++------- homeassistant/const.py | 2 -- requirements_all.txt | 2 +- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/.coveragerc b/.coveragerc index d9b4a29a1b5..433a43bea10 100644 --- a/.coveragerc +++ b/.coveragerc @@ -40,7 +40,6 @@ omit = 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/homeassistant/components/device_tracker/snmp.py b/homeassistant/components/device_tracker/snmp.py index 9113a1ab98d..6d8f5113df0 100644 --- a/homeassistant/components/device_tracker/snmp.py +++ b/homeassistant/components/device_tracker/snmp.py @@ -36,9 +36,8 @@ 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.const import CONF_HOST from homeassistant.helpers import validate_config from homeassistant.util import Throttle from homeassistant.components.device_tracker import DOMAIN @@ -47,7 +46,10 @@ from homeassistant.components.device_tracker import DOMAIN MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10) _LOGGER = logging.getLogger(__name__) -REQUIREMENTS = ['pysnmp<=4.2.5'] +REQUIREMENTS = ['pysnmp==4.2.5'] + +CONF_COMMUNITY = "community" +CONF_BASEOID = "baseoid" # pylint: disable=unused-argument @@ -90,6 +92,7 @@ class SnmpScanner(object): def get_device_name(self, device): """ Returns the name of the given device or None if we don't know. """ + # We have no names return None @Throttle(MIN_TIME_BETWEEN_SCANS) @@ -111,25 +114,30 @@ 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() - errIndication, errStatus, errIndex, varBindTable = cmdGen.nextCmd( + snmp = cmdgen.CommandGenerator() + errindication, errstatus, errindex, restable = snmp.nextCmd( cmdgen.CommunityData(self.community), cmdgen.UdpTransportTarget((self.host, 161)), cmdgen.MibVariable(self.baseoid) ) - if errIndication: - _LOGGER.exception("SNMPLIB error: {}".format(errIndication)) + + if errindication: + _LOGGER.error("SNMPLIB error: {}".format(errindication)) return - if errStatus: - _LOGGER.exception("SNMP error: {} at {}".format( - errStatus.prettyPrint(), - errIndex and varBindTable[-1][int(errIndex)-1] or '?')) + if errstatus: + err = "SNMP error: {} at {}" + _LOGGER.error(err.format(errstatus.prettyPrint(), + errindex and + restable[-1][int(errindex)-1] + or '?')) return - for varBindTableRow in varBindTable: - for _, val in varBindTableRow: + + for resrow in restable: + for _, val in resrow: 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}) diff --git a/homeassistant/const.py b/homeassistant/const.py index 53ccd3c67db..2d272ca3a0b 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -24,8 +24,6 @@ CONF_USERNAME = "username" CONF_PASSWORD = "password" CONF_API_KEY = "api_key" CONF_ACCESS_TOKEN = "access_token" -CONF_COMMUNITY = "community" -CONF_BASEOID = "baseoid" # #### EVENTS #### EVENT_HOMEASSISTANT_START = "homeassistant_start" diff --git a/requirements_all.txt b/requirements_all.txt index 1c59b62fabd..7e3a3acc458 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -139,5 +139,5 @@ SoCo==0.11.1 https://github.com/adrienbrault/python-plexapi/archive/df2d0847e801d6d5cda920326d693cf75f304f1a.zip#python-plexapi==1.0.2 # python-pysnmp (device_tracker.snmp) -pysnmp>=4.2.5 +pysnmp==4.2.5 From 85bf6cb5686fc547eadd831f7cbd1597d9a3ed2e Mon Sep 17 00:00:00 2001 From: Tom Duijf Date: Thu, 8 Oct 2015 10:01:10 +0000 Subject: [PATCH 10/12] Added pylint disables --- homeassistant/components/device_tracker/snmp.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/device_tracker/snmp.py b/homeassistant/components/device_tracker/snmp.py index 6d8f5113df0..8fe80cf85d6 100644 --- a/homeassistant/components/device_tracker/snmp.py +++ b/homeassistant/components/device_tracker/snmp.py @@ -76,7 +76,7 @@ class SnmpScanner(object): self.lock = threading.Lock() - self.last_results = {} + self.last_results = [] # Test the router is accessible data = self.get_snmp_data() @@ -90,6 +90,8 @@ class SnmpScanner(object): self._update_info() return [client['mac'] for client in self.last_results] + # Ignoring no-self-use warning + # pylint: disable=R0201 def get_device_name(self, device): """ Returns the name of the given device or None if we don't know. """ # We have no names @@ -126,10 +128,12 @@ class SnmpScanner(object): ) if errindication: + #pylint: disable=W1202 _LOGGER.error("SNMPLIB error: {}".format(errindication)) return if errstatus: err = "SNMP error: {} at {}" + #pylint: disable=W1202 _LOGGER.error(err.format(errstatus.prettyPrint(), errindex and restable[-1][int(errindex)-1] From 721c1d0f547293fe16f5487de03575ab06244356 Mon Sep 17 00:00:00 2001 From: Tom Duijf Date: Thu, 8 Oct 2015 10:24:55 +0000 Subject: [PATCH 11/12] styling fix for flake --- homeassistant/components/device_tracker/snmp.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/device_tracker/snmp.py b/homeassistant/components/device_tracker/snmp.py index 8fe80cf85d6..012c7250c33 100644 --- a/homeassistant/components/device_tracker/snmp.py +++ b/homeassistant/components/device_tracker/snmp.py @@ -90,7 +90,7 @@ class SnmpScanner(object): self._update_info() return [client['mac'] for client in self.last_results] - # Ignoring no-self-use warning + # Supressing no-self-use warning # pylint: disable=R0201 def get_device_name(self, device): """ Returns the name of the given device or None if we don't know. """ @@ -128,12 +128,14 @@ class SnmpScanner(object): ) if errindication: - #pylint: disable=W1202 + # Supressing logging-format-interpolation + # pylint: disable=W1202 _LOGGER.error("SNMPLIB error: {}".format(errindication)) return if errstatus: err = "SNMP error: {} at {}" - #pylint: disable=W1202 + # Supressing logging-format-interpolation + # pylint: disable=W1202 _LOGGER.error(err.format(errstatus.prettyPrint(), errindex and restable[-1][int(errindex)-1] From ee23c0fe14a8eb6b3db60f55773a64864330325d Mon Sep 17 00:00:00 2001 From: Tom Duijf Date: Thu, 8 Oct 2015 14:54:20 +0000 Subject: [PATCH 12/12] cleaner logging --- homeassistant/components/device_tracker/snmp.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/device_tracker/snmp.py b/homeassistant/components/device_tracker/snmp.py index 012c7250c33..2fbc03b980a 100644 --- a/homeassistant/components/device_tracker/snmp.py +++ b/homeassistant/components/device_tracker/snmp.py @@ -128,18 +128,12 @@ class SnmpScanner(object): ) if errindication: - # Supressing logging-format-interpolation - # pylint: disable=W1202 - _LOGGER.error("SNMPLIB error: {}".format(errindication)) + _LOGGER.error("SNMPLIB error: %s", errindication) return if errstatus: - err = "SNMP error: {} at {}" - # Supressing logging-format-interpolation - # pylint: disable=W1202 - _LOGGER.error(err.format(errstatus.prettyPrint(), - errindex and - restable[-1][int(errindex)-1] - or '?')) + _LOGGER.error('SNMP error: %s at %s', errstatus.prettyPrint(), + errindex and restable[-1][int(errindex)-1] + or '?') return for resrow in restable: