Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek committed Apr 5, 2024
1 parent 9b2e498 commit b87db00
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
13 changes: 10 additions & 3 deletions homeassistant/components/downloader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from http import HTTPStatus
import os
import re
import threading

import requests
import voluptuous as vol
Expand Down Expand Up @@ -42,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},
Expand Down Expand Up @@ -71,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:
Expand All @@ -88,7 +95,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
)
return False

async def download_file(service: ServiceCall) -> None:
def download_file(service: ServiceCall) -> None:
"""Start thread to download file specified in the URL."""

def do_download() -> None:
Expand Down Expand Up @@ -194,7 +201,7 @@ def do_download() -> None:
if final_path and os.path.isfile(final_path):
os.remove(final_path)

await hass.async_add_executor_job(do_download)
threading.Thread(target=do_download).start()

async_register_admin_service(
hass,
Expand Down
22 changes: 22 additions & 0 deletions tests/components/downloader/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
)
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

from tests.common import MockConfigEntry

Expand All @@ -27,3 +28,24 @@ 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) -> 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)

0 comments on commit b87db00

Please sign in to comment.