1
0
mirror of https://github.com/home-assistant/core.git synced 2026-04-02 00:20:30 +01:00

Migrate nmap_tracker to use runtime_data (#166932)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
epenet
2026-03-31 11:11:12 +02:00
committed by GitHub
parent 9bfac71bd7
commit 751f06eb58
3 changed files with 22 additions and 12 deletions

View File

@@ -39,6 +39,8 @@ from .const import (
TRACKER_SCAN_INTERVAL,
)
type NmapTrackerConfigEntry = ConfigEntry[NmapDeviceScanner]
# Some version of nmap will fail with 'Assertion failed: htn.toclock_running == true (Target.cc: stopTimeOutClock: 503)\n'
NMAP_TRANSIENT_FAILURE: Final = "Assertion failed: htn.toclock_running == true"
MAX_SCAN_ATTEMPTS: Final = 16
@@ -85,23 +87,25 @@ class NmapTrackedDevices:
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: NmapTrackerConfigEntry) -> bool:
"""Set up Nmap Tracker from a config entry."""
domain_data = hass.data.setdefault(DOMAIN, {})
devices = domain_data.setdefault(NMAP_TRACKED_DEVICES, NmapTrackedDevices())
scanner = domain_data[entry.entry_id] = NmapDeviceScanner(hass, entry, devices)
scanner = NmapDeviceScanner(hass, entry, devices)
await scanner.async_setup()
entry.runtime_data = scanner
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(
hass: HomeAssistant, entry: NmapTrackerConfigEntry
) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
_async_untrack_devices(hass, entry)
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok

View File

@@ -16,7 +16,6 @@ from homeassistant.components.device_tracker import (
)
from homeassistant.components.network import MDNS_TARGET_IP
from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
OptionsFlowWithReload,
@@ -26,6 +25,7 @@ from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.selector import TextSelector, TextSelectorConfig
from homeassistant.helpers.typing import VolDictType
from . import NmapTrackerConfigEntry
from .const import (
CONF_HOME_INTERVAL,
CONF_HOSTS_EXCLUDE,
@@ -184,7 +184,7 @@ async def _async_build_schema_with_user_input(
class OptionsFlowHandler(OptionsFlowWithReload):
"""Handle an option flow for nmap tracker."""
def __init__(self, config_entry: ConfigEntry) -> None:
def __init__(self, config_entry: NmapTrackerConfigEntry) -> None:
"""Initialize options flow."""
self.options = dict(config_entry.options)
@@ -259,6 +259,8 @@ class NmapTrackerConfigFlow(ConfigFlow, domain=DOMAIN):
@staticmethod
@callback
def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlowHandler:
def async_get_options_flow(
config_entry: NmapTrackerConfigEntry,
) -> OptionsFlowHandler:
"""Get the options flow for this handler."""
return OptionsFlowHandler(config_entry)

View File

@@ -6,24 +6,28 @@ import logging
from typing import Any
from homeassistant.components.device_tracker import ScannerEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import NmapDevice, NmapDeviceScanner, short_hostname, signal_device_update
from .const import DOMAIN
from . import (
NmapDevice,
NmapDeviceScanner,
NmapTrackerConfigEntry,
short_hostname,
signal_device_update,
)
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: NmapTrackerConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up device tracker for Nmap Tracker component."""
nmap_tracker = hass.data[DOMAIN][entry.entry_id]
nmap_tracker = entry.runtime_data
@callback
def device_new(mac_address):