mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Remove deprecated async_get_scanner from nmap_tracker (#63863)
Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
parent
9bcdc2b847
commit
8dcffc9993
@ -206,17 +206,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
return False
|
||||
return True
|
||||
|
||||
async def async_step_import(self, user_input: dict[str, Any]) -> FlowResult:
|
||||
"""Handle import from yaml."""
|
||||
if not self._async_is_unique_host_list(user_input):
|
||||
return self.async_abort(reason="already_configured")
|
||||
|
||||
normalize_input(user_input)
|
||||
|
||||
return self.async_create_entry(
|
||||
title=f"Nmap Tracker {user_input[CONF_HOSTS]}", data={}, options=user_input
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
@callback
|
||||
def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow:
|
||||
|
@ -4,89 +4,19 @@ from __future__ import annotations
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.device_tracker import (
|
||||
DOMAIN as DEVICE_TRACKER_DOMAIN,
|
||||
PLATFORM_SCHEMA as DEVICE_TRACKER_PLATFORM_SCHEMA,
|
||||
SOURCE_TYPE_ROUTER,
|
||||
)
|
||||
from homeassistant.components.device_tracker import SOURCE_TYPE_ROUTER
|
||||
from homeassistant.components.device_tracker.config_entry import ScannerEntity
|
||||
from homeassistant.components.device_tracker.const import (
|
||||
CONF_CONSIDER_HOME,
|
||||
CONF_SCAN_INTERVAL,
|
||||
DEFAULT_CONSIDER_HOME,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||
from homeassistant.const import CONF_EXCLUDE, CONF_HOSTS
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from . import NmapDevice, NmapDeviceScanner, short_hostname, signal_device_update
|
||||
from .const import (
|
||||
CONF_HOME_INTERVAL,
|
||||
CONF_OPTIONS,
|
||||
DEFAULT_OPTIONS,
|
||||
DOMAIN,
|
||||
TRACKER_SCAN_INTERVAL,
|
||||
)
|
||||
from .const import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
PLATFORM_SCHEMA = DEVICE_TRACKER_PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_HOSTS): cv.ensure_list,
|
||||
vol.Required(CONF_HOME_INTERVAL, default=0): cv.positive_int,
|
||||
vol.Required(
|
||||
CONF_CONSIDER_HOME, default=DEFAULT_CONSIDER_HOME.total_seconds()
|
||||
): cv.time_period,
|
||||
vol.Optional(CONF_EXCLUDE, default=[]): vol.All(cv.ensure_list, [cv.string]),
|
||||
vol.Optional(CONF_OPTIONS, default=DEFAULT_OPTIONS): cv.string,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_get_scanner(hass: HomeAssistant, config: ConfigType) -> None:
|
||||
"""Validate the configuration and return a Nmap scanner."""
|
||||
validated_config = config[DEVICE_TRACKER_DOMAIN]
|
||||
|
||||
if CONF_SCAN_INTERVAL in validated_config:
|
||||
scan_interval = validated_config[CONF_SCAN_INTERVAL].total_seconds()
|
||||
else:
|
||||
scan_interval = TRACKER_SCAN_INTERVAL
|
||||
|
||||
if CONF_CONSIDER_HOME in validated_config:
|
||||
consider_home = validated_config[CONF_CONSIDER_HOME].total_seconds()
|
||||
else:
|
||||
consider_home = DEFAULT_CONSIDER_HOME.total_seconds()
|
||||
|
||||
import_config = {
|
||||
CONF_HOSTS: ",".join(validated_config[CONF_HOSTS]),
|
||||
CONF_HOME_INTERVAL: validated_config[CONF_HOME_INTERVAL],
|
||||
CONF_CONSIDER_HOME: consider_home,
|
||||
CONF_EXCLUDE: ",".join(validated_config[CONF_EXCLUDE]),
|
||||
CONF_OPTIONS: validated_config[CONF_OPTIONS],
|
||||
CONF_SCAN_INTERVAL: scan_interval,
|
||||
}
|
||||
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data=import_config,
|
||||
)
|
||||
)
|
||||
|
||||
_LOGGER.warning(
|
||||
"Your Nmap Tracker configuration has been imported into the UI, "
|
||||
"please remove it from configuration.yaml. "
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
|
@ -241,70 +241,3 @@ async def test_options_flow(hass: HomeAssistant, mock_get_source_ip) -> None:
|
||||
CONF_SCAN_INTERVAL: 10,
|
||||
}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_import(hass: HomeAssistant, mock_get_source_ip) -> None:
|
||||
"""Test we can import from yaml."""
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nmap_tracker.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data={
|
||||
CONF_HOSTS: "1.2.3.4/20",
|
||||
CONF_HOME_INTERVAL: 3,
|
||||
CONF_CONSIDER_HOME: 500,
|
||||
CONF_OPTIONS: DEFAULT_OPTIONS,
|
||||
CONF_EXCLUDE: "4.4.4.4, 6.4.3.2",
|
||||
CONF_SCAN_INTERVAL: 2000,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["title"] == "Nmap Tracker 1.2.3.4/20"
|
||||
assert result["data"] == {}
|
||||
assert result["options"] == {
|
||||
CONF_HOSTS: "1.2.3.4/20",
|
||||
CONF_HOME_INTERVAL: 3,
|
||||
CONF_CONSIDER_HOME: 500,
|
||||
CONF_OPTIONS: DEFAULT_OPTIONS,
|
||||
CONF_EXCLUDE: "4.4.4.4,6.4.3.2",
|
||||
CONF_SCAN_INTERVAL: 2000,
|
||||
}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_import_aborts_if_matching(
|
||||
hass: HomeAssistant, mock_get_source_ip
|
||||
) -> None:
|
||||
"""Test we can import from yaml."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={},
|
||||
options={
|
||||
CONF_HOSTS: "192.168.0.0/20",
|
||||
CONF_HOME_INTERVAL: 3,
|
||||
CONF_OPTIONS: DEFAULT_OPTIONS,
|
||||
CONF_EXCLUDE: "4.4.4.4",
|
||||
},
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data={
|
||||
CONF_HOSTS: "192.168.0.0/20",
|
||||
CONF_HOME_INTERVAL: 3,
|
||||
CONF_OPTIONS: DEFAULT_OPTIONS,
|
||||
CONF_EXCLUDE: "4.4.4.4, 6.4.3.2",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "already_configured"
|
||||
|
Loading…
x
Reference in New Issue
Block a user