Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -9,26 +9,29 @@ import voluptuous as vol
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.components.device_tracker import (
|
||||
DOMAIN, PLATFORM_SCHEMA, DeviceScanner)
|
||||
DOMAIN,
|
||||
PLATFORM_SCHEMA,
|
||||
DeviceScanner,
|
||||
)
|
||||
from homeassistant.const import CONF_HOSTS
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_EXCLUDE = 'exclude'
|
||||
CONF_EXCLUDE = "exclude"
|
||||
# Interval in minutes to exclude devices from a scan while they are home
|
||||
CONF_HOME_INTERVAL = 'home_interval'
|
||||
CONF_OPTIONS = 'scan_options'
|
||||
DEFAULT_OPTIONS = '-F --host-timeout 5s'
|
||||
CONF_HOME_INTERVAL = "home_interval"
|
||||
CONF_OPTIONS = "scan_options"
|
||||
DEFAULT_OPTIONS = "-F --host-timeout 5s"
|
||||
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_HOSTS): cv.ensure_list,
|
||||
vol.Required(CONF_HOME_INTERVAL, default=0): cv.positive_int,
|
||||
vol.Optional(CONF_EXCLUDE, default=[]):
|
||||
vol.All(cv.ensure_list, [cv.string]),
|
||||
vol.Optional(CONF_OPTIONS, default=DEFAULT_OPTIONS):
|
||||
cv.string
|
||||
})
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_HOSTS): cv.ensure_list,
|
||||
vol.Required(CONF_HOME_INTERVAL, default=0): cv.positive_int,
|
||||
vol.Optional(CONF_EXCLUDE, default=[]): vol.All(cv.ensure_list, [cv.string]),
|
||||
vol.Optional(CONF_OPTIONS, default=DEFAULT_OPTIONS): cv.string,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def get_scanner(hass, config):
|
||||
@@ -36,7 +39,7 @@ def get_scanner(hass, config):
|
||||
return NmapDeviceScanner(config[DOMAIN])
|
||||
|
||||
|
||||
Device = namedtuple('Device', ['mac', 'name', 'ip', 'last_update'])
|
||||
Device = namedtuple("Device", ["mac", "name", "ip", "last_update"])
|
||||
|
||||
|
||||
class NmapDeviceScanner(DeviceScanner):
|
||||
@@ -66,8 +69,9 @@ class NmapDeviceScanner(DeviceScanner):
|
||||
|
||||
def get_device_name(self, device):
|
||||
"""Return the name of the given device or None if we don't know."""
|
||||
filter_named = [result.name for result in self.last_results
|
||||
if result.mac == device]
|
||||
filter_named = [
|
||||
result.name for result in self.last_results if result.mac == device
|
||||
]
|
||||
|
||||
if filter_named:
|
||||
return filter_named[0]
|
||||
@@ -75,10 +79,10 @@ class NmapDeviceScanner(DeviceScanner):
|
||||
|
||||
def get_extra_attributes(self, device):
|
||||
"""Return the IP of the given device."""
|
||||
filter_ip = next((
|
||||
result.ip for result in self.last_results
|
||||
if result.mac == device), None)
|
||||
return {'ip': filter_ip}
|
||||
filter_ip = next(
|
||||
(result.ip for result in self.last_results if result.mac == device), None
|
||||
)
|
||||
return {"ip": filter_ip}
|
||||
|
||||
def _update_info(self):
|
||||
"""Scan the network for devices.
|
||||
@@ -88,40 +92,40 @@ class NmapDeviceScanner(DeviceScanner):
|
||||
_LOGGER.debug("Scanning...")
|
||||
|
||||
from nmap import PortScanner, PortScannerError
|
||||
|
||||
scanner = PortScanner()
|
||||
|
||||
options = self._options
|
||||
|
||||
if self.home_interval:
|
||||
boundary = dt_util.now() - self.home_interval
|
||||
last_results = [device for device in self.last_results
|
||||
if device.last_update > boundary]
|
||||
last_results = [
|
||||
device for device in self.last_results if device.last_update > boundary
|
||||
]
|
||||
if last_results:
|
||||
exclude_hosts = self.exclude + [device.ip for device
|
||||
in last_results]
|
||||
exclude_hosts = self.exclude + [device.ip for device in last_results]
|
||||
else:
|
||||
exclude_hosts = self.exclude
|
||||
else:
|
||||
last_results = []
|
||||
exclude_hosts = self.exclude
|
||||
if exclude_hosts:
|
||||
options += ' --exclude {}'.format(','.join(exclude_hosts))
|
||||
options += " --exclude {}".format(",".join(exclude_hosts))
|
||||
|
||||
try:
|
||||
result = scanner.scan(hosts=' '.join(self.hosts),
|
||||
arguments=options)
|
||||
result = scanner.scan(hosts=" ".join(self.hosts), arguments=options)
|
||||
except PortScannerError:
|
||||
return False
|
||||
|
||||
now = dt_util.now()
|
||||
for ipv4, info in result['scan'].items():
|
||||
if info['status']['state'] != 'up':
|
||||
for ipv4, info in result["scan"].items():
|
||||
if info["status"]["state"] != "up":
|
||||
continue
|
||||
name = info['hostnames'][0]['name'] if info['hostnames'] else ipv4
|
||||
name = info["hostnames"][0]["name"] if info["hostnames"] else ipv4
|
||||
# Mac address only returned if nmap ran as root
|
||||
mac = info['addresses'].get('mac') or get_mac_address(ip=ipv4)
|
||||
mac = info["addresses"].get("mac") or get_mac_address(ip=ipv4)
|
||||
if mac is None:
|
||||
_LOGGER.info('No MAC address found for %s', ipv4)
|
||||
_LOGGER.info("No MAC address found for %s", ipv4)
|
||||
continue
|
||||
last_results.append(Device(mac.upper(), name, ipv4, now))
|
||||
|
||||
|
||||
Reference in new issue
Block a user