mirror of
https://github.com/home-assistant/core.git
synced 2026-04-18 07:56:03 +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:
@@ -39,6 +39,8 @@ from .const import (
|
|||||||
TRACKER_SCAN_INTERVAL,
|
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'
|
# 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"
|
NMAP_TRANSIENT_FAILURE: Final = "Assertion failed: htn.toclock_running == true"
|
||||||
MAX_SCAN_ATTEMPTS: Final = 16
|
MAX_SCAN_ATTEMPTS: Final = 16
|
||||||
@@ -85,23 +87,25 @@ class NmapTrackedDevices:
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_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."""
|
"""Set up Nmap Tracker from a config entry."""
|
||||||
domain_data = hass.data.setdefault(DOMAIN, {})
|
domain_data = hass.data.setdefault(DOMAIN, {})
|
||||||
devices = domain_data.setdefault(NMAP_TRACKED_DEVICES, NmapTrackedDevices())
|
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()
|
await scanner.async_setup()
|
||||||
|
entry.runtime_data = scanner
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
return True
|
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 a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
|
|
||||||
if unload_ok:
|
if unload_ok:
|
||||||
_async_untrack_devices(hass, entry)
|
_async_untrack_devices(hass, entry)
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ from homeassistant.components.device_tracker import (
|
|||||||
)
|
)
|
||||||
from homeassistant.components.network import MDNS_TARGET_IP
|
from homeassistant.components.network import MDNS_TARGET_IP
|
||||||
from homeassistant.config_entries import (
|
from homeassistant.config_entries import (
|
||||||
ConfigEntry,
|
|
||||||
ConfigFlow,
|
ConfigFlow,
|
||||||
ConfigFlowResult,
|
ConfigFlowResult,
|
||||||
OptionsFlowWithReload,
|
OptionsFlowWithReload,
|
||||||
@@ -26,6 +25,7 @@ from homeassistant.helpers.device_registry import format_mac
|
|||||||
from homeassistant.helpers.selector import TextSelector, TextSelectorConfig
|
from homeassistant.helpers.selector import TextSelector, TextSelectorConfig
|
||||||
from homeassistant.helpers.typing import VolDictType
|
from homeassistant.helpers.typing import VolDictType
|
||||||
|
|
||||||
|
from . import NmapTrackerConfigEntry
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_HOME_INTERVAL,
|
CONF_HOME_INTERVAL,
|
||||||
CONF_HOSTS_EXCLUDE,
|
CONF_HOSTS_EXCLUDE,
|
||||||
@@ -184,7 +184,7 @@ async def _async_build_schema_with_user_input(
|
|||||||
class OptionsFlowHandler(OptionsFlowWithReload):
|
class OptionsFlowHandler(OptionsFlowWithReload):
|
||||||
"""Handle an option flow for nmap tracker."""
|
"""Handle an option flow for nmap tracker."""
|
||||||
|
|
||||||
def __init__(self, config_entry: ConfigEntry) -> None:
|
def __init__(self, config_entry: NmapTrackerConfigEntry) -> None:
|
||||||
"""Initialize options flow."""
|
"""Initialize options flow."""
|
||||||
self.options = dict(config_entry.options)
|
self.options = dict(config_entry.options)
|
||||||
|
|
||||||
@@ -259,6 +259,8 @@ class NmapTrackerConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@callback
|
@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."""
|
"""Get the options flow for this handler."""
|
||||||
return OptionsFlowHandler(config_entry)
|
return OptionsFlowHandler(config_entry)
|
||||||
|
|||||||
@@ -6,24 +6,28 @@ import logging
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.device_tracker import ScannerEntity
|
from homeassistant.components.device_tracker import ScannerEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
from . import NmapDevice, NmapDeviceScanner, short_hostname, signal_device_update
|
from . import (
|
||||||
from .const import DOMAIN
|
NmapDevice,
|
||||||
|
NmapDeviceScanner,
|
||||||
|
NmapTrackerConfigEntry,
|
||||||
|
short_hostname,
|
||||||
|
signal_device_update,
|
||||||
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: NmapTrackerConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up device tracker for Nmap Tracker component."""
|
"""Set up device tracker for Nmap Tracker component."""
|
||||||
nmap_tracker = hass.data[DOMAIN][entry.entry_id]
|
nmap_tracker = entry.runtime_data
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def device_new(mac_address):
|
def device_new(mac_address):
|
||||||
|
|||||||
Reference in New Issue
Block a user