Skip to content

Commit

Permalink
Switch nmap_tracker to use aiooui (#111415)
Browse files Browse the repository at this point in the history
mac-vendor-lookup can take a long time to startup because
it has to fetch the oui db from the web
  • Loading branch information
bdraco authored Feb 26, 2024
1 parent 9420ac1 commit 93cc6e0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 28 deletions.
22 changes: 5 additions & 17 deletions homeassistant/components/nmap_tracker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
from __future__ import annotations

import asyncio
import contextlib
from dataclasses import dataclass
from datetime import datetime, timedelta
from functools import partial
import logging
from typing import Final

import aiohttp
import aiooui
from getmac import get_mac_address
from mac_vendor_lookup import AsyncMacLookup
from nmap import PortScanner, PortScannerError

from homeassistant.components.device_tracker import (
Expand Down Expand Up @@ -158,7 +156,6 @@ def __init__(
self._known_mac_addresses: dict[str, str] = {}
self._finished_first_scan = False
self._last_results: list[NmapDevice] = []
self._mac_vendor_lookup = None

async def async_setup(self):
"""Set up the tracker."""
Expand Down Expand Up @@ -206,12 +203,6 @@ def signal_device_missing(self) -> str:
"""Signal specific per nmap tracker entry to signal a missing device."""
return f"{DOMAIN}-device-missing-{self._entry_id}"

@callback
def _async_get_vendor(self, mac_address):
"""Lookup the vendor."""
oui = self._mac_vendor_lookup.sanitise(mac_address)[:6]
return self._mac_vendor_lookup.prefixes.get(oui)

@callback
def _async_stop(self):
"""Stop the scanner."""
Expand All @@ -227,11 +218,8 @@ async def _async_start_scanner(self, *_):
self._scan_interval,
)
)
self._mac_vendor_lookup = AsyncMacLookup()
with contextlib.suppress((TimeoutError, aiohttp.ClientError)):
# We don't care if this fails since it only
# improves the data when we don't have it from nmap
await self._mac_vendor_lookup.load_vendors()
if not aiooui.is_loaded():
await aiooui.async_load()
self._hass.async_create_task(self._async_scan_devices())

def _build_options(self):
Expand Down Expand Up @@ -293,7 +281,7 @@ async def _async_mark_missing_devices_as_not_home(self):
None,
original_name,
None,
self._async_get_vendor(mac_address),
aiooui.get_vendor(mac_address),
"Device not found in initial scan",
now,
1,
Expand Down Expand Up @@ -402,7 +390,7 @@ async def _async_run_nmap_scan(self):
continue

hostname = info["hostnames"][0]["name"] if info["hostnames"] else ipv4
vendor = info.get("vendor", {}).get(mac) or self._async_get_vendor(mac)
vendor = info.get("vendor", {}).get(mac) or aiooui.get_vendor(mac)
name = human_readable_name(hostname, vendor, mac)
device = NmapDevice(
formatted_mac, hostname, name, ipv4, vendor, reason, now, None
Expand Down
6 changes: 1 addition & 5 deletions homeassistant/components/nmap_tracker/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,5 @@
"documentation": "https://www.home-assistant.io/integrations/nmap_tracker",
"iot_class": "local_polling",
"loggers": ["nmap"],
"requirements": [
"netmap==0.7.0.2",
"getmac==0.9.4",
"mac-vendor-lookup==0.1.12"
]
"requirements": ["netmap==0.7.0.2", "getmac==0.9.4", "aiooui==0.1.5"]
}
6 changes: 3 additions & 3 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ aiooncue==0.3.5
# homeassistant.components.openexchangerates
aioopenexchangerates==0.4.0

# homeassistant.components.nmap_tracker
aiooui==0.1.5

# homeassistant.components.pegel_online
aiopegelonline==0.0.9

Expand Down Expand Up @@ -1264,9 +1267,6 @@ lw12==0.9.2
# homeassistant.components.scrape
lxml==5.1.0

# homeassistant.components.nmap_tracker
mac-vendor-lookup==0.1.12

# homeassistant.components.matrix
matrix-nio==0.24.0

Expand Down
6 changes: 3 additions & 3 deletions requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ aiooncue==0.3.5
# homeassistant.components.openexchangerates
aioopenexchangerates==0.4.0

# homeassistant.components.nmap_tracker
aiooui==0.1.5

# homeassistant.components.pegel_online
aiopegelonline==0.0.9

Expand Down Expand Up @@ -1006,9 +1009,6 @@ lupupy==0.3.2
# homeassistant.components.scrape
lxml==5.1.0

# homeassistant.components.nmap_tracker
mac-vendor-lookup==0.1.12

# homeassistant.components.matrix
matrix-nio==0.24.0

Expand Down

0 comments on commit 93cc6e0

Please sign in to comment.