Improve type hints in dsmr tests (#120393)

pull/120410/head
epenet 2024-06-25 13:11:27 +02:00 committed by GitHub
parent fccb7ea1f9
commit cbb3d48bd9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 85 additions and 34 deletions

View File

@ -15,10 +15,11 @@ from dsmr_parser.obis_references import (
)
from dsmr_parser.objects import CosemObject
import pytest
from typing_extensions import Generator
@pytest.fixture
async def dsmr_connection_fixture(hass):
def dsmr_connection_fixture() -> Generator[tuple[MagicMock, MagicMock, MagicMock]]:
"""Fixture that mocks serial connection."""
transport = MagicMock(spec=asyncio.Transport)
@ -44,7 +45,9 @@ async def dsmr_connection_fixture(hass):
@pytest.fixture
async def rfxtrx_dsmr_connection_fixture(hass):
def rfxtrx_dsmr_connection_fixture() -> (
Generator[tuple[MagicMock, MagicMock, MagicMock]]
):
"""Fixture that mocks RFXtrx connection."""
transport = MagicMock(spec=asyncio.Transport)
@ -70,7 +73,9 @@ async def rfxtrx_dsmr_connection_fixture(hass):
@pytest.fixture
async def dsmr_connection_send_validate_fixture(hass):
def dsmr_connection_send_validate_fixture() -> (
Generator[tuple[MagicMock, MagicMock, MagicMock]]
):
"""Fixture that mocks serial connection."""
transport = MagicMock(spec=asyncio.Transport)
@ -151,7 +156,9 @@ async def dsmr_connection_send_validate_fixture(hass):
@pytest.fixture
async def rfxtrx_dsmr_connection_send_validate_fixture(hass):
def rfxtrx_dsmr_connection_send_validate_fixture() -> (
Generator[tuple[MagicMock, MagicMock, MagicMock]]
):
"""Fixture that mocks serial connection."""
transport = MagicMock(spec=asyncio.Transport)

View File

@ -32,7 +32,8 @@ def com_port():
async def test_setup_network(
hass: HomeAssistant, dsmr_connection_send_validate_fixture
hass: HomeAssistant,
dsmr_connection_send_validate_fixture: tuple[MagicMock, MagicMock, MagicMock],
) -> None:
"""Test we can setup network."""
result = await hass.config_entries.flow.async_init(
@ -77,8 +78,10 @@ async def test_setup_network(
async def test_setup_network_rfxtrx(
hass: HomeAssistant,
dsmr_connection_send_validate_fixture,
rfxtrx_dsmr_connection_send_validate_fixture,
dsmr_connection_send_validate_fixture: tuple[MagicMock, MagicMock, MagicMock],
rfxtrx_dsmr_connection_send_validate_fixture: tuple[
MagicMock, MagicMock, MagicMock
],
) -> None:
"""Test we can setup network."""
(connection_factory, transport, protocol) = dsmr_connection_send_validate_fixture
@ -185,7 +188,7 @@ async def test_setup_network_rfxtrx(
async def test_setup_serial(
com_mock,
hass: HomeAssistant,
dsmr_connection_send_validate_fixture,
dsmr_connection_send_validate_fixture: tuple[MagicMock, MagicMock, MagicMock],
version: str,
entry_data: dict[str, Any],
) -> None:
@ -225,8 +228,10 @@ async def test_setup_serial(
async def test_setup_serial_rfxtrx(
com_mock,
hass: HomeAssistant,
dsmr_connection_send_validate_fixture,
rfxtrx_dsmr_connection_send_validate_fixture,
dsmr_connection_send_validate_fixture: tuple[MagicMock, MagicMock, MagicMock],
rfxtrx_dsmr_connection_send_validate_fixture: tuple[
MagicMock, MagicMock, MagicMock
],
) -> None:
"""Test we can setup serial."""
(connection_factory, transport, protocol) = dsmr_connection_send_validate_fixture
@ -273,7 +278,9 @@ async def test_setup_serial_rfxtrx(
@patch("serial.tools.list_ports.comports", return_value=[com_port()])
async def test_setup_serial_manual(
com_mock, hass: HomeAssistant, dsmr_connection_send_validate_fixture
com_mock,
hass: HomeAssistant,
dsmr_connection_send_validate_fixture: tuple[MagicMock, MagicMock, MagicMock],
) -> None:
"""Test we can setup serial with manual entry."""
result = await hass.config_entries.flow.async_init(
@ -321,7 +328,9 @@ async def test_setup_serial_manual(
@patch("serial.tools.list_ports.comports", return_value=[com_port()])
async def test_setup_serial_fail(
com_mock, hass: HomeAssistant, dsmr_connection_send_validate_fixture
com_mock,
hass: HomeAssistant,
dsmr_connection_send_validate_fixture: tuple[MagicMock, MagicMock, MagicMock],
) -> None:
"""Test failed serial connection."""
(connection_factory, transport, protocol) = dsmr_connection_send_validate_fixture
@ -369,8 +378,10 @@ async def test_setup_serial_fail(
async def test_setup_serial_timeout(
com_mock,
hass: HomeAssistant,
dsmr_connection_send_validate_fixture,
rfxtrx_dsmr_connection_send_validate_fixture,
dsmr_connection_send_validate_fixture: tuple[MagicMock, MagicMock, MagicMock],
rfxtrx_dsmr_connection_send_validate_fixture: tuple[
MagicMock, MagicMock, MagicMock
],
) -> None:
"""Test failed serial connection."""
(connection_factory, transport, protocol) = dsmr_connection_send_validate_fixture
@ -425,8 +436,10 @@ async def test_setup_serial_timeout(
async def test_setup_serial_wrong_telegram(
com_mock,
hass: HomeAssistant,
dsmr_connection_send_validate_fixture,
rfxtrx_dsmr_connection_send_validate_fixture,
dsmr_connection_send_validate_fixture: tuple[MagicMock, MagicMock, MagicMock],
rfxtrx_dsmr_connection_send_validate_fixture: tuple[
MagicMock, MagicMock, MagicMock
],
) -> None:
"""Test failed telegram data."""
(connection_factory, transport, protocol) = dsmr_connection_send_validate_fixture

View File

@ -2,6 +2,7 @@
import datetime
from decimal import Decimal
from unittest.mock import MagicMock
from dsmr_parser.obis_references import (
BELGIUM_MBUS1_DEVICE_TYPE,
@ -22,7 +23,7 @@ async def test_migrate_gas_to_mbus(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
dsmr_connection_fixture,
dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock],
) -> None:
"""Test migration of unique_id."""
(connection_factory, transport, protocol) = dsmr_connection_fixture
@ -113,7 +114,7 @@ async def test_migrate_gas_to_mbus_exists(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
dsmr_connection_fixture,
dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock],
) -> None:
"""Test migration of unique_id."""
(connection_factory, transport, protocol) = dsmr_connection_fixture

View File

@ -63,7 +63,9 @@ from tests.common import MockConfigEntry, patch
async def test_default_setup(
hass: HomeAssistant, entity_registry: er.EntityRegistry, dsmr_connection_fixture
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock],
) -> None:
"""Test the default setup."""
(connection_factory, transport, protocol) = dsmr_connection_fixture
@ -191,7 +193,9 @@ async def test_default_setup(
async def test_setup_only_energy(
hass: HomeAssistant, entity_registry: er.EntityRegistry, dsmr_connection_fixture
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock],
) -> None:
"""Test the default setup."""
(connection_factory, transport, protocol) = dsmr_connection_fixture
@ -240,7 +244,9 @@ async def test_setup_only_energy(
assert not entry
async def test_v4_meter(hass: HomeAssistant, dsmr_connection_fixture) -> None:
async def test_v4_meter(
hass: HomeAssistant, dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock]
) -> None:
"""Test if v4 meter is correctly parsed."""
(connection_factory, transport, protocol) = dsmr_connection_fixture
@ -319,7 +325,10 @@ async def test_v4_meter(hass: HomeAssistant, dsmr_connection_fixture) -> None:
],
)
async def test_v5_meter(
hass: HomeAssistant, dsmr_connection_fixture, value: Decimal, state: str
hass: HomeAssistant,
dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock],
value: Decimal,
state: str,
) -> None:
"""Test if v5 meter is correctly parsed."""
(connection_factory, transport, protocol) = dsmr_connection_fixture
@ -386,7 +395,9 @@ async def test_v5_meter(
)
async def test_luxembourg_meter(hass: HomeAssistant, dsmr_connection_fixture) -> None:
async def test_luxembourg_meter(
hass: HomeAssistant, dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock]
) -> None:
"""Test if v5 meter is correctly parsed."""
(connection_factory, transport, protocol) = dsmr_connection_fixture
@ -468,7 +479,9 @@ async def test_luxembourg_meter(hass: HomeAssistant, dsmr_connection_fixture) ->
)
async def test_belgian_meter(hass: HomeAssistant, dsmr_connection_fixture) -> None:
async def test_belgian_meter(
hass: HomeAssistant, dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock]
) -> None:
"""Test if Belgian meter is correctly parsed."""
(connection_factory, transport, protocol) = dsmr_connection_fixture
@ -651,7 +664,9 @@ async def test_belgian_meter(hass: HomeAssistant, dsmr_connection_fixture) -> No
)
async def test_belgian_meter_alt(hass: HomeAssistant, dsmr_connection_fixture) -> None:
async def test_belgian_meter_alt(
hass: HomeAssistant, dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock]
) -> None:
"""Test if Belgian meter is correctly parsed."""
(connection_factory, transport, protocol) = dsmr_connection_fixture
@ -798,7 +813,9 @@ async def test_belgian_meter_alt(hass: HomeAssistant, dsmr_connection_fixture) -
)
async def test_belgian_meter_mbus(hass: HomeAssistant, dsmr_connection_fixture) -> None:
async def test_belgian_meter_mbus(
hass: HomeAssistant, dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock]
) -> None:
"""Test if Belgian meter is correctly parsed."""
(connection_factory, transport, protocol) = dsmr_connection_fixture
@ -905,7 +922,9 @@ async def test_belgian_meter_mbus(hass: HomeAssistant, dsmr_connection_fixture)
)
async def test_belgian_meter_low(hass: HomeAssistant, dsmr_connection_fixture) -> None:
async def test_belgian_meter_low(
hass: HomeAssistant, dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock]
) -> None:
"""Test if Belgian meter is correctly parsed."""
(connection_factory, transport, protocol) = dsmr_connection_fixture
@ -951,7 +970,9 @@ async def test_belgian_meter_low(hass: HomeAssistant, dsmr_connection_fixture) -
assert active_tariff.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
async def test_swedish_meter(hass: HomeAssistant, dsmr_connection_fixture) -> None:
async def test_swedish_meter(
hass: HomeAssistant, dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock]
) -> None:
"""Test if v5 meter is correctly parsed."""
(connection_factory, transport, protocol) = dsmr_connection_fixture
@ -1017,7 +1038,9 @@ async def test_swedish_meter(hass: HomeAssistant, dsmr_connection_fixture) -> No
)
async def test_easymeter(hass: HomeAssistant, dsmr_connection_fixture) -> None:
async def test_easymeter(
hass: HomeAssistant, dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock]
) -> None:
"""Test if Q3D meter is correctly parsed."""
(connection_factory, transport, protocol) = dsmr_connection_fixture
@ -1086,7 +1109,9 @@ async def test_easymeter(hass: HomeAssistant, dsmr_connection_fixture) -> None:
)
async def test_tcp(hass: HomeAssistant, dsmr_connection_fixture) -> None:
async def test_tcp(
hass: HomeAssistant, dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock]
) -> None:
"""If proper config provided TCP connection should be made."""
(connection_factory, transport, protocol) = dsmr_connection_fixture
@ -1112,7 +1137,10 @@ async def test_tcp(hass: HomeAssistant, dsmr_connection_fixture) -> None:
assert connection_factory.call_args_list[0][0][1] == "1234"
async def test_rfxtrx_tcp(hass: HomeAssistant, rfxtrx_dsmr_connection_fixture) -> None:
async def test_rfxtrx_tcp(
hass: HomeAssistant,
rfxtrx_dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock],
) -> None:
"""If proper config provided RFXtrx TCP connection should be made."""
(connection_factory, transport, protocol) = rfxtrx_dsmr_connection_fixture
@ -1140,7 +1168,7 @@ async def test_rfxtrx_tcp(hass: HomeAssistant, rfxtrx_dsmr_connection_fixture) -
@patch("homeassistant.components.dsmr.sensor.DEFAULT_RECONNECT_INTERVAL", 0)
async def test_connection_errors_retry(
hass: HomeAssistant, dsmr_connection_fixture
hass: HomeAssistant, dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock]
) -> None:
"""Connection should be retried on error during setup."""
(connection_factory, transport, protocol) = dsmr_connection_fixture
@ -1177,7 +1205,9 @@ async def test_connection_errors_retry(
@patch("homeassistant.components.dsmr.sensor.DEFAULT_RECONNECT_INTERVAL", 0)
async def test_reconnect(hass: HomeAssistant, dsmr_connection_fixture) -> None:
async def test_reconnect(
hass: HomeAssistant, dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock]
) -> None:
"""If transport disconnects, the connection should be retried."""
(connection_factory, transport, protocol) = dsmr_connection_fixture
@ -1255,7 +1285,7 @@ async def test_reconnect(hass: HomeAssistant, dsmr_connection_fixture) -> None:
async def test_gas_meter_providing_energy_reading(
hass: HomeAssistant, dsmr_connection_fixture
hass: HomeAssistant, dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock]
) -> None:
"""Test that gas providing energy readings use the correct device class."""
(connection_factory, transport, protocol) = dsmr_connection_fixture