Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attributes of ARP table #17987

Merged
merged 2 commits into from
Nov 6, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions homeassistant/components/device_tracker/luci.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import logging
import re
from collections import namedtuple

import requests
import voluptuous as vol
Expand Down Expand Up @@ -43,14 +44,17 @@ def get_scanner(hass, config):
return scanner if scanner.success_init else None


Device = namedtuple('Device', ['mac', 'ip', 'flags', 'device', 'host'])


class LuciDeviceScanner(DeviceScanner):
"""This class queries a wireless router running OpenWrt firmware."""

def __init__(self, config):
"""Initialize the scanner."""
host = config[CONF_HOST]
self.host = config[CONF_HOST]
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.password = config[CONF_PASSWORD]

Expand All @@ -68,7 +72,7 @@ def refresh_token(self):
def scan_devices(self):
"""Scan for new devices and return a list with found device IDs."""
self._update_info()
return self.last_results
return [device.mac for device in self.last_results]

def get_device_name(self, device):
"""Return the name of the given device or None if we don't know."""
Expand All @@ -88,6 +92,18 @@ def get_device_name(self, device):
return
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):
"""Ensure the information from the Luci router is up to date.

Expand All @@ -114,7 +130,11 @@ def _update_info(self):
# Check if the Flags for each device contain
# NUD_REACHABLE and if so, add it to last_results
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

Expand Down