diff --git a/tests/components/sonarr/__init__.py b/tests/components/sonarr/__init__.py index c7f31c67742..6a6f3f37283 100644 --- a/tests/components/sonarr/__init__.py +++ b/tests/components/sonarr/__init__.py @@ -18,6 +18,7 @@ from homeassistant.const import ( ) from homeassistant.helpers.typing import HomeAssistantType +from tests.async_mock import patch from tests.common import MockConfigEntry, load_fixture from tests.test_util.aiohttp import AiohttpClientMocker @@ -196,6 +197,10 @@ async def setup_integration( CONF_UPCOMING_DAYS: DEFAULT_UPCOMING_DAYS, CONF_WANTED_MAX_ITEMS: DEFAULT_WANTED_MAX_ITEMS, }, + options={ + CONF_UPCOMING_DAYS: DEFAULT_UPCOMING_DAYS, + CONF_WANTED_MAX_ITEMS: DEFAULT_WANTED_MAX_ITEMS, + }, ) entry.add_to_hass(hass) @@ -215,3 +220,17 @@ async def setup_integration( await hass.async_block_till_done() return entry + + +def _patch_async_setup(return_value=True): + """Patch the async setup of sonarr.""" + return patch( + "homeassistant.components.sonarr.async_setup", return_value=return_value + ) + + +def _patch_async_setup_entry(return_value=True): + """Patch the async entry setup of sonarr.""" + return patch( + "homeassistant.components.sonarr.async_setup_entry", return_value=return_value, + ) diff --git a/tests/components/sonarr/test_config_flow.py b/tests/components/sonarr/test_config_flow.py index 23ebd7c8e98..e376cda04ee 100644 --- a/tests/components/sonarr/test_config_flow.py +++ b/tests/components/sonarr/test_config_flow.py @@ -19,6 +19,8 @@ from tests.async_mock import patch from tests.components.sonarr import ( HOST, MOCK_USER_INPUT, + _patch_async_setup, + _patch_async_setup_entry, mock_connection, mock_connection_error, mock_connection_invalid_auth, @@ -27,30 +29,6 @@ from tests.components.sonarr import ( from tests.test_util.aiohttp import AiohttpClientMocker -async def test_options(hass, aioclient_mock: AiohttpClientMocker): - """Test updating options.""" - entry = await setup_integration(hass, aioclient_mock) - assert entry.options[CONF_UPCOMING_DAYS] == DEFAULT_UPCOMING_DAYS - assert entry.options[CONF_WANTED_MAX_ITEMS] == DEFAULT_WANTED_MAX_ITEMS - - result = await hass.config_entries.options.async_init(entry.entry_id) - - assert result["type"] == RESULT_TYPE_FORM - assert result["step_id"] == "init" - - with patch( - "homeassistant.components.sonarr.async_setup_entry", return_value=True - ), patch("homeassistant.components.sonarr.async_setup", return_value=True): - result = await hass.config_entries.options.async_configure( - result["flow_id"], - user_input={CONF_UPCOMING_DAYS: 2, CONF_WANTED_MAX_ITEMS: 100}, - ) - - assert result["type"] == RESULT_TYPE_CREATE_ENTRY - assert result["data"][CONF_UPCOMING_DAYS] == 2 - assert result["data"][CONF_WANTED_MAX_ITEMS] == 100 - - async def test_show_user_form(hass: HomeAssistantType) -> None: """Test that the user set up form is served.""" result = await hass.config_entries.flow.async_init( @@ -118,9 +96,10 @@ async def test_full_import_flow_implementation( user_input = MOCK_USER_INPUT.copy() - result = await hass.config_entries.flow.async_init( - DOMAIN, context={CONF_SOURCE: SOURCE_IMPORT}, data=user_input, - ) + with _patch_async_setup(), _patch_async_setup_entry(): + result = await hass.config_entries.flow.async_init( + DOMAIN, context={CONF_SOURCE: SOURCE_IMPORT}, data=user_input, + ) assert result["type"] == RESULT_TYPE_CREATE_ENTRY assert result["title"] == HOST @@ -128,10 +107,6 @@ async def test_full_import_flow_implementation( assert result["data"] assert result["data"][CONF_HOST] == HOST - assert result["result"] - assert result["result"].options[CONF_UPCOMING_DAYS] == DEFAULT_UPCOMING_DAYS - assert result["result"].options[CONF_WANTED_MAX_ITEMS] == DEFAULT_WANTED_MAX_ITEMS - async def test_full_user_flow_implementation( hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker @@ -147,9 +122,8 @@ async def test_full_user_flow_implementation( assert result["step_id"] == "user" user_input = MOCK_USER_INPUT.copy() - with patch( - "homeassistant.components.sonarr.async_setup_entry", return_value=True - ), patch("homeassistant.components.sonarr.async_setup", return_value=True): + + with _patch_async_setup(), _patch_async_setup_entry(): result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=user_input, ) @@ -179,9 +153,7 @@ async def test_full_user_flow_advanced_options( CONF_VERIFY_SSL: True, } - with patch( - "homeassistant.components.sonarr.async_setup_entry", return_value=True - ), patch("homeassistant.components.sonarr.async_setup", return_value=True): + with _patch_async_setup(), _patch_async_setup_entry(): result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=user_input, ) @@ -192,3 +164,25 @@ async def test_full_user_flow_advanced_options( assert result["data"] assert result["data"][CONF_HOST] == HOST assert result["data"][CONF_VERIFY_SSL] + + +async def test_options_flow(hass, aioclient_mock: AiohttpClientMocker): + """Test updating options.""" + entry = await setup_integration(hass, aioclient_mock, skip_entry_setup=True) + assert entry.options[CONF_UPCOMING_DAYS] == DEFAULT_UPCOMING_DAYS + assert entry.options[CONF_WANTED_MAX_ITEMS] == DEFAULT_WANTED_MAX_ITEMS + + result = await hass.config_entries.options.async_init(entry.entry_id) + + assert result["type"] == RESULT_TYPE_FORM + assert result["step_id"] == "init" + + with _patch_async_setup(), _patch_async_setup_entry(): + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={CONF_UPCOMING_DAYS: 2, CONF_WANTED_MAX_ITEMS: 100}, + ) + + assert result["type"] == RESULT_TYPE_CREATE_ENTRY + assert result["data"][CONF_UPCOMING_DAYS] == 2 + assert result["data"][CONF_WANTED_MAX_ITEMS] == 100 diff --git a/tests/components/sonarr/test_init.py b/tests/components/sonarr/test_init.py index 852befcb31c..27361382e78 100644 --- a/tests/components/sonarr/test_init.py +++ b/tests/components/sonarr/test_init.py @@ -7,6 +7,7 @@ from homeassistant.config_entries import ( ) from homeassistant.core import HomeAssistant +from tests.async_mock import patch from tests.components.sonarr import setup_integration from tests.test_util.aiohttp import AiohttpClientMocker @@ -23,7 +24,10 @@ async def test_unload_config_entry( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker ) -> None: """Test the configuration entry unloading.""" - entry = await setup_integration(hass, aioclient_mock) + with patch( + "homeassistant.components.sonarr.sensor.async_setup_entry", return_value=True, + ): + entry = await setup_integration(hass, aioclient_mock) assert hass.data[DOMAIN] assert entry.entry_id in hass.data[DOMAIN]