diff --git a/tests/components/rainforest_raven/__init__.py b/tests/components/rainforest_raven/__init__.py index 0269e4cf0f4..eb3cb4efcc2 100644 --- a/tests/components/rainforest_raven/__init__.py +++ b/tests/components/rainforest_raven/__init__.py @@ -17,7 +17,7 @@ from .const import ( from tests.common import AsyncMock, MockConfigEntry -def create_mock_device(): +def create_mock_device() -> AsyncMock: """Create a mock instance of RAVEnStreamDevice.""" device = AsyncMock() @@ -33,7 +33,7 @@ def create_mock_device(): return device -def create_mock_entry(no_meters=False): +def create_mock_entry(no_meters: bool = False) -> MockConfigEntry: """Create a mock config entry for a RAVEn device.""" return MockConfigEntry( domain=DOMAIN, diff --git a/tests/components/rainforest_raven/conftest.py b/tests/components/rainforest_raven/conftest.py new file mode 100644 index 00000000000..e935dbd3692 --- /dev/null +++ b/tests/components/rainforest_raven/conftest.py @@ -0,0 +1,33 @@ +"""Fixtures for the Rainforest RAVEn tests.""" + +from collections.abc import Generator +from unittest.mock import AsyncMock, patch + +import pytest + +from homeassistant.core import HomeAssistant + +from . import create_mock_device, create_mock_entry + +from tests.common import MockConfigEntry + + +@pytest.fixture +def mock_device() -> Generator[AsyncMock, None, None]: + """Mock a functioning RAVEn device.""" + mock_device = create_mock_device() + with patch( + "homeassistant.components.rainforest_raven.coordinator.RAVEnSerialDevice", + return_value=mock_device, + ): + yield mock_device + + +@pytest.fixture +async def mock_entry(hass: HomeAssistant, mock_device: AsyncMock) -> MockConfigEntry: + """Mock a functioning RAVEn config entry.""" + mock_entry = create_mock_entry() + mock_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_entry.entry_id) + await hass.async_block_till_done() + return mock_entry diff --git a/tests/components/rainforest_raven/test_coordinator.py b/tests/components/rainforest_raven/test_coordinator.py index 1a5f4d3d3f7..cc0bcac3978 100644 --- a/tests/components/rainforest_raven/test_coordinator.py +++ b/tests/components/rainforest_raven/test_coordinator.py @@ -10,20 +10,7 @@ from homeassistant.components.rainforest_raven.coordinator import RAVEnDataCoord from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady -from . import create_mock_device, create_mock_entry - -from tests.common import patch - - -@pytest.fixture -def mock_device(): - """Mock a functioning RAVEn device.""" - mock_device = create_mock_device() - with patch( - "homeassistant.components.rainforest_raven.coordinator.RAVEnSerialDevice", - return_value=mock_device, - ): - yield mock_device +from . import create_mock_entry async def test_coordinator_device_info(hass: HomeAssistant, mock_device): diff --git a/tests/components/rainforest_raven/test_diagnostics.py b/tests/components/rainforest_raven/test_diagnostics.py index d8caeb32f4a..86a86032ac6 100644 --- a/tests/components/rainforest_raven/test_diagnostics.py +++ b/tests/components/rainforest_raven/test_diagnostics.py @@ -8,35 +8,13 @@ from homeassistant.components.diagnostics import REDACTED from homeassistant.const import CONF_MAC from homeassistant.core import HomeAssistant -from . import create_mock_device, create_mock_entry +from . import create_mock_entry from .const import DEMAND, NETWORK_INFO, PRICE_CLUSTER, SUMMATION -from tests.common import patch from tests.components.diagnostics import get_diagnostics_for_config_entry from tests.typing import ClientSessionGenerator -@pytest.fixture -def mock_device(): - """Mock a functioning RAVEn device.""" - mock_device = create_mock_device() - with patch( - "homeassistant.components.rainforest_raven.coordinator.RAVEnSerialDevice", - return_value=mock_device, - ): - yield mock_device - - -@pytest.fixture -async def mock_entry(hass: HomeAssistant, mock_device): - """Mock a functioning RAVEn config entry.""" - mock_entry = create_mock_entry() - mock_entry.add_to_hass(hass) - await hass.config_entries.async_setup(mock_entry.entry_id) - await hass.async_block_till_done() - return mock_entry - - @pytest.fixture async def mock_entry_no_meters(hass: HomeAssistant, mock_device): """Mock a RAVEn config entry with no meters.""" diff --git a/tests/components/rainforest_raven/test_init.py b/tests/components/rainforest_raven/test_init.py index 1cc50971e09..5214e1ca563 100644 --- a/tests/components/rainforest_raven/test_init.py +++ b/tests/components/rainforest_raven/test_init.py @@ -1,36 +1,9 @@ """Tests for the Rainforest RAVEn component initialisation.""" -import pytest - from homeassistant.components.rainforest_raven.const import DOMAIN from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant -from . import create_mock_device, create_mock_entry - -from tests.common import patch - - -@pytest.fixture -def mock_device(): - """Mock a functioning RAVEn device.""" - mock_device = create_mock_device() - with patch( - "homeassistant.components.rainforest_raven.coordinator.RAVEnSerialDevice", - return_value=mock_device, - ): - yield mock_device - - -@pytest.fixture -async def mock_entry(hass: HomeAssistant, mock_device): - """Mock a functioning RAVEn config entry.""" - mock_entry = create_mock_entry() - mock_entry.add_to_hass(hass) - await hass.config_entries.async_setup(mock_entry.entry_id) - await hass.async_block_till_done() - return mock_entry - async def test_load_unload_entry(hass: HomeAssistant, mock_entry): """Test load and unload.""" diff --git a/tests/components/rainforest_raven/test_sensor.py b/tests/components/rainforest_raven/test_sensor.py index 36e6572149f..3259d8d7f2f 100644 --- a/tests/components/rainforest_raven/test_sensor.py +++ b/tests/components/rainforest_raven/test_sensor.py @@ -1,34 +1,7 @@ """Tests for the Rainforest RAVEn sensors.""" -import pytest - from homeassistant.core import HomeAssistant -from . import create_mock_device, create_mock_entry - -from tests.common import patch - - -@pytest.fixture -def mock_device(): - """Mock a functioning RAVEn device.""" - mock_device = create_mock_device() - with patch( - "homeassistant.components.rainforest_raven.coordinator.RAVEnSerialDevice", - return_value=mock_device, - ): - yield mock_device - - -@pytest.fixture -async def mock_entry(hass: HomeAssistant, mock_device): - """Mock a functioning RAVEn config entry.""" - mock_entry = create_mock_entry() - mock_entry.add_to_hass(hass) - await hass.config_entries.async_setup(mock_entry.entry_id) - await hass.async_block_till_done() - return mock_entry - async def test_sensors(hass: HomeAssistant, mock_device, mock_entry): """Test the sensors."""