mirror of
https://github.com/home-assistant/core.git
synced 2025-07-11 23:37:18 +00:00
Add test of lazy_error in modbus (#57170)
This commit is contained in:
parent
3a2d6a6343
commit
199cf649be
@ -648,11 +648,8 @@ omit =
|
|||||||
homeassistant/components/mjpeg/camera.py
|
homeassistant/components/mjpeg/camera.py
|
||||||
homeassistant/components/mochad/*
|
homeassistant/components/mochad/*
|
||||||
homeassistant/components/modbus/base_platform.py
|
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/climate.py
|
||||||
homeassistant/components/modbus/modbus.py
|
homeassistant/components/modbus/modbus.py
|
||||||
homeassistant/components/modbus/sensor.py
|
|
||||||
homeassistant/components/modbus/validators.py
|
homeassistant/components/modbus/validators.py
|
||||||
homeassistant/components/modem_callerid/sensor.py
|
homeassistant/components/modem_callerid/sensor.py
|
||||||
homeassistant/components/motion_blinds/__init__.py
|
homeassistant/components/motion_blinds/__init__.py
|
||||||
|
@ -147,6 +147,18 @@ async def mock_do_cycle(hass, mock_pymodbus_exception, mock_pymodbus_return):
|
|||||||
):
|
):
|
||||||
async_fire_time_changed(hass, now)
|
async_fire_time_changed(hass, now)
|
||||||
await hass.async_block_till_done()
|
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
|
@pytest.fixture
|
||||||
@ -161,3 +173,9 @@ async def mock_ha(hass, mock_pymodbus_return):
|
|||||||
"""Load homeassistant to allow service calls."""
|
"""Load homeassistant to allow service calls."""
|
||||||
assert await async_setup_component(hass, "homeassistant", {})
|
assert await async_setup_component(hass, "homeassistant", {})
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
async def caplog_setup_text(caplog):
|
||||||
|
"""Return setup log of integration."""
|
||||||
|
yield caplog.text
|
||||||
|
@ -21,7 +21,7 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import State
|
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}"
|
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
|
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(
|
@pytest.mark.parametrize(
|
||||||
"do_config",
|
"do_config",
|
||||||
[
|
[
|
||||||
|
@ -30,7 +30,7 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import State
|
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}"
|
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
|
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(
|
@pytest.mark.parametrize(
|
||||||
"do_config",
|
"do_config",
|
||||||
[
|
[
|
||||||
|
@ -39,7 +39,7 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import State
|
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}"
|
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
|
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(
|
@pytest.mark.parametrize(
|
||||||
"do_config",
|
"do_config",
|
||||||
[
|
[
|
||||||
|
Loading…
x
Reference in New Issue
Block a user