From 58533f02af7c612abdc51ce7ebcda20093ffe267 Mon Sep 17 00:00:00 2001
From: Joost Lekkerkerker <joostlek@outlook.com>
Date: Fri, 5 Apr 2024 11:32:59 +0200
Subject: [PATCH] Fix Downloader YAML import (#114844)

---
 .../components/downloader/__init__.py         | 10 +++-
 tests/components/downloader/test_init.py      | 51 +++++++++++++++++++
 2 files changed, 59 insertions(+), 2 deletions(-)
 create mode 100644 tests/components/downloader/test_init.py

diff --git a/homeassistant/components/downloader/__init__.py b/homeassistant/components/downloader/__init__.py
index 3ca503a2167..d110c28785a 100644
--- a/homeassistant/components/downloader/__init__.py
+++ b/homeassistant/components/downloader/__init__.py
@@ -43,6 +43,13 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
     if DOMAIN not in config:
         return True
 
+    hass.async_create_task(_async_import_config(hass, config), eager_start=True)
+    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},
@@ -62,7 +69,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
         hass,
         DOMAIN,
         f"deprecated_yaml_{DOMAIN}",
-        breaks_in_ha_version="2024.9.0",
+        breaks_in_ha_version="2024.10.0",
         is_fixable=False,
         issue_domain=DOMAIN,
         severity=IssueSeverity.WARNING,
@@ -72,7 +79,6 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
             "integration_title": "Downloader",
         },
     )
-    return True
 
 
 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
diff --git a/tests/components/downloader/test_init.py b/tests/components/downloader/test_init.py
new file mode 100644
index 00000000000..8cd0d00b1ab
--- /dev/null
+++ b/tests/components/downloader/test_init.py
@@ -0,0 +1,51 @@
+"""Tests for the downloader component init."""
+
+from unittest.mock import patch
+
+from homeassistant.components.downloader import (
+    CONF_DOWNLOAD_DIR,
+    DOMAIN,
+    SERVICE_DOWNLOAD_FILE,
+)
+from homeassistant.config_entries import ConfigEntryState
+from homeassistant.core import HomeAssistant
+from homeassistant.setup import async_setup_component
+
+from tests.common import MockConfigEntry
+
+
+async def test_initialization(hass: HomeAssistant) -> None:
+    """Test the initialization 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 hass.config_entries.async_setup(config_entry.entry_id)
+
+    assert hass.services.has_service(DOMAIN, SERVICE_DOWNLOAD_FILE)
+    assert config_entry.state is ConfigEntryState.LOADED
+
+
+async def test_import(hass: HomeAssistant) -> 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)