Refactor dsmr tests (#39724)
This commit is contained in:
parent
fd065e7aea
commit
e97792adf0
3 changed files with 115 additions and 146 deletions
|
@ -10,46 +10,17 @@ import datetime
|
|||
from decimal import Decimal
|
||||
from itertools import chain, repeat
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.dsmr.const import DOMAIN
|
||||
from homeassistant.components.dsmr.sensor import DerivativeDSMREntity
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.const import ENERGY_KILO_WATT_HOUR, TIME_HOURS, VOLUME_CUBIC_METERS
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
import tests.async_mock
|
||||
from tests.async_mock import DEFAULT, MagicMock, Mock
|
||||
from tests.async_mock import DEFAULT, MagicMock
|
||||
from tests.common import MockConfigEntry, patch
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_connection_factory(monkeypatch):
|
||||
"""Mock the create functions for serial and TCP Asyncio connections."""
|
||||
from dsmr_parser.clients.protocol import DSMRProtocol
|
||||
|
||||
transport = tests.async_mock.Mock(spec=asyncio.Transport)
|
||||
protocol = tests.async_mock.Mock(spec=DSMRProtocol)
|
||||
|
||||
async def connection_factory(*args, **kwargs):
|
||||
"""Return mocked out Asyncio classes."""
|
||||
return (transport, protocol)
|
||||
|
||||
connection_factory = Mock(wraps=connection_factory)
|
||||
|
||||
# apply the mock to both connection factories
|
||||
monkeypatch.setattr(
|
||||
"homeassistant.components.dsmr.sensor.create_dsmr_reader", connection_factory
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"homeassistant.components.dsmr.sensor.create_tcp_dsmr_reader",
|
||||
connection_factory,
|
||||
)
|
||||
|
||||
return connection_factory, transport, protocol
|
||||
|
||||
|
||||
async def test_setup_platform(hass, mock_connection_factory):
|
||||
async def test_setup_platform(hass, dsmr_connection_fixture):
|
||||
"""Test setup of platform."""
|
||||
async_add_entities = MagicMock()
|
||||
|
||||
|
@ -87,9 +58,9 @@ async def test_setup_platform(hass, mock_connection_factory):
|
|||
assert entry.data == {**entry_data, **serial_data}
|
||||
|
||||
|
||||
async def test_default_setup(hass, mock_connection_factory):
|
||||
async def test_default_setup(hass, dsmr_connection_fixture):
|
||||
"""Test the default setup."""
|
||||
(connection_factory, transport, protocol) = mock_connection_factory
|
||||
(connection_factory, transport, protocol) = dsmr_connection_fixture
|
||||
|
||||
from dsmr_parser.obis_references import (
|
||||
CURRENT_ELECTRICITY_USAGE,
|
||||
|
@ -157,10 +128,6 @@ async def test_default_setup(hass, mock_connection_factory):
|
|||
assert gas_consumption.state == "745.695"
|
||||
assert gas_consumption.attributes.get("unit_of_measurement") == VOLUME_CUBIC_METERS
|
||||
|
||||
await hass.config_entries.async_unload(mock_entry.entry_id)
|
||||
|
||||
assert mock_entry.state == "not_loaded"
|
||||
|
||||
|
||||
async def test_derivative():
|
||||
"""Test calculation of derivative value."""
|
||||
|
@ -202,9 +169,9 @@ async def test_derivative():
|
|||
assert entity.unit_of_measurement == f"{VOLUME_CUBIC_METERS}/{TIME_HOURS}"
|
||||
|
||||
|
||||
async def test_v4_meter(hass, mock_connection_factory):
|
||||
async def test_v4_meter(hass, dsmr_connection_fixture):
|
||||
"""Test if v4 meter is correctly parsed."""
|
||||
(connection_factory, transport, protocol) = mock_connection_factory
|
||||
(connection_factory, transport, protocol) = dsmr_connection_fixture
|
||||
|
||||
from dsmr_parser.obis_references import (
|
||||
ELECTRICITY_ACTIVE_TARIFF,
|
||||
|
@ -256,14 +223,10 @@ async def test_v4_meter(hass, mock_connection_factory):
|
|||
assert gas_consumption.state == "745.695"
|
||||
assert gas_consumption.attributes.get("unit_of_measurement") == VOLUME_CUBIC_METERS
|
||||
|
||||
await hass.config_entries.async_unload(mock_entry.entry_id)
|
||||
|
||||
assert mock_entry.state == "not_loaded"
|
||||
|
||||
|
||||
async def test_v5_meter(hass, mock_connection_factory):
|
||||
async def test_v5_meter(hass, dsmr_connection_fixture):
|
||||
"""Test if v5 meter is correctly parsed."""
|
||||
(connection_factory, transport, protocol) = mock_connection_factory
|
||||
(connection_factory, transport, protocol) = dsmr_connection_fixture
|
||||
|
||||
from dsmr_parser.obis_references import (
|
||||
ELECTRICITY_ACTIVE_TARIFF,
|
||||
|
@ -315,14 +278,10 @@ async def test_v5_meter(hass, mock_connection_factory):
|
|||
assert gas_consumption.state == "745.695"
|
||||
assert gas_consumption.attributes.get("unit_of_measurement") == VOLUME_CUBIC_METERS
|
||||
|
||||
await hass.config_entries.async_unload(mock_entry.entry_id)
|
||||
|
||||
assert mock_entry.state == "not_loaded"
|
||||
|
||||
|
||||
async def test_belgian_meter(hass, mock_connection_factory):
|
||||
async def test_belgian_meter(hass, dsmr_connection_fixture):
|
||||
"""Test if Belgian meter is correctly parsed."""
|
||||
(connection_factory, transport, protocol) = mock_connection_factory
|
||||
(connection_factory, transport, protocol) = dsmr_connection_fixture
|
||||
|
||||
from dsmr_parser.obis_references import (
|
||||
BELGIUM_HOURLY_GAS_METER_READING,
|
||||
|
@ -374,14 +333,10 @@ async def test_belgian_meter(hass, mock_connection_factory):
|
|||
assert gas_consumption.state == "745.695"
|
||||
assert gas_consumption.attributes.get("unit_of_measurement") == VOLUME_CUBIC_METERS
|
||||
|
||||
await hass.config_entries.async_unload(mock_entry.entry_id)
|
||||
|
||||
assert mock_entry.state == "not_loaded"
|
||||
|
||||
|
||||
async def test_belgian_meter_low(hass, mock_connection_factory):
|
||||
async def test_belgian_meter_low(hass, dsmr_connection_fixture):
|
||||
"""Test if Belgian meter is correctly parsed."""
|
||||
(connection_factory, transport, protocol) = mock_connection_factory
|
||||
(connection_factory, transport, protocol) = dsmr_connection_fixture
|
||||
|
||||
from dsmr_parser.obis_references import ELECTRICITY_ACTIVE_TARIFF
|
||||
from dsmr_parser.objects import CosemObject
|
||||
|
@ -417,14 +372,10 @@ async def test_belgian_meter_low(hass, mock_connection_factory):
|
|||
assert power_tariff.state == "low"
|
||||
assert power_tariff.attributes.get("unit_of_measurement") == ""
|
||||
|
||||
await hass.config_entries.async_unload(mock_entry.entry_id)
|
||||
|
||||
assert mock_entry.state == "not_loaded"
|
||||
|
||||
|
||||
async def test_tcp(hass, mock_connection_factory):
|
||||
async def test_tcp(hass, dsmr_connection_fixture):
|
||||
"""If proper config provided TCP connection should be made."""
|
||||
(connection_factory, transport, protocol) = mock_connection_factory
|
||||
(connection_factory, transport, protocol) = dsmr_connection_fixture
|
||||
|
||||
entry_data = {
|
||||
"host": "localhost",
|
||||
|
@ -446,14 +397,10 @@ async def test_tcp(hass, mock_connection_factory):
|
|||
assert connection_factory.call_args_list[0][0][0] == "localhost"
|
||||
assert connection_factory.call_args_list[0][0][1] == "1234"
|
||||
|
||||
await hass.config_entries.async_unload(mock_entry.entry_id)
|
||||
|
||||
assert mock_entry.state == "not_loaded"
|
||||
|
||||
|
||||
async def test_connection_errors_retry(hass, monkeypatch, mock_connection_factory):
|
||||
async def test_connection_errors_retry(hass, dsmr_connection_fixture):
|
||||
"""Connection should be retried on error during setup."""
|
||||
(connection_factory, transport, protocol) = mock_connection_factory
|
||||
(connection_factory, transport, protocol) = dsmr_connection_fixture
|
||||
|
||||
entry_data = {
|
||||
"port": "/dev/ttyUSB0",
|
||||
|
@ -463,37 +410,32 @@ async def test_connection_errors_retry(hass, monkeypatch, mock_connection_factor
|
|||
}
|
||||
|
||||
# override the mock to have it fail the first time and succeed after
|
||||
first_fail_connection_factory = tests.async_mock.AsyncMock(
|
||||
first_fail_connection_factory = MagicMock(
|
||||
return_value=(transport, protocol),
|
||||
side_effect=chain([TimeoutError], repeat(DEFAULT)),
|
||||
)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"homeassistant.components.dsmr.sensor.create_dsmr_reader",
|
||||
first_fail_connection_factory,
|
||||
)
|
||||
|
||||
mock_entry = MockConfigEntry(
|
||||
domain="dsmr", unique_id="/dev/ttyUSB0", data=entry_data
|
||||
)
|
||||
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(mock_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
with patch(
|
||||
"homeassistant.components.dsmr.sensor.create_dsmr_reader",
|
||||
first_fail_connection_factory,
|
||||
):
|
||||
await hass.config_entries.async_setup(mock_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# wait for sleep to resolve
|
||||
await hass.async_block_till_done()
|
||||
assert first_fail_connection_factory.call_count >= 2, "connecting not retried"
|
||||
|
||||
await hass.config_entries.async_unload(mock_entry.entry_id)
|
||||
|
||||
assert mock_entry.state == "not_loaded"
|
||||
# wait for sleep to resolve
|
||||
await hass.async_block_till_done()
|
||||
assert first_fail_connection_factory.call_count >= 2, "connecting not retried"
|
||||
|
||||
|
||||
async def test_reconnect(hass, monkeypatch, mock_connection_factory):
|
||||
async def test_reconnect(hass, dsmr_connection_fixture):
|
||||
"""If transport disconnects, the connection should be retried."""
|
||||
(connection_factory, transport, protocol) = mock_connection_factory
|
||||
(connection_factory, transport, protocol) = dsmr_connection_fixture
|
||||
|
||||
entry_data = {
|
||||
"port": "/dev/ttyUSB0",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue