Fixed b/octet to mac adress conversion

This commit is contained in:
Tom Duijf 2015-10-07 16:57:01 +00:00
parent e535f50e03
commit a58382e763

View File

@ -26,19 +26,18 @@ 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
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=5) MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['pysnmp'] REQUIREMENTS = ['pysnmp']
def setup_scanner(hass, config): def setup_scanner(hass, config, see):
""" Setup snmp scanning """ """ Setup snmp scanning """
if not validate_config(config, #if not validate_config(config, {DOMAIN: [CONF_HOST, CONF_COMMUNITY, CONF_BASEOID]}, _LOGGER):
{DOMAIN: [CONF_HOST, CONF_COMMUNITY, CONF_BASEOID]}, # return None
_LOGGER):
return None
scanner = SnmpScanner(config[DOMAIN]) #scanner = SnmpScanner(config[DOMAIN])
scanner = SnmpScanner(config)
return scanner if scanner.success_init else None 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. """ """ This class queries any SNMP capable Acces Point for connected devices. """
def __init__(self, config): def __init__(self, config):
self.host = config[CONF_HOST] self.host = config[CONF_HOST]
self.community = config[CONF_USERNAME] self.community = config[CONF_COMMUNITY]
self.baseoid = config[CONF_BASEOID] self.baseoid = config[CONF_BASEOID]
self.lock = threading.Lock() self.lock = threading.Lock()
self.last_results = [] self.last_results = {}
# Test the router is accessible # Test the router is accessible
data = self.get_snmp_data() data = self.get_snmp_data()
@ -64,7 +63,7 @@ class SnmpScanner(object):
""" """
self._update_info() self._update_info()
return self.last_results return [client['mac'] for client in self.last_results]
def get_device_name(self, device): def get_device_name(self, device):
""" Returns the name of the given device or None if we don't know. """ """ 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): def get_snmp_data(self):
""" Fetch mac addresses from WAP via SNMP. """ """ Fetch mac addresses from WAP via SNMP. """
devices = []
from pysnmp.entity.rfc3413.oneliner import cmdgen from pysnmp.entity.rfc3413.oneliner import cmdgen
oid='1.3.6.1.4.1.14988.1.1.1.2.1.1' devices = {}
cmdGen = cmdgen.CommandGenerator()
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
cmdgen.CommunityData( self.community ), cmdgen.CommunityData( self.community ),
cmdgen.UdpTransportTarget( ( self.host , 161) ), cmdgen.UdpTransportTarget( ( self.host , 161) ),
cmdgen.MibVariable( self.baseoid ) cmdgen.MibVariable( self.baseoid )
) )
if errorIndication: if errorIndication:
_LOGGER.exception( "SNMPLIB error: {}".format( errorIndication ) ) _LOGGER.exception( "SNMPLIB error: {}".format( errorIndication ) )
return return
else: if errorStatus:
if errorStatus: _LOGGER.exception( "SNMP error: {} at {}".format( errorStatus.prettyPrint(), errorIndex and varBindTable[-1][int(errorIndex)-1] or '?' ) )
_LOGGER.exception( "SNMP error: {} at {}".format( errorStatus.prettyPrint(), errorIndex and varBindTable[-1][int(errorIndex)-1] or '?' ) ) return
return for varBindTableRow in varBindTable:
else: for key,val in varBindTableRow:
for varBindTableRow in varBindTable: mac = binascii.hexlify( val.asOctets() ).decode('utf-8')
for val in varBindTableRow.values(): mac = ':'.join( [ mac[i:i+2] for i in range( 0, len(mac), 2 ) ] )
devices.append( convertMac( val ) ) devices[mac] = { 'mac' : mac }
return devices 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)