From 199cf649bea3c71a699db6f74f5ebd77a7dcec25 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Mon, 11 Oct 2021 12:43:05 +0200 Subject: [PATCH] Add test of lazy_error in modbus (#57170) --- .coveragerc | 3 -- tests/components/modbus/conftest.py | 18 +++++++++ tests/components/modbus/test_binary_sensor.py | 39 +++++++++++++++++- tests/components/modbus/test_cover.py | 40 ++++++++++++++++++- tests/components/modbus/test_sensor.py | 40 ++++++++++++++++++- 5 files changed, 134 insertions(+), 6 deletions(-) diff --git a/.coveragerc b/.coveragerc index 70f2b8cee34..26e0a214e40 100644 --- a/.coveragerc +++ b/.coveragerc @@ -648,11 +648,8 @@ omit = homeassistant/components/mjpeg/camera.py homeassistant/components/mochad/* homeassistant/components/modbus/base_platform.py - homeassistant/components/modbus/binary_sensor.py - homeassistant/components/modbus/cover.py homeassistant/components/modbus/climate.py homeassistant/components/modbus/modbus.py - homeassistant/components/modbus/sensor.py homeassistant/components/modbus/validators.py homeassistant/components/modem_callerid/sensor.py homeassistant/components/motion_blinds/__init__.py diff --git a/tests/components/modbus/conftest.py b/tests/components/modbus/conftest.py index bd2ed9f7778..9b85d23df1c 100644 --- a/tests/components/modbus/conftest.py +++ b/tests/components/modbus/conftest.py @@ -147,6 +147,18 @@ async def mock_do_cycle(hass, mock_pymodbus_exception, mock_pymodbus_return): ): async_fire_time_changed(hass, now) await hass.async_block_till_done() + return now + + +async def do_next_cycle(hass, now, cycle): + """Trigger update call with time_changed event.""" + now += timedelta(seconds=cycle) + with mock.patch( + "homeassistant.helpers.event.dt_util.utcnow", return_value=now, autospec=True + ): + async_fire_time_changed(hass, now) + await hass.async_block_till_done() + return now @pytest.fixture @@ -161,3 +173,9 @@ async def mock_ha(hass, mock_pymodbus_return): """Load homeassistant to allow service calls.""" assert await async_setup_component(hass, "homeassistant", {}) await hass.async_block_till_done() + + +@pytest.fixture +async def caplog_setup_text(caplog): + """Return setup log of integration.""" + yield caplog.text diff --git a/tests/components/modbus/test_binary_sensor.py b/tests/components/modbus/test_binary_sensor.py index 5de03287592..d36a11e3eab 100644 --- a/tests/components/modbus/test_binary_sensor.py +++ b/tests/components/modbus/test_binary_sensor.py @@ -21,7 +21,7 @@ from homeassistant.const import ( ) from homeassistant.core import State -from .conftest import TEST_ENTITY_NAME, ReadResult +from .conftest import TEST_ENTITY_NAME, ReadResult, do_next_cycle ENTITY_ID = f"{SENSOR_DOMAIN}.{TEST_ENTITY_NAME}" @@ -119,6 +119,43 @@ async def test_all_binary_sensor(hass, expected, mock_do_cycle): assert hass.states.get(ENTITY_ID).state == expected +@pytest.mark.parametrize( + "do_config", + [ + { + CONF_BINARY_SENSORS: [ + { + CONF_NAME: TEST_ENTITY_NAME, + CONF_ADDRESS: 51, + CONF_INPUT_TYPE: CALL_TYPE_COIL, + CONF_SCAN_INTERVAL: 10, + CONF_LAZY_ERROR: 2, + }, + ], + }, + ], +) +@pytest.mark.parametrize( + "register_words,do_exception,start_expect,end_expect", + [ + ( + [0x00], + True, + STATE_OFF, + STATE_UNAVAILABLE, + ), + ], +) +async def test_lazy_error_binary_sensor(hass, start_expect, end_expect, mock_do_cycle): + """Run test for given config.""" + now = mock_do_cycle + assert hass.states.get(ENTITY_ID).state == start_expect + now = await do_next_cycle(hass, now, 11) + assert hass.states.get(ENTITY_ID).state == start_expect + now = await do_next_cycle(hass, now, 11) + assert hass.states.get(ENTITY_ID).state == end_expect + + @pytest.mark.parametrize( "do_config", [ diff --git a/tests/components/modbus/test_cover.py b/tests/components/modbus/test_cover.py index 9c5b08d59b6..f772955e55c 100644 --- a/tests/components/modbus/test_cover.py +++ b/tests/components/modbus/test_cover.py @@ -30,7 +30,7 @@ from homeassistant.const import ( ) from homeassistant.core import State -from .conftest import TEST_ENTITY_NAME, ReadResult +from .conftest import TEST_ENTITY_NAME, ReadResult, do_next_cycle ENTITY_ID = f"{COVER_DOMAIN}.{TEST_ENTITY_NAME}" @@ -111,6 +111,44 @@ async def test_coil_cover(hass, expected, mock_do_cycle): assert hass.states.get(ENTITY_ID).state == expected +@pytest.mark.parametrize( + "do_config", + [ + { + CONF_COVERS: [ + { + CONF_NAME: TEST_ENTITY_NAME, + CONF_INPUT_TYPE: CALL_TYPE_COIL, + CONF_ADDRESS: 1234, + CONF_SLAVE: 1, + CONF_SCAN_INTERVAL: 10, + CONF_LAZY_ERROR: 2, + }, + ], + }, + ], +) +@pytest.mark.parametrize( + "register_words,do_exception, start_expect,end_expect", + [ + ( + [0x00], + True, + STATE_OPEN, + STATE_UNAVAILABLE, + ), + ], +) +async def test_lazy_error_cover(hass, start_expect, end_expect, mock_do_cycle): + """Run test for given config.""" + now = mock_do_cycle + assert hass.states.get(ENTITY_ID).state == start_expect + now = await do_next_cycle(hass, now, 11) + assert hass.states.get(ENTITY_ID).state == start_expect + now = await do_next_cycle(hass, now, 11) + assert hass.states.get(ENTITY_ID).state == end_expect + + @pytest.mark.parametrize( "do_config", [ diff --git a/tests/components/modbus/test_sensor.py b/tests/components/modbus/test_sensor.py index e2435d6acc8..b979ac0435e 100644 --- a/tests/components/modbus/test_sensor.py +++ b/tests/components/modbus/test_sensor.py @@ -39,7 +39,7 @@ from homeassistant.const import ( ) from homeassistant.core import State -from .conftest import TEST_ENTITY_NAME, ReadResult +from .conftest import TEST_ENTITY_NAME, ReadResult, do_next_cycle ENTITY_ID = f"{SENSOR_DOMAIN}.{TEST_ENTITY_NAME}" @@ -552,6 +552,44 @@ async def test_all_sensor(hass, mock_do_cycle, expected): assert hass.states.get(ENTITY_ID).state == expected +@pytest.mark.parametrize( + "do_config", + [ + { + CONF_SENSORS: [ + { + CONF_NAME: TEST_ENTITY_NAME, + CONF_ADDRESS: 51, + CONF_SCAN_INTERVAL: 10, + CONF_LAZY_ERROR: 1, + }, + ], + }, + ], +) +@pytest.mark.parametrize( + "register_words,do_exception,start_expect,end_expect", + [ + ( + [0x8000], + True, + "17", + STATE_UNAVAILABLE, + ), + ], +) +async def test_lazy_error_sensor(hass, mock_do_cycle, start_expect, end_expect): + """Run test for sensor.""" + hass.states.async_set(ENTITY_ID, 17) + await hass.async_block_till_done() + now = mock_do_cycle + assert hass.states.get(ENTITY_ID).state == start_expect + now = await do_next_cycle(hass, now, 11) + assert hass.states.get(ENTITY_ID).state == start_expect + now = await do_next_cycle(hass, now, 11) + assert hass.states.get(ENTITY_ID).state == end_expect + + @pytest.mark.parametrize( "do_config", [