core/homeassistant/components/snmp/device_tracker.py

125 lines
3.8 KiB
Python
Raw Normal View History

"""Support for fetching WiFi associations through SNMP."""
2016-02-19 05:27:50 +00:00
import binascii
2015-10-06 21:26:32 +00:00
import logging
from pysnmp.entity import config as cfg
from pysnmp.entity.rfc3413.oneliner import cmdgen
import voluptuous as vol
from homeassistant.components.device_tracker import (
2019-07-31 19:25:30 +00:00
DOMAIN,
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
2019-07-31 19:25:30 +00:00
DeviceScanner,
)
2015-10-08 08:00:30 +00:00
from homeassistant.const import CONF_HOST
import homeassistant.helpers.config_validation as cv
2015-10-06 21:26:32 +00:00
from .const import (
2019-07-31 19:25:30 +00:00
CONF_AUTH_KEY,
CONF_BASEOID,
CONF_COMMUNITY,
CONF_PRIV_KEY,
DEFAULT_COMMUNITY,
)
2015-10-06 21:26:32 +00:00
_LOGGER = logging.getLogger(__name__)
2017-02-05 10:22:32 +00:00
PLATFORM_SCHEMA = PARENT_PLATFORM_SCHEMA.extend(
2019-07-31 19:25:30 +00:00
{
vol.Required(CONF_BASEOID): cv.string,
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_COMMUNITY, default=DEFAULT_COMMUNITY): cv.string,
vol.Inclusive(CONF_AUTH_KEY, "keys"): cv.string,
vol.Inclusive(CONF_PRIV_KEY, "keys"): cv.string,
}
)
2015-10-07 21:45:24 +00:00
2015-10-07 21:04:34 +00:00
def get_scanner(hass, config):
2017-10-07 07:47:52 +00:00
"""Validate the configuration and return an SNMP scanner."""
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(DeviceScanner):
2016-03-07 20:18:53 +00:00
"""Queries any SNMP capable Access Point for connected devices."""
2015-10-06 21:26:32 +00:00
def __init__(self, config):
2016-03-07 20:18:53 +00:00
"""Initialize the scanner."""
2019-07-31 19:25:30 +00:00
2015-11-11 21:54:33 +00:00
self.snmp = cmdgen.CommandGenerator()
self.host = cmdgen.UdpTransportTarget((config[CONF_HOST], 161))
if CONF_AUTH_KEY not in config or CONF_PRIV_KEY not in config:
self.auth = cmdgen.CommunityData(config[CONF_COMMUNITY])
else:
self.auth = cmdgen.UsmUserData(
config[CONF_COMMUNITY],
config[CONF_AUTH_KEY],
config[CONF_PRIV_KEY],
authProtocol=cfg.usmHMACSHAAuthProtocol,
2019-07-31 19:25:30 +00:00
privProtocol=cfg.usmAesCfb128Protocol,
)
2015-11-11 21:54:33 +00:00
self.baseoid = cmdgen.MibVariable(config[CONF_BASEOID])
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):
2016-03-07 20:18:53 +00:00
"""Scan for new devices and return a list with found device IDs."""
2015-10-06 21:26:32 +00:00
self._update_info()
2019-07-31 19:25:30 +00:00
return [client["mac"] for client in self.last_results if client.get("mac")]
2015-10-06 21:26:32 +00:00
def get_device_name(self, device):
2016-03-07 20:18:53 +00:00
"""Return 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
def _update_info(self):
"""Ensure the information from the device is up to date.
2016-03-07 20:18:53 +00:00
Return boolean if scanning successful.
2015-10-06 21:26:32 +00:00
"""
if not self.success_init:
return False
data = self.get_snmp_data()
if not data:
return False
2015-10-06 21:26:32 +00:00
self.last_results = data
return True
2015-10-06 21:26:32 +00:00
def get_snmp_data(self):
"""Fetch MAC addresses from access point via SNMP."""
2015-10-07 21:04:34 +00:00
devices = []
2015-10-06 21:26:32 +00:00
2015-11-11 21:54:33 +00:00
errindication, errstatus, errindex, restable = self.snmp.nextCmd(
2019-07-31 19:25:30 +00:00
self.auth, self.host, self.baseoid
)
2015-10-08 08:00:30 +00:00
if errindication:
2015-10-08 14:54:20 +00:00
_LOGGER.error("SNMPLIB error: %s", errindication)
2015-10-06 21:26:32 +00:00
return
2015-10-08 08:00:30 +00:00
if errstatus:
2019-07-31 19:25:30 +00:00
_LOGGER.error(
"SNMP error: %s at %s",
errstatus.prettyPrint(),
errindex and restable[int(errindex) - 1][0] 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:
2017-03-27 10:22:59 +00:00
try:
2019-07-31 19:25:30 +00:00
mac = binascii.hexlify(val.asOctets()).decode("utf-8")
2017-03-27 10:22:59 +00:00
except AttributeError:
continue
_LOGGER.debug("Found MAC address: %s", mac)
2019-07-31 19:25:30 +00:00
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