From db1349b95c87fc7c84a371372274d36d50c59677 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Mon, 16 Sep 2024 10:20:32 +0200 Subject: [PATCH] Remove yaml import from downloader (#125921) --- .../components/downloader/__init__.py | 72 +--------------- .../components/downloader/config_flow.py | 8 -- .../components/downloader/strings.json | 6 -- .../components/downloader/test_config_flow.py | 42 +--------- tests/components/downloader/test_init.py | 84 +------------------ 5 files changed, 5 insertions(+), 207 deletions(-) diff --git a/homeassistant/components/downloader/__init__.py b/homeassistant/components/downloader/__init__.py index 3fded1215c4..75e1103a712 100644 --- a/homeassistant/components/downloader/__init__.py +++ b/homeassistant/components/downloader/__init__.py @@ -10,17 +10,10 @@ import threading import requests import voluptuous as vol -from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry -from homeassistant.core import ( - DOMAIN as HOMEASSISTANT_DOMAIN, - HomeAssistant, - ServiceCall, -) -from homeassistant.data_entry_flow import FlowResultType +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant, ServiceCall import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue from homeassistant.helpers.service import async_register_admin_service -from homeassistant.helpers.typing import ConfigType from homeassistant.util import raise_if_invalid_filename, raise_if_invalid_path from .const import ( @@ -36,67 +29,6 @@ from .const import ( SERVICE_DOWNLOAD_FILE, ) -CONFIG_SCHEMA = vol.Schema( - {DOMAIN: vol.Schema({vol.Required(CONF_DOWNLOAD_DIR): cv.string})}, - extra=vol.ALLOW_EXTRA, -) - - -async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: - """Set up the Downloader component, via the YAML file.""" - if DOMAIN not in config: - return True - - hass.async_create_task(_async_import_config(hass, config)) - return True - - -async def _async_import_config(hass: HomeAssistant, config: ConfigType) -> None: - """Import the Downloader component from the YAML file.""" - - import_result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_IMPORT}, - data={ - CONF_DOWNLOAD_DIR: config[DOMAIN][CONF_DOWNLOAD_DIR], - }, - ) - - if ( - import_result["type"] == FlowResultType.ABORT - and import_result["reason"] != "single_instance_allowed" - ): - async_create_issue( - hass, - DOMAIN, - f"deprecated_yaml_{DOMAIN}", - breaks_in_ha_version="2024.10.0", - is_fixable=False, - issue_domain=DOMAIN, - severity=IssueSeverity.WARNING, - translation_key="directory_does_not_exist", - translation_placeholders={ - "domain": DOMAIN, - "integration_title": "Downloader", - "url": "/config/integrations/dashboard/add?domain=downloader", - }, - ) - else: - async_create_issue( - hass, - HOMEASSISTANT_DOMAIN, - f"deprecated_yaml_{DOMAIN}", - breaks_in_ha_version="2024.10.0", - is_fixable=False, - issue_domain=DOMAIN, - severity=IssueSeverity.WARNING, - translation_key="deprecated_yaml", - translation_placeholders={ - "domain": DOMAIN, - "integration_title": "Downloader", - }, - ) - async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Listen for download events to download files.""" diff --git a/homeassistant/components/downloader/config_flow.py b/homeassistant/components/downloader/config_flow.py index 61a7ba8fe52..3c3d6189f8a 100644 --- a/homeassistant/components/downloader/config_flow.py +++ b/homeassistant/components/downloader/config_flow.py @@ -43,14 +43,6 @@ class DownloaderConfigFlow(ConfigFlow, domain=DOMAIN): errors=errors, ) - async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: - """Handle a flow initiated by configuration file.""" - try: - await self._validate_input(import_data) - except DirectoryDoesNotExist: - return self.async_abort(reason="directory_does_not_exist") - return self.async_create_entry(title=DEFAULT_NAME, data=import_data) - async def _validate_input(self, user_input: dict[str, Any]) -> None: """Validate the user input if the directory exists.""" download_path = user_input[CONF_DOWNLOAD_DIR] diff --git a/homeassistant/components/downloader/strings.json b/homeassistant/components/downloader/strings.json index cf962bd9713..11a2bda8fce 100644 --- a/homeassistant/components/downloader/strings.json +++ b/homeassistant/components/downloader/strings.json @@ -35,11 +35,5 @@ } } } - }, - "issues": { - "directory_does_not_exist": { - "title": "The {integration_title} failed to import", - "description": "The {integration_title} integration failed to import because the configured directory does not exist.\n\nEnsure the directory exists and restart Home Assistant to try again or remove the {integration_title} configuration from your configuration.yaml file and continue to [set up the integration]({url}) manually." - } } } diff --git a/tests/components/downloader/test_config_flow.py b/tests/components/downloader/test_config_flow.py index 132b83dffdf..6bd740afab8 100644 --- a/tests/components/downloader/test_config_flow.py +++ b/tests/components/downloader/test_config_flow.py @@ -4,9 +4,8 @@ from unittest.mock import patch import pytest -from homeassistant import config_entries from homeassistant.components.downloader.const import CONF_DOWNLOAD_DIR, DOMAIN -from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -54,7 +53,7 @@ async def test_user_form(hass: HomeAssistant) -> None: assert result["data"] == {"download_dir": "download_dir"} -@pytest.mark.parametrize("source", [SOURCE_USER, SOURCE_IMPORT]) +@pytest.mark.parametrize("source", [SOURCE_USER]) async def test_single_instance_allowed( hass: HomeAssistant, source: str, @@ -69,40 +68,3 @@ async def test_single_instance_allowed( assert result["type"] is FlowResultType.ABORT assert result["reason"] == "single_instance_allowed" - - -async def test_import_flow_success(hass: HomeAssistant) -> None: - """Test import flow.""" - with ( - patch( - "homeassistant.components.downloader.async_setup_entry", return_value=True - ), - patch( - "os.path.isdir", - return_value=True, - ), - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data=CONFIG, - ) - await hass.async_block_till_done() - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "Downloader" - assert result["data"] == CONFIG - - -async def test_import_flow_directory_not_found(hass: HomeAssistant) -> None: - """Test import flow.""" - with patch("os.path.isdir", return_value=False): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={ - CONF_DOWNLOAD_DIR: "download_dir", - }, - ) - await hass.async_block_till_done() - assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "directory_does_not_exist" diff --git a/tests/components/downloader/test_init.py b/tests/components/downloader/test_init.py index 5832c0402b4..70dfd227019 100644 --- a/tests/components/downloader/test_init.py +++ b/tests/components/downloader/test_init.py @@ -8,9 +8,7 @@ from homeassistant.components.downloader import ( SERVICE_DOWNLOAD_FILE, ) from homeassistant.config_entries import ConfigEntryState -from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant -from homeassistant.helpers import issue_registry as ir -from homeassistant.setup import async_setup_component +from homeassistant.core import HomeAssistant from tests.common import MockConfigEntry @@ -29,83 +27,3 @@ async def test_initialization(hass: HomeAssistant) -> None: assert hass.services.has_service(DOMAIN, SERVICE_DOWNLOAD_FILE) assert config_entry.state is ConfigEntryState.LOADED - - -async def test_import(hass: HomeAssistant, issue_registry: ir.IssueRegistry) -> None: - """Test the import of the downloader component.""" - with patch("os.path.isdir", return_value=True): - assert await async_setup_component( - hass, - DOMAIN, - { - DOMAIN: { - CONF_DOWNLOAD_DIR: "/test_dir", - }, - }, - ) - await hass.async_block_till_done() - - assert len(hass.config_entries.async_entries(DOMAIN)) == 1 - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - assert config_entry.data == {CONF_DOWNLOAD_DIR: "/test_dir"} - assert config_entry.state is ConfigEntryState.LOADED - assert hass.services.has_service(DOMAIN, SERVICE_DOWNLOAD_FILE) - assert len(issue_registry.issues) == 1 - issue = issue_registry.async_get_issue( - issue_id="deprecated_yaml_downloader", domain=HOMEASSISTANT_DOMAIN - ) - assert issue - - -async def test_import_directory_missing( - hass: HomeAssistant, issue_registry: ir.IssueRegistry -) -> None: - """Test the import of the downloader component.""" - with patch("os.path.isdir", return_value=False): - assert await async_setup_component( - hass, - DOMAIN, - { - DOMAIN: { - CONF_DOWNLOAD_DIR: "/test_dir", - }, - }, - ) - await hass.async_block_till_done() - - assert len(hass.config_entries.async_entries(DOMAIN)) == 0 - assert len(issue_registry.issues) == 1 - issue = issue_registry.async_get_issue( - issue_id="deprecated_yaml_downloader", domain=DOMAIN - ) - assert issue - - -async def test_import_already_exists( - hass: HomeAssistant, issue_registry: ir.IssueRegistry -) -> None: - """Test the import of the downloader component.""" - config_entry = MockConfigEntry( - domain=DOMAIN, - data={ - CONF_DOWNLOAD_DIR: "/test_dir", - }, - ) - config_entry.add_to_hass(hass) - with patch("os.path.isdir", return_value=True): - assert await async_setup_component( - hass, - DOMAIN, - { - DOMAIN: { - CONF_DOWNLOAD_DIR: "/test_dir", - }, - }, - ) - await hass.async_block_till_done() - - assert len(issue_registry.issues) == 1 - issue = issue_registry.async_get_issue( - issue_id="deprecated_yaml_downloader", domain=HOMEASSISTANT_DOMAIN - ) - assert issue