core/homeassistant/components/device_tracker/snmp.py

149 lines
4.1 KiB
Python
Raw Normal View History

2015-10-06 21:26:32 +00:00
"""
2015-10-07 21:04:34 +00:00
homeassistant.components.device_tracker.snmp
2015-10-06 21:26:32 +00:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2015-10-07 21:04:34 +00:00
Device tracker platform that supports fetching WiFi assiciations
through SNMP
2015-10-06 21:26:32 +00:00
2015-10-07 21:04:34 +00:00
This device tracker needs SNMP to be enabled on the WRT or WAP
Configuration:
2015-10-06 21:26:32 +00:00
device_tracker:
platform: snmp
host: YOUR_WAP_IP
community: SNMP_COMMUNITY
baseoid: BASE_OID
2015-10-07 21:04:34 +00:00
Variables:
Host
*required
The IP address of the router, e.g. 192.168.1.1
community
*Required
The SNMP community. Read-only is fine
2015-10-06 21:26:32 +00:00
2015-10-07 21:04:34 +00:00
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)
2015-10-06 21:26:32 +00:00
"""
import logging
from datetime import timedelta
import threading
import binascii
2015-10-08 08:00:30 +00:00
from homeassistant.const import CONF_HOST
2015-10-06 21:26:32 +00:00
from homeassistant.helpers import validate_config
from homeassistant.util import Throttle
from homeassistant.components.device_tracker import DOMAIN
2015-10-07 21:04:34 +00:00
# Return cached results if last scan was less then this time ago
2015-10-07 16:57:01 +00:00
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
2015-10-06 21:26:32 +00:00
_LOGGER = logging.getLogger(__name__)
2015-10-08 08:00:30 +00:00
REQUIREMENTS = ['pysnmp==4.2.5']
CONF_COMMUNITY = "community"
CONF_BASEOID = "baseoid"
2015-10-06 21:26:32 +00:00
2015-10-07 21:45:24 +00:00
2015-10-07 21:04:34 +00:00
# pylint: disable=unused-argument
def get_scanner(hass, config):
""" Validates config and returns an snmp scanner """
2015-10-07 21:45:24 +00:00
if not validate_config(config,
{DOMAIN: [CONF_HOST, CONF_COMMUNITY, CONF_BASEOID]},
_LOGGER):
2015-10-07 21:04:34 +00:00
return None
2015-10-06 21:26:32 +00:00
2015-10-07 21:04:34 +00:00
scanner = SnmpScanner(config[DOMAIN])
2015-10-06 21:26:32 +00:00
return scanner if scanner.success_init else None
class SnmpScanner(object):
2015-10-07 21:45:24 +00:00
"""
This class queries any SNMP capable Acces Point for connected devices.
"""
2015-10-06 21:26:32 +00:00
def __init__(self, config):
self.host = config[CONF_HOST]
2015-10-07 16:57:01 +00:00
self.community = config[CONF_COMMUNITY]
2015-10-06 21:26:32 +00:00
self.baseoid = config[CONF_BASEOID]
self.lock = threading.Lock()
2015-10-08 10:01:10 +00:00
self.last_results = []
2015-10-06 21:26:32 +00:00
# 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()
2015-10-07 16:57:01 +00:00
return [client['mac'] for client in self.last_results]
2015-10-06 21:26:32 +00:00
2015-10-08 10:01:10 +00:00
# Ignoring no-self-use warning
# pylint: disable=R0201
2015-10-06 21:26:32 +00:00
def get_device_name(self, device):
""" Returns the name of the given device or None if we don't know. """
2015-10-08 08:00:30 +00:00
# We have no names
2015-10-06 21:26:32 +00:00
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. """
2015-10-08 08:00:30 +00:00
from pysnmp.entity.rfc3413.oneliner import cmdgen
2015-10-06 21:26:32 +00:00
2015-10-07 21:04:34 +00:00
devices = []
2015-10-06 21:26:32 +00:00
2015-10-08 08:00:30 +00:00
snmp = cmdgen.CommandGenerator()
errindication, errstatus, errindex, restable = snmp.nextCmd(
2015-10-07 21:45:24 +00:00
cmdgen.CommunityData(self.community),
cmdgen.UdpTransportTarget((self.host, 161)),
cmdgen.MibVariable(self.baseoid)
2015-10-06 21:26:32 +00:00
)
2015-10-08 08:00:30 +00:00
if errindication:
2015-10-08 10:01:10 +00:00
#pylint: disable=W1202
2015-10-08 08:00:30 +00:00
_LOGGER.error("SNMPLIB error: {}".format(errindication))
2015-10-06 21:26:32 +00:00
return
2015-10-08 08:00:30 +00:00
if errstatus:
err = "SNMP error: {} at {}"
2015-10-08 10:01:10 +00:00
#pylint: disable=W1202
2015-10-08 08:00:30 +00:00
_LOGGER.error(err.format(errstatus.prettyPrint(),
errindex and
restable[-1][int(errindex)-1]
or '?'))
2015-10-07 16:57:01 +00:00
return
2015-10-08 08:00:30 +00:00
for resrow in restable:
for _, val in resrow:
2015-10-07 21:45:24 +00:00
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})
2015-10-06 21:26:32 +00:00
return devices