diff --git a/tests/components/smhi/conftest.py b/tests/components/smhi/conftest.py index 39b3504e4b2..112abe093ac 100644 --- a/tests/components/smhi/conftest.py +++ b/tests/components/smhi/conftest.py @@ -49,7 +49,7 @@ async def load_int( entry_id="1", unique_id="59.32624-17.84197", version=3, - title="Home", + title="Test", ) config_entry.add_to_hass(hass) @@ -92,7 +92,7 @@ async def get_data_from_library( aioclient_mock.create_session(hass.loop), ) with patch.object( - client, + client._api, "async_get_data", return_value=load_json[0], ): diff --git a/tests/components/smhi/test_config_flow.py b/tests/components/smhi/test_config_flow.py index 524aad873f9..4544b46ba33 100644 --- a/tests/components/smhi/test_config_flow.py +++ b/tests/components/smhi/test_config_flow.py @@ -5,6 +5,7 @@ from __future__ import annotations from unittest.mock import patch from pysmhi import SmhiForecastException +import pytest from homeassistant import config_entries from homeassistant.components.smhi.const import DOMAIN @@ -16,6 +17,8 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er from tests.common import MockConfigEntry +pytestmark = pytest.mark.usefixtures("mock_setup_entry") + async def test_form(hass: HomeAssistant) -> None: """Test we get the form and create an entry.""" @@ -64,15 +67,9 @@ async def test_form(hass: HomeAssistant) -> None: result3 = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) - with ( - patch( - "homeassistant.components.smhi.config_flow.SMHIPointForecast.async_get_daily_forecast", - return_value={"test": "something", "test2": "something else"}, - ), - patch( - "homeassistant.components.smhi.async_setup_entry", - return_value=True, - ), + with patch( + "homeassistant.components.smhi.config_flow.SMHIPointForecast.async_get_daily_forecast", + return_value={"test": "something", "test2": "something else"}, ): result4 = await hass.config_entries.flow.async_configure( result3["flow_id"], @@ -120,15 +117,9 @@ async def test_form_invalid_coordinates(hass: HomeAssistant) -> None: assert result2["errors"] == {"base": "wrong_location"} # Continue flow with new coordinates - with ( - patch( - "homeassistant.components.smhi.config_flow.SMHIPointForecast.async_get_daily_forecast", - return_value={"test": "something", "test2": "something else"}, - ), - patch( - "homeassistant.components.smhi.async_setup_entry", - return_value=True, - ), + with patch( + "homeassistant.components.smhi.config_flow.SMHIPointForecast.async_get_daily_forecast", + return_value={"test": "something", "test2": "something else"}, ): result3 = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -235,15 +226,9 @@ async def test_reconfigure_flow( assert result["type"] is FlowResultType.FORM assert result["errors"] == {"base": "wrong_location"} - with ( - patch( - "homeassistant.components.smhi.config_flow.SMHIPointForecast.async_get_daily_forecast", - return_value={"test": "something", "test2": "something else"}, - ), - patch( - "homeassistant.components.smhi.async_setup_entry", - return_value=True, - ) as mock_setup_entry, + with patch( + "homeassistant.components.smhi.config_flow.SMHIPointForecast.async_get_daily_forecast", + return_value={"test": "something", "test2": "something else"}, ): result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -273,4 +258,3 @@ async def test_reconfigure_flow( device = device_registry.async_get(device.id) assert device assert device.identifiers == {(DOMAIN, "58.2898, 14.6304")} - assert len(mock_setup_entry.mock_calls) == 1 diff --git a/tests/components/smhi/test_init.py b/tests/components/smhi/test_init.py index f301e684e3e..7962c6a4ac6 100644 --- a/tests/components/smhi/test_init.py +++ b/tests/components/smhi/test_init.py @@ -1,6 +1,6 @@ """Test SMHI component setup process.""" -from pysmhi.const import API_POINT_FORECAST +from pysmhi import SMHIPointForecast from homeassistant.components.smhi.const import DOMAIN from homeassistant.config_entries import ConfigEntryState @@ -10,47 +10,21 @@ from homeassistant.helpers import entity_registry as er from . import ENTITY_ID, TEST_CONFIG, TEST_CONFIG_MIGRATE from tests.common import MockConfigEntry -from tests.test_util.aiohttp import AiohttpClientMocker -async def test_setup_entry( - hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, api_response: str -) -> None: - """Test setup entry.""" - uri = API_POINT_FORECAST.format( - TEST_CONFIG["location"]["longitude"], TEST_CONFIG["location"]["latitude"] - ) - aioclient_mock.get(uri, text=api_response) - entry = MockConfigEntry(domain=DOMAIN, title="test", data=TEST_CONFIG, version=3) - entry.add_to_hass(hass) - - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - - state = hass.states.get(ENTITY_ID) - assert state - - -async def test_remove_entry( - hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, api_response: str +async def test_load_and_unload_config_entry( + hass: HomeAssistant, load_int: MockConfigEntry ) -> None: """Test remove entry.""" - uri = API_POINT_FORECAST.format( - TEST_CONFIG["location"]["longitude"], TEST_CONFIG["location"]["latitude"] - ) - aioclient_mock.get(uri, text=api_response) - entry = MockConfigEntry(domain=DOMAIN, title="test", data=TEST_CONFIG, version=3) - entry.add_to_hass(hass) - - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() + assert load_int.state is ConfigEntryState.LOADED state = hass.states.get(ENTITY_ID) assert state - await hass.config_entries.async_remove(entry.entry_id) + await hass.config_entries.async_remove(load_int.entry_id) await hass.async_block_till_done() + assert load_int.state is ConfigEntryState.NOT_LOADED state = hass.states.get(ENTITY_ID) assert not state @@ -58,14 +32,10 @@ async def test_remove_entry( async def test_migrate_entry( hass: HomeAssistant, entity_registry: er.EntityRegistry, - aioclient_mock: AiohttpClientMocker, - api_response: str, + mock_client: SMHIPointForecast, ) -> None: """Test migrate entry data.""" - uri = API_POINT_FORECAST.format( - TEST_CONFIG_MIGRATE["longitude"], TEST_CONFIG_MIGRATE["latitude"] - ) - aioclient_mock.get(uri, text=api_response) + entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG_MIGRATE) entry.add_to_hass(hass) assert entry.version == 1 @@ -94,13 +64,9 @@ async def test_migrate_entry( async def test_migrate_from_future_version( - hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, api_response: str + hass: HomeAssistant, mock_client: SMHIPointForecast ) -> None: """Test migrate entry not possible from future version.""" - uri = API_POINT_FORECAST.format( - TEST_CONFIG_MIGRATE["longitude"], TEST_CONFIG_MIGRATE["latitude"] - ) - aioclient_mock.get(uri, text=api_response) entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG_MIGRATE, version=4) entry.add_to_hass(hass) assert entry.version == 4