diff --git a/homeassistant/components/ping/__init__.py b/homeassistant/components/ping/__init__.py index 12bad449f99..f4a04caae5b 100644 --- a/homeassistant/components/ping/__init__.py +++ b/homeassistant/components/ping/__init__.py @@ -19,7 +19,7 @@ from .helpers import PingDataICMPLib, PingDataSubProcess _LOGGER = logging.getLogger(__name__) -CONFIG_SCHEMA = cv.platform_only_config_schema(DOMAIN) +CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) PLATFORMS = [Platform.BINARY_SENSOR, Platform.DEVICE_TRACKER, Platform.SENSOR] diff --git a/homeassistant/components/ping/binary_sensor.py b/homeassistant/components/ping/binary_sensor.py index 2c26b460047..93f4e0f3896 100644 --- a/homeassistant/components/ping/binary_sensor.py +++ b/homeassistant/components/ping/binary_sensor.py @@ -2,78 +2,26 @@ from __future__ import annotations -import logging from typing import Any -import voluptuous as vol - from homeassistant.components.binary_sensor import ( - PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA, BinarySensorDeviceClass, BinarySensorEntity, ) -from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry -from homeassistant.const import CONF_HOST, CONF_NAME -from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant -import homeassistant.helpers.config_validation as cv +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue -from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from . import PingConfigEntry -from .const import CONF_IMPORTED_BY, CONF_PING_COUNT, DEFAULT_PING_COUNT, DOMAIN +from .const import CONF_IMPORTED_BY from .coordinator import PingUpdateCoordinator from .entity import PingEntity -_LOGGER = logging.getLogger(__name__) - ATTR_ROUND_TRIP_TIME_AVG = "round_trip_time_avg" ATTR_ROUND_TRIP_TIME_MAX = "round_trip_time_max" ATTR_ROUND_TRIP_TIME_MDEV = "round_trip_time_mdev" ATTR_ROUND_TRIP_TIME_MIN = "round_trip_time_min" -PLATFORM_SCHEMA = PARENT_PLATFORM_SCHEMA.extend( - { - vol.Required(CONF_HOST): cv.string, - vol.Optional(CONF_NAME): cv.string, - vol.Optional(CONF_PING_COUNT, default=DEFAULT_PING_COUNT): vol.Range( - min=1, max=100 - ), - } -) - - -async def async_setup_platform( - hass: HomeAssistant, - config: ConfigType, - async_add_entities: AddEntitiesCallback, - discovery_info: DiscoveryInfoType | None = None, -) -> None: - """YAML init: import via config flow.""" - - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_IMPORT}, - data={CONF_IMPORTED_BY: "binary_sensor", **config}, - ) - ) - - async_create_issue( - hass, - HOMEASSISTANT_DOMAIN, - f"deprecated_yaml_{DOMAIN}", - breaks_in_ha_version="2024.6.0", - is_fixable=False, - issue_domain=DOMAIN, - severity=IssueSeverity.WARNING, - translation_key="deprecated_yaml", - translation_placeholders={ - "domain": DOMAIN, - "integration_title": "Ping", - }, - ) - async def async_setup_entry( hass: HomeAssistant, entry: PingConfigEntry, async_add_entities: AddEntitiesCallback diff --git a/homeassistant/components/ping/config_flow.py b/homeassistant/components/ping/config_flow.py index 52600c379c4..9470b2134d4 100644 --- a/homeassistant/components/ping/config_flow.py +++ b/homeassistant/components/ping/config_flow.py @@ -2,7 +2,6 @@ from __future__ import annotations -from collections.abc import Mapping import logging from typing import Any @@ -18,12 +17,12 @@ from homeassistant.config_entries import ( ConfigFlowResult, OptionsFlow, ) -from homeassistant.const import CONF_HOST, CONF_NAME +from homeassistant.const import CONF_HOST from homeassistant.core import callback from homeassistant.helpers import selector from homeassistant.util.network import is_ip_address -from .const import CONF_IMPORTED_BY, CONF_PING_COUNT, DEFAULT_PING_COUNT, DOMAIN +from .const import CONF_PING_COUNT, DEFAULT_PING_COUNT, DOMAIN _LOGGER = logging.getLogger(__name__) @@ -61,27 +60,6 @@ class PingConfigFlow(ConfigFlow, domain=DOMAIN): }, ) - async def async_step_import( - self, import_info: Mapping[str, Any] - ) -> ConfigFlowResult: - """Import an entry.""" - - to_import = { - CONF_HOST: import_info[CONF_HOST], - CONF_PING_COUNT: import_info[CONF_PING_COUNT], - CONF_CONSIDER_HOME: import_info.get( - CONF_CONSIDER_HOME, DEFAULT_CONSIDER_HOME - ).seconds, - } - title = import_info.get(CONF_NAME, import_info[CONF_HOST]) - - self._async_abort_entries_match({CONF_HOST: to_import[CONF_HOST]}) - return self.async_create_entry( - title=title, - data={CONF_IMPORTED_BY: import_info[CONF_IMPORTED_BY]}, - options=to_import, - ) - @staticmethod @callback def async_get_options_flow( diff --git a/homeassistant/components/ping/device_tracker.py b/homeassistant/components/ping/device_tracker.py index bbbc336a423..ce7cc4522a0 100644 --- a/homeassistant/components/ping/device_tracker.py +++ b/homeassistant/components/ping/device_tracker.py @@ -3,126 +3,23 @@ from __future__ import annotations from datetime import datetime, timedelta -import logging -from typing import Any - -import voluptuous as vol from homeassistant.components.device_tracker import ( CONF_CONSIDER_HOME, DEFAULT_CONSIDER_HOME, - PLATFORM_SCHEMA as BASE_PLATFORM_SCHEMA, - AsyncSeeCallback, ScannerEntity, SourceType, ) -from homeassistant.components.device_tracker.legacy import ( - YAML_DEVICES, - remove_device_from_config, -) -from homeassistant.config import load_yaml_config_file -from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry -from homeassistant.const import ( - CONF_HOST, - CONF_HOSTS, - CONF_NAME, - EVENT_HOMEASSISTANT_STARTED, -) -from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, Event, HomeAssistant -from homeassistant.exceptions import HomeAssistantError -import homeassistant.helpers.config_validation as cv +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue -from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.util import dt as dt_util from . import PingConfigEntry -from .const import CONF_IMPORTED_BY, CONF_PING_COUNT, DOMAIN +from .const import CONF_IMPORTED_BY from .coordinator import PingUpdateCoordinator -_LOGGER = logging.getLogger(__name__) - -PLATFORM_SCHEMA = BASE_PLATFORM_SCHEMA.extend( - { - vol.Required(CONF_HOSTS): {cv.slug: cv.string}, - vol.Optional(CONF_PING_COUNT, default=1): cv.positive_int, - } -) - - -async def async_setup_scanner( - hass: HomeAssistant, - config: ConfigType, - async_see: AsyncSeeCallback, - discovery_info: DiscoveryInfoType | None = None, -) -> bool: - """Legacy init: import via config flow.""" - - async def _run_import(_: Event) -> None: - """Delete devices from known_device.yaml and import them via config flow.""" - _LOGGER.debug( - "Home Assistant successfully started, importing ping device tracker config entries now" - ) - - devices: dict[str, dict[str, Any]] = {} - try: - devices = await hass.async_add_executor_job( - load_yaml_config_file, hass.config.path(YAML_DEVICES) - ) - except (FileNotFoundError, HomeAssistantError): - _LOGGER.debug( - "No valid known_devices.yaml found, " - "skip removal of devices from known_devices.yaml" - ) - - for dev_name, dev_host in config[CONF_HOSTS].items(): - if dev_name in devices: - await hass.async_add_executor_job( - remove_device_from_config, hass, dev_name - ) - _LOGGER.debug("Removed device %s from known_devices.yaml", dev_name) - - if not hass.states.async_available(f"device_tracker.{dev_name}"): - hass.states.async_remove(f"device_tracker.{dev_name}") - - # run import after everything has been cleaned up - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_IMPORT}, - data={ - CONF_IMPORTED_BY: "device_tracker", - CONF_NAME: dev_name, - CONF_HOST: dev_host, - CONF_PING_COUNT: config[CONF_PING_COUNT], - CONF_CONSIDER_HOME: config[CONF_CONSIDER_HOME], - }, - ) - ) - - async_create_issue( - hass, - HOMEASSISTANT_DOMAIN, - f"deprecated_yaml_{DOMAIN}", - breaks_in_ha_version="2024.6.0", - is_fixable=False, - issue_domain=DOMAIN, - severity=IssueSeverity.WARNING, - translation_key="deprecated_yaml", - translation_placeholders={ - "domain": DOMAIN, - "integration_title": "Ping", - }, - ) - - # delay the import until after Home Assistant has started and everything has been initialized, - # as the legacy device tracker entities will be restored after the legacy device tracker platforms - # have been set up, so we can only remove the entities from the state machine then - hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, _run_import) - - return True - async def async_setup_entry( hass: HomeAssistant, entry: PingConfigEntry, async_add_entities: AddEntitiesCallback diff --git a/tests/components/ping/test_binary_sensor.py b/tests/components/ping/test_binary_sensor.py index ea3145af253..660b5ca31f1 100644 --- a/tests/components/ping/test_binary_sensor.py +++ b/tests/components/ping/test_binary_sensor.py @@ -10,8 +10,8 @@ from syrupy import SnapshotAssertion from syrupy.filters import props from homeassistant.components.ping.const import CONF_IMPORTED_BY, DOMAIN -from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant -from homeassistant.helpers import entity_registry as er, issue_registry as ir +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er from homeassistant.setup import async_setup_component from tests.common import MockConfigEntry @@ -64,29 +64,3 @@ async def test_disabled_after_import( assert entry assert entry.disabled assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION - - -async def test_import_issue_creation( - hass: HomeAssistant, - issue_registry: ir.IssueRegistry, -) -> None: - """Test if import issue is raised.""" - - await async_setup_component( - hass, - "binary_sensor", - { - "binary_sensor": { - "platform": "ping", - "name": "test", - "host": "127.0.0.1", - "count": 1, - } - }, - ) - await hass.async_block_till_done() - - issue = issue_registry.async_get_issue( - HOMEASSISTANT_DOMAIN, f"deprecated_yaml_{DOMAIN}" - ) - assert issue diff --git a/tests/components/ping/test_config_flow.py b/tests/components/ping/test_config_flow.py index 1f55957410d..8204a000f29 100644 --- a/tests/components/ping/test_config_flow.py +++ b/tests/components/ping/test_config_flow.py @@ -6,12 +6,9 @@ import pytest from homeassistant import config_entries from homeassistant.components.ping import DOMAIN -from homeassistant.components.ping.const import CONF_IMPORTED_BY from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType -from .const import BINARY_SENSOR_IMPORT_DATA - from tests.common import MockConfigEntry @@ -87,41 +84,3 @@ async def test_options(hass: HomeAssistant, host, count, expected_title) -> None "host": "10.10.10.1", "consider_home": 180, } - - -@pytest.mark.usefixtures("patch_setup") -async def test_step_import(hass: HomeAssistant) -> None: - """Test for import step.""" - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={CONF_IMPORTED_BY: "binary_sensor", **BINARY_SENSOR_IMPORT_DATA}, - ) - await hass.async_block_till_done() - - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "test2" - assert result["data"] == {CONF_IMPORTED_BY: "binary_sensor"} - assert result["options"] == { - "host": "127.0.0.1", - "count": 1, - "consider_home": 240, - } - - # test import without name - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={CONF_IMPORTED_BY: "binary_sensor", "host": "10.10.10.10", "count": 5}, - ) - await hass.async_block_till_done() - - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "10.10.10.10" - assert result["data"] == {CONF_IMPORTED_BY: "binary_sensor"} - assert result["options"] == { - "host": "10.10.10.10", - "count": 5, - "consider_home": 180, - } diff --git a/tests/components/ping/test_device_tracker.py b/tests/components/ping/test_device_tracker.py index f65f619b3c6..5aa425226b3 100644 --- a/tests/components/ping/test_device_tracker.py +++ b/tests/components/ping/test_device_tracker.py @@ -8,15 +8,10 @@ from icmplib import Host import pytest from typing_extensions import Generator -from homeassistant.components.device_tracker import legacy -from homeassistant.components.ping.const import DOMAIN -from homeassistant.const import EVENT_HOMEASSISTANT_STARTED -from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant -from homeassistant.helpers import entity_registry as er, issue_registry as ir -from homeassistant.setup import async_setup_component -from homeassistant.util.yaml import dump +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er -from tests.common import MockConfigEntry, async_fire_time_changed, patch_yaml_files +from tests.common import MockConfigEntry, async_fire_time_changed @pytest.fixture @@ -85,60 +80,6 @@ async def test_setup_and_update( assert state.state == "home" -async def test_import_issue_creation( - hass: HomeAssistant, - issue_registry: ir.IssueRegistry, -) -> None: - """Test if import issue is raised.""" - - await async_setup_component( - hass, - "device_tracker", - {"device_tracker": {"platform": "ping", "hosts": {"test": "10.10.10.10"}}}, - ) - await hass.async_block_till_done() - - hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) - await hass.async_block_till_done() - - issue = issue_registry.async_get_issue( - HOMEASSISTANT_DOMAIN, f"deprecated_yaml_{DOMAIN}" - ) - assert issue - - -async def test_import_delete_known_devices(hass: HomeAssistant) -> None: - """Test if import deletes known devices.""" - yaml_devices = { - "test": { - "hide_if_away": True, - "mac": "00:11:22:33:44:55", - "name": "Test name", - "picture": "/local/test.png", - "track": True, - }, - } - files = {legacy.YAML_DEVICES: dump(yaml_devices)} - - with ( - patch_yaml_files(files, True), - patch( - "homeassistant.components.ping.device_tracker.remove_device_from_config" - ) as remove_device_from_config, - ): - await async_setup_component( - hass, - "device_tracker", - {"device_tracker": {"platform": "ping", "hosts": {"test": "10.10.10.10"}}}, - ) - await hass.async_block_till_done() - - hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) - await hass.async_block_till_done() - - assert len(remove_device_from_config.mock_calls) == 1 - - @pytest.mark.usefixtures("entity_registry_enabled_by_default", "setup_integration") async def test_reload_not_triggering_home( hass: HomeAssistant,