diff --git a/tests/components/discovergy/conftest.py b/tests/components/discovergy/conftest.py index ea0fe84852f..b3a452e36e5 100644 --- a/tests/components/discovergy/conftest.py +++ b/tests/components/discovergy/conftest.py @@ -1,33 +1,59 @@ """Fixtures for Discovergy integration tests.""" -from unittest.mock import AsyncMock, Mock, patch +from unittest.mock import AsyncMock, patch +from pydiscovergy import Discovergy +from pydiscovergy.models import Reading import pytest from homeassistant.components.discovergy import DOMAIN from homeassistant.const import CONF_EMAIL, CONF_PASSWORD from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component from tests.common import MockConfigEntry -from tests.components.discovergy.const import GET_METERS +from tests.components.discovergy.const import GET_METERS, LAST_READING, LAST_READING_GAS -@pytest.fixture -def mock_meters() -> Mock: - """Patch libraries.""" - with patch("pydiscovergy.Discovergy.meters") as discovergy: - discovergy.side_effect = AsyncMock(return_value=GET_METERS) - yield discovergy +def _meter_last_reading(meter_id: str) -> Reading: + """Side effect function for Discovergy mock.""" + return ( + LAST_READING_GAS + if meter_id == "d81a652fe0824f9a9d336016587d3b9d" + else LAST_READING + ) -@pytest.fixture +@pytest.fixture(name="discovergy") +def mock_discovergy() -> None: + """Mock the pydiscovergy client.""" + mock = AsyncMock(spec=Discovergy) + mock.meters.return_value = GET_METERS + mock.meter_last_reading.side_effect = _meter_last_reading + + with patch( + "homeassistant.components.discovergy.pydiscovergy.Discovergy", + return_value=mock, + ): + yield mock + + +@pytest.fixture(name="config_entry") async def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry: """Return a MockConfigEntry for testing.""" - entry = MockConfigEntry( + return MockConfigEntry( domain=DOMAIN, title="user@example.org", unique_id="user@example.org", data={CONF_EMAIL: "user@example.org", CONF_PASSWORD: "supersecretpassword"}, ) - entry.add_to_hass(hass) - return entry + +@pytest.fixture(name="setup_integration") +async def mock_setup_integration( + hass: HomeAssistant, config_entry: MockConfigEntry, discovergy: AsyncMock +) -> None: + """Fixture for setting up the component.""" + config_entry.add_to_hass(hass) + + assert await async_setup_component(hass, DOMAIN, {}) + await hass.async_block_till_done() diff --git a/tests/components/discovergy/const.py b/tests/components/discovergy/const.py index 5c233d50ba8..6c5428741af 100644 --- a/tests/components/discovergy/const.py +++ b/tests/components/discovergy/const.py @@ -30,6 +30,32 @@ GET_METERS = [ "last_measurement_time": 1678430543742, }, ), + Meter( + meter_id="d81a652fe0824f9a9d336016587d3b9d", + serial_number="def456", + full_serial_number="def456", + type="PIP", + measurement_type="GAS", + load_profile_type="SLP", + location=Location( + zip=12345, + city="Testhause", + street="Teststraße", + street_number="1", + country="Germany", + ), + additional={ + "manufacturer_id": "TST", + "printed_full_serial_number": "def456", + "administration_number": "12345", + "scaling_factor": 1, + "current_scaling_factor": 1, + "voltage_scaling_factor": 1, + "internal_meters": 1, + "first_measurement_time": 1517569090926, + "last_measurement_time": 1678430543742, + }, + ), ] LAST_READING = Reading( @@ -50,3 +76,8 @@ LAST_READING = Reading( "voltage3": 239000.0, }, ) + +LAST_READING_GAS = Reading( + time=datetime.datetime(2023, 3, 10, 7, 32, 6, 702000), + values={"actualityDuration": 52000.0, "storageNumber": 0.0, "volume": 21064800.0}, +) diff --git a/tests/components/discovergy/snapshots/test_diagnostics.ambr b/tests/components/discovergy/snapshots/test_diagnostics.ambr index d02f57c7540..2a7dd6903af 100644 --- a/tests/components/discovergy/snapshots/test_diagnostics.ambr +++ b/tests/components/discovergy/snapshots/test_diagnostics.ambr @@ -22,8 +22,36 @@ 'serial_number': '**REDACTED**', 'type': 'TST', }), + dict({ + 'additional': dict({ + 'administration_number': '**REDACTED**', + 'current_scaling_factor': 1, + 'first_measurement_time': 1517569090926, + 'internal_meters': 1, + 'last_measurement_time': 1678430543742, + 'manufacturer_id': 'TST', + 'printed_full_serial_number': '**REDACTED**', + 'scaling_factor': 1, + 'voltage_scaling_factor': 1, + }), + 'full_serial_number': '**REDACTED**', + 'load_profile_type': 'SLP', + 'location': '**REDACTED**', + 'measurement_type': 'GAS', + 'meter_id': 'd81a652fe0824f9a9d336016587d3b9d', + 'serial_number': '**REDACTED**', + 'type': 'PIP', + }), ]), 'readings': dict({ + 'd81a652fe0824f9a9d336016587d3b9d': dict({ + 'time': '2023-03-10T07:32:06.702000', + 'values': dict({ + 'actualityDuration': 52000.0, + 'storageNumber': 0.0, + 'volume': 21064800.0, + }), + }), 'f8d610b7a8cc4e73939fa33b990ded54': dict({ 'time': '2023-03-10T07:32:06.702000', 'values': dict({ diff --git a/tests/components/discovergy/test_config_flow.py b/tests/components/discovergy/test_config_flow.py index 08e9df06978..16ba3a1546e 100644 --- a/tests/components/discovergy/test_config_flow.py +++ b/tests/components/discovergy/test_config_flow.py @@ -1,5 +1,5 @@ """Test the Discovergy config flow.""" -from unittest.mock import Mock, patch +from unittest.mock import AsyncMock, patch from pydiscovergy.error import DiscovergyClientError, HTTPError, InvalidLogin import pytest @@ -14,7 +14,7 @@ from tests.common import MockConfigEntry from tests.components.discovergy.const import GET_METERS -async def test_form(hass: HomeAssistant, mock_meters: Mock) -> None: +async def test_form(hass: HomeAssistant, discovergy: AsyncMock) -> None: """Test we get the form.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} @@ -25,7 +25,10 @@ async def test_form(hass: HomeAssistant, mock_meters: Mock) -> None: with patch( "homeassistant.components.discovergy.async_setup_entry", return_value=True, - ) as mock_setup_entry: + ) as mock_setup_entry, patch( + "homeassistant.components.discovergy.config_flow.pydiscovergy.Discovergy", + return_value=discovergy, + ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], { @@ -45,12 +48,14 @@ async def test_form(hass: HomeAssistant, mock_meters: Mock) -> None: async def test_reauth( - hass: HomeAssistant, mock_meters: Mock, mock_config_entry: MockConfigEntry + hass: HomeAssistant, config_entry: MockConfigEntry, discovergy: AsyncMock ) -> None: """Test reauth flow.""" + config_entry.add_to_hass(hass) + init_result = await hass.config_entries.flow.async_init( DOMAIN, - context={"source": SOURCE_REAUTH, "unique_id": mock_config_entry.unique_id}, + context={"source": SOURCE_REAUTH, "unique_id": config_entry.unique_id}, data=None, ) @@ -60,7 +65,10 @@ async def test_reauth( with patch( "homeassistant.components.discovergy.async_setup_entry", return_value=True, - ) as mock_setup_entry: + ) as mock_setup_entry, patch( + "homeassistant.components.discovergy.config_flow.pydiscovergy.Discovergy", + return_value=discovergy, + ): configure_result = await hass.config_entries.flow.async_configure( init_result["flow_id"], { @@ -88,7 +96,7 @@ async def test_form_fail(hass: HomeAssistant, error: Exception, message: str) -> """Test to handle exceptions.""" with patch( - "pydiscovergy.Discovergy.meters", + "homeassistant.components.discovergy.config_flow.pydiscovergy.Discovergy.meters", side_effect=error, ): result = await hass.config_entries.flow.async_init( @@ -104,7 +112,10 @@ async def test_form_fail(hass: HomeAssistant, error: Exception, message: str) -> assert result["step_id"] == "user" assert result["errors"] == {"base": message} - with patch("pydiscovergy.Discovergy.meters", return_value=GET_METERS): + with patch( + "homeassistant.components.discovergy.config_flow.pydiscovergy.Discovergy.meters", + return_value=GET_METERS, + ): result = await hass.config_entries.flow.async_configure( result["flow_id"], { diff --git a/tests/components/discovergy/test_diagnostics.py b/tests/components/discovergy/test_diagnostics.py index d7565e3f0c4..f2db5fb854d 100644 --- a/tests/components/discovergy/test_diagnostics.py +++ b/tests/components/discovergy/test_diagnostics.py @@ -1,31 +1,22 @@ """Test Discovergy diagnostics.""" -from unittest.mock import patch - +import pytest from syrupy import SnapshotAssertion from homeassistant.core import HomeAssistant from tests.common import MockConfigEntry from tests.components.diagnostics import get_diagnostics_for_config_entry -from tests.components.discovergy.const import GET_METERS, LAST_READING from tests.typing import ClientSessionGenerator +@pytest.mark.usefixtures("setup_integration") async def test_entry_diagnostics( hass: HomeAssistant, hass_client: ClientSessionGenerator, - mock_config_entry: MockConfigEntry, + config_entry: MockConfigEntry, snapshot: SnapshotAssertion, ) -> None: """Test config entry diagnostics.""" - with patch("pydiscovergy.Discovergy.meters", return_value=GET_METERS), patch( - "pydiscovergy.Discovergy.meter_last_reading", return_value=LAST_READING - ): - await hass.config_entries.async_setup(mock_config_entry.entry_id) - await hass.async_block_till_done() - - result = await get_diagnostics_for_config_entry( - hass, hass_client, mock_config_entry - ) + result = await get_diagnostics_for_config_entry(hass, hass_client, config_entry) assert result == snapshot