diff --git a/homeassistant/components/nzbget/__init__.py b/homeassistant/components/nzbget/__init__.py index cb906495d58..a29ea829bbc 100644 --- a/homeassistant/components/nzbget/__init__.py +++ b/homeassistant/components/nzbget/__init__.py @@ -1,31 +1,18 @@ """The NZBGet integration.""" import voluptuous as vol -from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry -from homeassistant.const import ( - CONF_HOST, - CONF_NAME, - CONF_PASSWORD, - CONF_PORT, - CONF_SCAN_INTERVAL, - CONF_SSL, - CONF_USERNAME, - Platform, -) +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_SCAN_INTERVAL, Platform from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.helpers import config_validation as cv -from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import ( ATTR_SPEED, DATA_COORDINATOR, DATA_UNDO_UPDATE_LISTENER, - DEFAULT_NAME, - DEFAULT_PORT, DEFAULT_SCAN_INTERVAL, DEFAULT_SPEED_LIMIT, - DEFAULT_SSL, DOMAIN, SERVICE_PAUSE, SERVICE_RESUME, @@ -35,54 +22,17 @@ from .coordinator import NZBGetDataUpdateCoordinator PLATFORMS = [Platform.SENSOR, Platform.SWITCH] -CONFIG_SCHEMA = vol.Schema( - vol.All( - cv.deprecated(DOMAIN), - { - DOMAIN: vol.Schema( - { - vol.Required(CONF_HOST): cv.string, - vol.Optional(CONF_PASSWORD): cv.string, - vol.Optional(CONF_USERNAME): cv.string, - vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port, - vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, - vol.Optional( - CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL - ): cv.time_period, - vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean, - } - ) - }, - ), - extra=vol.ALLOW_EXTRA, -) +CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False) SPEED_LIMIT_SCHEMA = vol.Schema( {vol.Optional(ATTR_SPEED, default=DEFAULT_SPEED_LIMIT): cv.positive_int} ) -async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: - """Set up the NZBGet integration.""" - hass.data.setdefault(DOMAIN, {}) - - if hass.config_entries.async_entries(DOMAIN): - return True - - if DOMAIN in config: - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_IMPORT}, - data=config[DOMAIN], - ) - ) - - return True - - async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up NZBGet from a config entry.""" + hass.data.setdefault(DOMAIN, {}) + if not entry.options: options = { CONF_SCAN_INTERVAL: entry.data.get( diff --git a/homeassistant/components/nzbget/config_flow.py b/homeassistant/components/nzbget/config_flow.py index c7a1699a86c..732ef879762 100644 --- a/homeassistant/components/nzbget/config_flow.py +++ b/homeassistant/components/nzbget/config_flow.py @@ -6,7 +6,7 @@ from typing import Any import voluptuous as vol -from homeassistant.config_entries import ConfigFlow, OptionsFlow +from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow from homeassistant.const import ( CONF_HOST, CONF_NAME, @@ -17,7 +17,7 @@ from homeassistant.const import ( CONF_USERNAME, CONF_VERIFY_SSL, ) -from homeassistant.core import HomeAssistant, callback +from homeassistant.core import callback from homeassistant.data_entry_flow import FlowResult from .const import ( @@ -33,7 +33,7 @@ from .coordinator import NZBGetAPI, NZBGetAPIException _LOGGER = logging.getLogger(__name__) -def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]: +def _validate_input(data: dict[str, Any]) -> None: """Validate the user input allows us to connect. Data has the keys from DATA_SCHEMA with values provided by the user. @@ -49,8 +49,6 @@ def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]: nzbget_api.version() - return True - class NZBGetConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow for NZBGet.""" @@ -59,21 +57,10 @@ class NZBGetConfigFlow(ConfigFlow, domain=DOMAIN): @staticmethod @callback - def async_get_options_flow(config_entry): + def async_get_options_flow(config_entry: ConfigEntry) -> NZBGetOptionsFlowHandler: """Get the options flow for this handler.""" return NZBGetOptionsFlowHandler(config_entry) - async def async_step_import( - self, user_input: dict[str, Any] | None = None - ) -> FlowResult: - """Handle a flow initiated by configuration file.""" - if CONF_SCAN_INTERVAL in user_input: - user_input[CONF_SCAN_INTERVAL] = user_input[ - CONF_SCAN_INTERVAL - ].total_seconds() - - return await self.async_step_user(user_input) - async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> FlowResult: @@ -88,9 +75,7 @@ class NZBGetConfigFlow(ConfigFlow, domain=DOMAIN): user_input[CONF_VERIFY_SSL] = DEFAULT_VERIFY_SSL try: - await self.hass.async_add_executor_job( - validate_input, self.hass, user_input - ) + await self.hass.async_add_executor_job(_validate_input, user_input) except NZBGetAPIException: errors["base"] = "cannot_connect" except Exception: # pylint: disable=broad-except @@ -126,11 +111,13 @@ class NZBGetConfigFlow(ConfigFlow, domain=DOMAIN): class NZBGetOptionsFlowHandler(OptionsFlow): """Handle NZBGet client options.""" - def __init__(self, config_entry): + def __init__(self, config_entry: ConfigEntry) -> None: """Initialize options flow.""" self.config_entry = config_entry - async def async_step_init(self, user_input: dict[str, Any] | None = None): + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: """Manage NZBGet options.""" if user_input is not None: return self.async_create_entry(title="", data=user_input) diff --git a/tests/components/nzbget/__init__.py b/tests/components/nzbget/__init__.py index 9993bdaff1e..331b45e3de8 100644 --- a/tests/components/nzbget/__init__.py +++ b/tests/components/nzbget/__init__.py @@ -1,5 +1,4 @@ """Tests for the NZBGet integration.""" -from datetime import timedelta from unittest.mock import patch from homeassistant.components.nzbget.const import DOMAIN @@ -37,16 +36,6 @@ USER_INPUT = { 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 = { @@ -84,13 +73,6 @@ async def init_integration( 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", diff --git a/tests/components/nzbget/test_config_flow.py b/tests/components/nzbget/test_config_flow.py index f6e91d13d9e..8799e3adcf0 100644 --- a/tests/components/nzbget/test_config_flow.py +++ b/tests/components/nzbget/test_config_flow.py @@ -15,7 +15,6 @@ from homeassistant.data_entry_flow import ( from . import ( ENTRY_CONFIG, USER_INPUT, - _patch_async_setup, _patch_async_setup_entry, _patch_history, _patch_status, @@ -34,7 +33,7 @@ async def test_user_form(hass): 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: + with _patch_version(), _patch_status(), _patch_history(), _patch_async_setup_entry() as mock_setup_entry: result = await hass.config_entries.flow.async_configure( result["flow_id"], USER_INPUT, @@ -45,7 +44,6 @@ async def test_user_form(hass): 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 @@ -63,7 +61,7 @@ async def test_user_form_show_advanced_options(hass): 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: + with _patch_version(), _patch_status(), _patch_history(), _patch_async_setup_entry() as mock_setup_entry: result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input_advanced, @@ -74,7 +72,6 @@ async def test_user_form_show_advanced_options(hass): 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 @@ -149,7 +146,7 @@ async def test_options_flow(hass, nzbget_api): assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "init" - with _patch_async_setup(), _patch_async_setup_entry(): + with _patch_async_setup_entry(): result = await hass.config_entries.options.async_configure( result["flow_id"], user_input={CONF_SCAN_INTERVAL: 15}, diff --git a/tests/components/nzbget/test_init.py b/tests/components/nzbget/test_init.py index e83672769da..fbb65a4f8b2 100644 --- a/tests/components/nzbget/test_init.py +++ b/tests/components/nzbget/test_init.py @@ -5,36 +5,12 @@ from pynzbgetapi import NZBGetAPIException from homeassistant.components.nzbget.const import DOMAIN from homeassistant.config_entries import ConfigEntryState -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 . import ENTRY_CONFIG, _patch_version, init_integration 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, nzbget_api): """Test successful unload of entry.""" entry = await init_integration(hass)