Switch nmap_tracker to use aiooui (#111415)

mac-vendor-lookup can take a long time to startup because
it has to fetch the oui db from the web
This commit is contained in:
J. Nick Koston 2024-02-26 06:32:53 -10:00 committed by GitHub
parent 9420ac1cff
commit 93cc6e0f36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 12 additions and 28 deletions

View File

@ -2,16 +2,14 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
import contextlib
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime, timedelta from datetime import datetime, timedelta
from functools import partial from functools import partial
import logging import logging
from typing import Final from typing import Final
import aiohttp import aiooui
from getmac import get_mac_address from getmac import get_mac_address
from mac_vendor_lookup import AsyncMacLookup
from nmap import PortScanner, PortScannerError from nmap import PortScanner, PortScannerError
from homeassistant.components.device_tracker import ( from homeassistant.components.device_tracker import (
@ -158,7 +156,6 @@ class NmapDeviceScanner:
self._known_mac_addresses: dict[str, str] = {} self._known_mac_addresses: dict[str, str] = {}
self._finished_first_scan = False self._finished_first_scan = False
self._last_results: list[NmapDevice] = [] self._last_results: list[NmapDevice] = []
self._mac_vendor_lookup = None
async def async_setup(self): async def async_setup(self):
"""Set up the tracker.""" """Set up the tracker."""
@ -206,12 +203,6 @@ class NmapDeviceScanner:
"""Signal specific per nmap tracker entry to signal a missing device.""" """Signal specific per nmap tracker entry to signal a missing device."""
return f"{DOMAIN}-device-missing-{self._entry_id}" 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 @callback
def _async_stop(self): def _async_stop(self):
"""Stop the scanner.""" """Stop the scanner."""
@ -227,11 +218,8 @@ class NmapDeviceScanner:
self._scan_interval, self._scan_interval,
) )
) )
self._mac_vendor_lookup = AsyncMacLookup() if not aiooui.is_loaded():
with contextlib.suppress((TimeoutError, aiohttp.ClientError)): await aiooui.async_load()
# 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()
self._hass.async_create_task(self._async_scan_devices()) self._hass.async_create_task(self._async_scan_devices())
def _build_options(self): def _build_options(self):
@ -293,7 +281,7 @@ class NmapDeviceScanner:
None, None,
original_name, original_name,
None, None,
self._async_get_vendor(mac_address), aiooui.get_vendor(mac_address),
"Device not found in initial scan", "Device not found in initial scan",
now, now,
1, 1,
@ -402,7 +390,7 @@ class NmapDeviceScanner:
continue continue
hostname = info["hostnames"][0]["name"] if info["hostnames"] else ipv4 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) name = human_readable_name(hostname, vendor, mac)
device = NmapDevice( device = NmapDevice(
formatted_mac, hostname, name, ipv4, vendor, reason, now, None formatted_mac, hostname, name, ipv4, vendor, reason, now, None

View File

@ -7,9 +7,5 @@
"documentation": "https://www.home-assistant.io/integrations/nmap_tracker", "documentation": "https://www.home-assistant.io/integrations/nmap_tracker",
"iot_class": "local_polling", "iot_class": "local_polling",
"loggers": ["nmap"], "loggers": ["nmap"],
"requirements": [ "requirements": ["netmap==0.7.0.2", "getmac==0.9.4", "aiooui==0.1.5"]
"netmap==0.7.0.2",
"getmac==0.9.4",
"mac-vendor-lookup==0.1.12"
]
} }

View File

@ -323,6 +323,9 @@ aiooncue==0.3.5
# homeassistant.components.openexchangerates # homeassistant.components.openexchangerates
aioopenexchangerates==0.4.0 aioopenexchangerates==0.4.0
# homeassistant.components.nmap_tracker
aiooui==0.1.5
# homeassistant.components.pegel_online # homeassistant.components.pegel_online
aiopegelonline==0.0.9 aiopegelonline==0.0.9
@ -1264,9 +1267,6 @@ lw12==0.9.2
# homeassistant.components.scrape # homeassistant.components.scrape
lxml==5.1.0 lxml==5.1.0
# homeassistant.components.nmap_tracker
mac-vendor-lookup==0.1.12
# homeassistant.components.matrix # homeassistant.components.matrix
matrix-nio==0.24.0 matrix-nio==0.24.0

View File

@ -296,6 +296,9 @@ aiooncue==0.3.5
# homeassistant.components.openexchangerates # homeassistant.components.openexchangerates
aioopenexchangerates==0.4.0 aioopenexchangerates==0.4.0
# homeassistant.components.nmap_tracker
aiooui==0.1.5
# homeassistant.components.pegel_online # homeassistant.components.pegel_online
aiopegelonline==0.0.9 aiopegelonline==0.0.9
@ -1006,9 +1009,6 @@ lupupy==0.3.2
# homeassistant.components.scrape # homeassistant.components.scrape
lxml==5.1.0 lxml==5.1.0
# homeassistant.components.nmap_tracker
mac-vendor-lookup==0.1.12
# homeassistant.components.matrix # homeassistant.components.matrix
matrix-nio==0.24.0 matrix-nio==0.24.0