Add config flow to nzbget (#38938)
* work on config flow * Update test_init.py * work on config flow * Update test_config_flow.py * Update test_config_flow.py * Update __init__.py * Update test_config_flow.py * Update __init__.py * Update test_config_flow.py * Update test_config_flow.py * Update test_config_flow.py * Update test_config_flow.py * Update test_config_flow.py * Update test_config_flow.py * Update __init__.py * Update __init__.py * Update __init__.py * Apply suggestions from code review Co-authored-by: J. Nick Koston <nick@koston.org> * Update __init__.py * Update __init__.py * Update __init__.py * Update config_flow.py * Update __init__.py * Update __init__.py * Create coordinator.py * Update __init__.py * Update sensor.py * Update __init__.py * Update .coveragerc * Update coordinator.py * Update __init__.py * Update coordinator.py * Update __init__.py * Update coordinator.py * Update config_flow.py * Update __init__.py * Update coordinator.py * Update __init__.py * Update test_config_flow.py * Update coordinator.py * Update test_config_flow.py * Update test_init.py * Update homeassistant/components/nzbget/coordinator.py * Update test_config_flow.py Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
1 parent
54ef16f01a
commit
7469f57a7b
13 files changed
+852
-212
No files matched your search
@@ -0,0 +1,119 @@
|
||||
"""Tests for the NZBGet integration."""
|
||||
from datetime import timedelta
|
||||
|
||||
from homeassistant.components.nzbget.const import DOMAIN
|
||||
from homeassistant.const import (
|
||||
CONF_HOST,
|
||||
CONF_NAME,
|
||||
CONF_PASSWORD,
|
||||
CONF_PORT,
|
||||
CONF_SCAN_INTERVAL,
|
||||
CONF_SSL,
|
||||
CONF_USERNAME,
|
||||
CONF_VERIFY_SSL,
|
||||
)
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
ENTRY_CONFIG = {
|
||||
CONF_HOST: "10.10.10.30",
|
||||
CONF_NAME: "NZBGetTest",
|
||||
CONF_PASSWORD: "",
|
||||
CONF_PORT: 6789,
|
||||
CONF_SSL: False,
|
||||
CONF_USERNAME: "",
|
||||
CONF_VERIFY_SSL: False,
|
||||
}
|
||||
|
||||
USER_INPUT = {
|
||||
CONF_HOST: "10.10.10.30",
|
||||
CONF_NAME: "NZBGet",
|
||||
CONF_PASSWORD: "",
|
||||
CONF_PORT: 6789,
|
||||
CONF_SSL: False,
|
||||
CONF_USERNAME: "",
|
||||
}
|
||||
|
||||
YAML_CONFIG = {
|
||||
CONF_HOST: "10.10.10.30",
|
||||
CONF_NAME: "GetNZBsTest",
|
||||
CONF_PASSWORD: "",
|
||||
CONF_PORT: 6789,
|
||||
CONF_SCAN_INTERVAL: timedelta(seconds=5),
|
||||
CONF_SSL: False,
|
||||
CONF_USERNAME: "",
|
||||
}
|
||||
|
||||
MOCK_VERSION = "21.0"
|
||||
|
||||
MOCK_STATUS = {
|
||||
"ArticleCacheMB": "64",
|
||||
"AverageDownloadRate": "512",
|
||||
"DownloadPaused": "4",
|
||||
"DownloadRate": "1000",
|
||||
"DownloadedSizeMB": "256",
|
||||
"FreeDiskSpaceMB": "1024",
|
||||
"PostJobCount": "2",
|
||||
"PostPaused": "4",
|
||||
"RemainingSizeMB": "512",
|
||||
"UpTimeSec": "600",
|
||||
}
|
||||
|
||||
MOCK_HISTORY = [
|
||||
{"Name": "Downloaded Item XYZ", "Category": "", "Status": "SUCCESS"},
|
||||
{"Name": "Failed Item ABC", "Category": "", "Status": "FAILURE"},
|
||||
]
|
||||
|
||||
|
||||
async def init_integration(
|
||||
hass,
|
||||
*,
|
||||
status: dict = MOCK_STATUS,
|
||||
history: dict = MOCK_HISTORY,
|
||||
version: str = MOCK_VERSION,
|
||||
) -> MockConfigEntry:
|
||||
"""Set up the NZBGet integration in Home Assistant."""
|
||||
entry = MockConfigEntry(domain=DOMAIN, data=ENTRY_CONFIG)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with _patch_version(version), _patch_status(status), _patch_history(history):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return entry
|
||||
|
||||
|
||||
def _patch_async_setup(return_value=True):
|
||||
return patch(
|
||||
"homeassistant.components.nzbget.async_setup",
|
||||
return_value=return_value,
|
||||
)
|
||||
|
||||
|
||||
def _patch_async_setup_entry(return_value=True):
|
||||
return patch(
|
||||
"homeassistant.components.nzbget.async_setup_entry",
|
||||
return_value=return_value,
|
||||
)
|
||||
|
||||
|
||||
def _patch_history(return_value=MOCK_HISTORY):
|
||||
return patch(
|
||||
"homeassistant.components.nzbget.coordinator.NZBGetAPI.history",
|
||||
return_value=return_value,
|
||||
)
|
||||
|
||||
|
||||
def _patch_status(return_value=MOCK_STATUS):
|
||||
return patch(
|
||||
"homeassistant.components.nzbget.coordinator.NZBGetAPI.status",
|
||||
return_value=return_value,
|
||||
)
|
||||
|
||||
|
||||
def _patch_version(return_value=MOCK_VERSION):
|
||||
return patch(
|
||||
"homeassistant.components.nzbget.coordinator.NZBGetAPI.version",
|
||||
return_value=return_value,
|
||||
)
|
||||
@@ -0,0 +1,156 @@
|
||||
"""Test the NZBGet config flow."""
|
||||
from pynzbgetapi import NZBGetAPIException
|
||||
|
||||
from homeassistant.components.nzbget.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_SCAN_INTERVAL, CONF_VERIFY_SSL
|
||||
from homeassistant.data_entry_flow import (
|
||||
RESULT_TYPE_ABORT,
|
||||
RESULT_TYPE_CREATE_ENTRY,
|
||||
RESULT_TYPE_FORM,
|
||||
)
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from . import (
|
||||
ENTRY_CONFIG,
|
||||
USER_INPUT,
|
||||
_patch_async_setup,
|
||||
_patch_async_setup_entry,
|
||||
_patch_history,
|
||||
_patch_status,
|
||||
_patch_version,
|
||||
)
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_user_form(hass):
|
||||
"""Test we get the user initiated form."""
|
||||
await async_setup_component(hass, "persistent_notification", {})
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
with _patch_version(), _patch_status(), _patch_history(), _patch_async_setup() as mock_setup, _patch_async_setup_entry() as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
USER_INPUT,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == "10.10.10.30"
|
||||
assert result["data"] == {**USER_INPUT, CONF_VERIFY_SSL: False}
|
||||
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_user_form_show_advanced_options(hass):
|
||||
"""Test we get the user initiated form with advanced options shown."""
|
||||
await async_setup_component(hass, "persistent_notification", {})
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER, "show_advanced_options": True}
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
user_input_advanced = {
|
||||
**USER_INPUT,
|
||||
CONF_VERIFY_SSL: True,
|
||||
}
|
||||
|
||||
with _patch_version(), _patch_status(), _patch_history(), _patch_async_setup() as mock_setup, _patch_async_setup_entry() as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input_advanced,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == "10.10.10.30"
|
||||
assert result["data"] == {**USER_INPUT, CONF_VERIFY_SSL: True}
|
||||
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_user_form_cannot_connect(hass):
|
||||
"""Test we handle cannot connect error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nzbget.coordinator.NZBGetAPI.version",
|
||||
side_effect=NZBGetAPIException(),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
USER_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_user_form_unexpected_exception(hass):
|
||||
"""Test we handle unexpected exception."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nzbget.coordinator.NZBGetAPI.version",
|
||||
side_effect=Exception(),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
USER_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "unknown"
|
||||
|
||||
|
||||
async def test_user_form_single_instance_allowed(hass):
|
||||
"""Test that configuring more than one instance is rejected."""
|
||||
entry = MockConfigEntry(domain=DOMAIN, data=ENTRY_CONFIG)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=USER_INPUT,
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "single_instance_allowed"
|
||||
|
||||
|
||||
async def test_options_flow(hass):
|
||||
"""Test updating options."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=ENTRY_CONFIG,
|
||||
options={CONF_SCAN_INTERVAL: 5},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
assert entry.options[CONF_SCAN_INTERVAL] == 5
|
||||
|
||||
result = await hass.config_entries.options.async_init(entry.entry_id)
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_SCAN_INTERVAL: 15},
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"][CONF_SCAN_INTERVAL] == 15
|
||||
@@ -0,0 +1,66 @@
|
||||
"""Test the NZBGet config flow."""
|
||||
from pynzbgetapi import NZBGetAPIException
|
||||
|
||||
from homeassistant.components.nzbget.const import DOMAIN
|
||||
from homeassistant.config_entries import (
|
||||
ENTRY_STATE_LOADED,
|
||||
ENTRY_STATE_NOT_LOADED,
|
||||
ENTRY_STATE_SETUP_RETRY,
|
||||
)
|
||||
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from . import (
|
||||
ENTRY_CONFIG,
|
||||
YAML_CONFIG,
|
||||
_patch_async_setup_entry,
|
||||
_patch_history,
|
||||
_patch_status,
|
||||
_patch_version,
|
||||
init_integration,
|
||||
)
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_import_from_yaml(hass) -> None:
|
||||
"""Test import from YAML."""
|
||||
with _patch_version(), _patch_status(), _patch_history(), _patch_async_setup_entry():
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: YAML_CONFIG})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entries = hass.config_entries.async_entries(DOMAIN)
|
||||
assert len(entries) == 1
|
||||
|
||||
assert entries[0].data[CONF_NAME] == "GetNZBsTest"
|
||||
assert entries[0].data[CONF_HOST] == "10.10.10.30"
|
||||
assert entries[0].data[CONF_PORT] == 6789
|
||||
|
||||
|
||||
async def test_unload_entry(hass):
|
||||
"""Test successful unload of entry."""
|
||||
entry = await init_integration(hass)
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert entry.state == ENTRY_STATE_LOADED
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert entry.state == ENTRY_STATE_NOT_LOADED
|
||||
assert not hass.data.get(DOMAIN)
|
||||
|
||||
|
||||
async def test_async_setup_raises_entry_not_ready(hass):
|
||||
"""Test that it throws ConfigEntryNotReady when exception occurs during setup."""
|
||||
config_entry = MockConfigEntry(domain=DOMAIN, data=ENTRY_CONFIG)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
with _patch_version(), patch(
|
||||
"homeassistant.components.nzbget.coordinator.NZBGetAPI.status",
|
||||
side_effect=NZBGetAPIException(),
|
||||
):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
|
||||
assert config_entry.state == ENTRY_STATE_SETUP_RETRY
|
||||
Reference in new issue
Block a user