Migrate dsmr tests from coroutine to async/await (#30333)

This commit is contained in:
Franck Nijhof 2019-12-31 21:03:21 +01:00 committed by Martin Hjelmare
parent 2620a95944
commit 30dbed3f98

View File

@ -27,8 +27,7 @@ def mock_connection_factory(monkeypatch):
transport = asynctest.Mock(spec=asyncio.Transport) transport = asynctest.Mock(spec=asyncio.Transport)
protocol = asynctest.Mock(spec=DSMRProtocol) protocol = asynctest.Mock(spec=DSMRProtocol)
@asyncio.coroutine async def connection_factory(*args, **kwargs):
def connection_factory(*args, **kwargs):
"""Return mocked out Asyncio classes.""" """Return mocked out Asyncio classes."""
return (transport, protocol) return (transport, protocol)
@ -46,8 +45,7 @@ def mock_connection_factory(monkeypatch):
return connection_factory, transport, protocol return connection_factory, transport, protocol
@asyncio.coroutine async def test_default_setup(hass, mock_connection_factory):
def test_default_setup(hass, mock_connection_factory):
"""Test the default setup.""" """Test the default setup."""
(connection_factory, transport, protocol) = mock_connection_factory (connection_factory, transport, protocol) = mock_connection_factory
@ -67,7 +65,7 @@ def test_default_setup(hass, mock_connection_factory):
} }
with assert_setup_component(1): with assert_setup_component(1):
yield from async_setup_component(hass, "sensor", {"sensor": config}) await async_setup_component(hass, "sensor", {"sensor": config})
telegram_callback = connection_factory.call_args_list[0][0][2] telegram_callback = connection_factory.call_args_list[0][0][2]
@ -80,7 +78,7 @@ def test_default_setup(hass, mock_connection_factory):
telegram_callback(telegram) telegram_callback(telegram)
# after receiving telegram entities need to have the chance to update # after receiving telegram entities need to have the chance to update
yield from asyncio.sleep(0) await asyncio.sleep(0)
# ensure entities have new state value after incoming telegram # ensure entities have new state value after incoming telegram
power_consumption = hass.states.get("sensor.power_consumption") power_consumption = hass.states.get("sensor.power_consumption")
@ -93,15 +91,14 @@ def test_default_setup(hass, mock_connection_factory):
assert power_tariff.attributes.get("unit_of_measurement") == "" assert power_tariff.attributes.get("unit_of_measurement") == ""
@asyncio.coroutine async def test_derivative():
def test_derivative():
"""Test calculation of derivative value.""" """Test calculation of derivative value."""
from dsmr_parser.objects import MBusObject from dsmr_parser.objects import MBusObject
config = {"platform": "dsmr"} config = {"platform": "dsmr"}
entity = DerivativeDSMREntity("test", "1.0.0", config) entity = DerivativeDSMREntity("test", "1.0.0", config)
yield from entity.async_update() await entity.async_update()
assert entity.state is None, "initial state not unknown" assert entity.state is None, "initial state not unknown"
@ -113,7 +110,7 @@ def test_derivative():
] ]
) )
} }
yield from entity.async_update() await entity.async_update()
assert entity.state is None, "state after first update should still be unknown" assert entity.state is None, "state after first update should still be unknown"
@ -125,7 +122,7 @@ def test_derivative():
] ]
) )
} }
yield from entity.async_update() await entity.async_update()
assert ( assert (
abs(entity.state - 0.033) < 0.00001 abs(entity.state - 0.033) < 0.00001
@ -134,22 +131,20 @@ def test_derivative():
assert entity.unit_of_measurement == "m3/h" assert entity.unit_of_measurement == "m3/h"
@asyncio.coroutine async def test_tcp(hass, mock_connection_factory):
def test_tcp(hass, mock_connection_factory):
"""If proper config provided TCP connection should be made.""" """If proper config provided TCP connection should be made."""
(connection_factory, transport, protocol) = mock_connection_factory (connection_factory, transport, protocol) = mock_connection_factory
config = {"platform": "dsmr", "host": "localhost", "port": 1234} config = {"platform": "dsmr", "host": "localhost", "port": 1234}
with assert_setup_component(1): with assert_setup_component(1):
yield from async_setup_component(hass, "sensor", {"sensor": config}) await async_setup_component(hass, "sensor", {"sensor": config})
assert connection_factory.call_args_list[0][0][0] == "localhost" assert connection_factory.call_args_list[0][0][0] == "localhost"
assert connection_factory.call_args_list[0][0][1] == "1234" assert connection_factory.call_args_list[0][0][1] == "1234"
@asyncio.coroutine async def test_connection_errors_retry(hass, monkeypatch, mock_connection_factory):
def test_connection_errors_retry(hass, monkeypatch, mock_connection_factory):
"""Connection should be retried on error during setup.""" """Connection should be retried on error during setup."""
(connection_factory, transport, protocol) = mock_connection_factory (connection_factory, transport, protocol) = mock_connection_factory
@ -164,15 +159,14 @@ def test_connection_errors_retry(hass, monkeypatch, mock_connection_factory):
"homeassistant.components.dsmr.sensor.create_dsmr_reader", "homeassistant.components.dsmr.sensor.create_dsmr_reader",
first_fail_connection_factory, first_fail_connection_factory,
) )
yield from async_setup_component(hass, "sensor", {"sensor": config}) await async_setup_component(hass, "sensor", {"sensor": config})
# wait for sleep to resolve # wait for sleep to resolve
yield from hass.async_block_till_done() await hass.async_block_till_done()
assert first_fail_connection_factory.call_count == 2, "connecting not retried" assert first_fail_connection_factory.call_count == 2, "connecting not retried"
@asyncio.coroutine async def test_reconnect(hass, monkeypatch, mock_connection_factory):
def test_reconnect(hass, monkeypatch, mock_connection_factory):
"""If transport disconnects, the connection should be retried.""" """If transport disconnects, the connection should be retried."""
(connection_factory, transport, protocol) = mock_connection_factory (connection_factory, transport, protocol) = mock_connection_factory
config = {"platform": "dsmr", "reconnect_interval": 0} config = {"platform": "dsmr", "reconnect_interval": 0}
@ -182,26 +176,25 @@ def test_reconnect(hass, monkeypatch, mock_connection_factory):
# Handshake so that `hass.async_block_till_done()` doesn't cycle forever # Handshake so that `hass.async_block_till_done()` doesn't cycle forever
closed2 = asyncio.Event() closed2 = asyncio.Event()
@asyncio.coroutine async def wait_closed():
def wait_closed(): await closed.wait()
yield from closed.wait()
closed2.set() closed2.set()
closed.clear() closed.clear()
protocol.wait_closed = wait_closed protocol.wait_closed = wait_closed
yield from async_setup_component(hass, "sensor", {"sensor": config}) await async_setup_component(hass, "sensor", {"sensor": config})
assert connection_factory.call_count == 1 assert connection_factory.call_count == 1
# indicate disconnect, release wait lock and allow reconnect to happen # indicate disconnect, release wait lock and allow reconnect to happen
closed.set() closed.set()
# wait for lock set to resolve # wait for lock set to resolve
yield from closed2.wait() await closed2.wait()
closed2.clear() closed2.clear()
assert not closed.is_set() assert not closed.is_set()
closed.set() closed.set()
yield from hass.async_block_till_done() await hass.async_block_till_done()
assert connection_factory.call_count >= 2, "connecting not retried" assert connection_factory.call_count >= 2, "connecting not retried"