Skip to content

Commit

Permalink
Solve modbus test problem (#115376)
Browse files Browse the repository at this point in the history
Fix test.
  • Loading branch information
janiversen authored and frenck committed Apr 12, 2024
1 parent 98bc7c0 commit d055f98
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
12 changes: 11 additions & 1 deletion tests/components/modbus/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ def mock_pymodbus_fixture():
"""Mock pymodbus."""
mock_pb = mock.AsyncMock()
mock_pb.close = mock.MagicMock()
read_result = ReadResult([])
mock_pb.read_coils.return_value = read_result
mock_pb.read_discrete_inputs.return_value = read_result
mock_pb.read_input_registers.return_value = read_result
mock_pb.read_holding_registers.return_value = read_result
mock_pb.write_register.return_value = read_result
mock_pb.write_registers.return_value = read_result
mock_pb.write_coil.return_value = read_result
mock_pb.write_coils.return_value = read_result
with (
mock.patch(
"homeassistant.components.modbus.modbus.AsyncModbusTcpClient",
Expand Down Expand Up @@ -156,7 +165,7 @@ async def mock_pymodbus_exception_fixture(hass, do_exception, mock_modbus):
@pytest.fixture(name="mock_pymodbus_return")
async def mock_pymodbus_return_fixture(hass, register_words, mock_modbus):
"""Trigger update call with time_changed event."""
read_result = ReadResult(register_words) if register_words else None
read_result = ReadResult(register_words if register_words else [])
mock_modbus.read_coils.return_value = read_result
mock_modbus.read_discrete_inputs.return_value = read_result
mock_modbus.read_input_registers.return_value = read_result
Expand All @@ -165,6 +174,7 @@ async def mock_pymodbus_return_fixture(hass, register_words, mock_modbus):
mock_modbus.write_registers.return_value = read_result
mock_modbus.write_coil.return_value = read_result
mock_modbus.write_coils.return_value = read_result
return mock_modbus


@pytest.fixture(name="mock_do_cycle")
Expand Down
4 changes: 4 additions & 0 deletions tests/components/modbus/fixtures/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ modbus:
host: "testHost"
port: 5001
name: "testModbus"
sensors:
- name: "dummy"
address: 117
slave: 0
28 changes: 15 additions & 13 deletions tests/components/modbus/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,7 @@ async def test_shutdown(
],
)
async def test_stop_restart(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_modbus
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_pymodbus_return
) -> None:
"""Run test for service stop."""

Expand All @@ -1572,31 +1572,31 @@ async def test_stop_restart(
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == "17"

mock_modbus.reset_mock()
mock_pymodbus_return.reset_mock()
caplog.clear()
data = {
ATTR_HUB: TEST_MODBUS_NAME,
}
await hass.services.async_call(DOMAIN, SERVICE_STOP, data, blocking=True)
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
assert mock_modbus.close.called
assert mock_pymodbus_return.close.called
assert f"modbus {TEST_MODBUS_NAME} communication closed" in caplog.text

mock_modbus.reset_mock()
mock_pymodbus_return.reset_mock()
caplog.clear()
await hass.services.async_call(DOMAIN, SERVICE_RESTART, data, blocking=True)
await hass.async_block_till_done()
assert not mock_modbus.close.called
assert mock_modbus.connect.called
assert not mock_pymodbus_return.close.called
assert mock_pymodbus_return.connect.called
assert f"modbus {TEST_MODBUS_NAME} communication open" in caplog.text

mock_modbus.reset_mock()
mock_pymodbus_return.reset_mock()
caplog.clear()
await hass.services.async_call(DOMAIN, SERVICE_RESTART, data, blocking=True)
await hass.async_block_till_done()
assert mock_modbus.close.called
assert mock_modbus.connect.called
assert mock_pymodbus_return.close.called
assert mock_pymodbus_return.connect.called
assert f"modbus {TEST_MODBUS_NAME} communication closed" in caplog.text
assert f"modbus {TEST_MODBUS_NAME} communication open" in caplog.text

Expand Down Expand Up @@ -1626,7 +1626,7 @@ async def test_write_no_client(hass: HomeAssistant, mock_modbus) -> None:
async def test_integration_reload(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
mock_modbus,
mock_pymodbus_return,
freezer: FrozenDateTimeFactory,
) -> None:
"""Run test for integration reload."""
Expand All @@ -1647,7 +1647,7 @@ async def test_integration_reload(

@pytest.mark.parametrize("do_config", [{}])
async def test_integration_reload_failed(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_modbus
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_pymodbus_return
) -> None:
"""Run test for integration connect failure on reload."""
caplog.set_level(logging.INFO)
Expand All @@ -1656,7 +1656,9 @@ async def test_integration_reload_failed(
yaml_path = get_fixture_path("configuration.yaml", "modbus")
with (
mock.patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path),
mock.patch.object(mock_modbus, "connect", side_effect=ModbusException("error")),
mock.patch.object(
mock_pymodbus_return, "connect", side_effect=ModbusException("error")
),
):
await hass.services.async_call(DOMAIN, SERVICE_RELOAD, blocking=True)
await hass.async_block_till_done()
Expand All @@ -1667,7 +1669,7 @@ async def test_integration_reload_failed(

@pytest.mark.parametrize("do_config", [{}])
async def test_integration_setup_failed(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_modbus
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_pymodbus_return
) -> None:
"""Run test for integration setup on reload."""
with mock.patch.object(
Expand Down

0 comments on commit d055f98

Please sign in to comment.