Remove yaml import from downloader (#125921)

This commit is contained in:
G Johansson 2024-09-16 10:20:32 +02:00 committed by GitHub
parent e0d18c621b
commit db1349b95c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 5 additions and 207 deletions

View File

@ -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."""

View File

@ -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]

View File

@ -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."
}
}
}

View File

@ -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"

View File

@ -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