Add attributes of ARP table (#17987)

* Add attributes of ARP table

This adds the device attributes available in the ARP table and a few more. Implementation is inspired by the nmap scanner.

* lint spaces
This commit is contained in:
akloeckner 2018-11-06 13:15:48 +01:00 committed by Paulus Schoutsen
parent 114bc8ec18
commit 1aba4699b9

View File

@ -7,6 +7,7 @@ https://home-assistant.io/components/device_tracker.luci/
import json import json
import logging import logging
import re import re
from collections import namedtuple
import requests import requests
import voluptuous as vol import voluptuous as vol
@ -43,14 +44,17 @@ def get_scanner(hass, config):
return scanner if scanner.success_init else None return scanner if scanner.success_init else None
Device = namedtuple('Device', ['mac', 'ip', 'flags', 'device', 'host'])
class LuciDeviceScanner(DeviceScanner): class LuciDeviceScanner(DeviceScanner):
"""This class queries a wireless router running OpenWrt firmware.""" """This class queries a wireless router running OpenWrt firmware."""
def __init__(self, config): def __init__(self, config):
"""Initialize the scanner.""" """Initialize the scanner."""
host = config[CONF_HOST] self.host = config[CONF_HOST]
protocol = 'http' if not config[CONF_SSL] else 'https' protocol = 'http' if not config[CONF_SSL] else 'https'
self.origin = '{}://{}'.format(protocol, host) self.origin = '{}://{}'.format(protocol, self.host)
self.username = config[CONF_USERNAME] self.username = config[CONF_USERNAME]
self.password = config[CONF_PASSWORD] self.password = config[CONF_PASSWORD]
@ -68,7 +72,7 @@ class LuciDeviceScanner(DeviceScanner):
def scan_devices(self): def scan_devices(self):
"""Scan for new devices and return a list with found device IDs.""" """Scan for new devices and return a list with found device IDs."""
self._update_info() self._update_info()
return self.last_results return [device.mac for device in self.last_results]
def get_device_name(self, device): def get_device_name(self, device):
"""Return the name of the given device or None if we don't know.""" """Return the name of the given device or None if we don't know."""
@ -88,6 +92,18 @@ class LuciDeviceScanner(DeviceScanner):
return return
return self.mac2name.get(device.upper(), None) return self.mac2name.get(device.upper(), None)
def get_extra_attributes(self, device):
"""Return the IP of the given device."""
filter_att = next((
{
'ip': result.ip,
'flags': result.flags,
'device': result.device,
'host': result.host
} for result in self.last_results
if result.mac == device), None)
return filter_att
def _update_info(self): def _update_info(self):
"""Ensure the information from the Luci router is up to date. """Ensure the information from the Luci router is up to date.
@ -114,7 +130,11 @@ class LuciDeviceScanner(DeviceScanner):
# Check if the Flags for each device contain # Check if the Flags for each device contain
# NUD_REACHABLE and if so, add it to last_results # NUD_REACHABLE and if so, add it to last_results
if int(device_entry['Flags'], 16) & 0x2: if int(device_entry['Flags'], 16) & 0x2:
self.last_results.append(device_entry['HW address']) self.last_results.append(Device(device_entry['HW address'],
device_entry['IP address'],
device_entry['Flags'],
device_entry['Device'],
self.host))
return True return True