Update pymodbus fixtures to use autospec (#55686)

This commit is contained in:
jan iversen 2021-09-13 06:16:48 +02:00 committed by GitHub
parent a180c3f813
commit 1f997fcd58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 7 deletions

View File

@ -39,12 +39,17 @@ def mock_pymodbus():
"""Mock pymodbus."""
mock_pb = mock.MagicMock()
with mock.patch(
"homeassistant.components.modbus.modbus.ModbusTcpClient", return_value=mock_pb
"homeassistant.components.modbus.modbus.ModbusTcpClient",
return_value=mock_pb,
autospec=True,
), mock.patch(
"homeassistant.components.modbus.modbus.ModbusSerialClient",
return_value=mock_pb,
autospec=True,
), mock.patch(
"homeassistant.components.modbus.modbus.ModbusUdpClient", return_value=mock_pb
"homeassistant.components.modbus.modbus.ModbusUdpClient",
return_value=mock_pb,
autospec=True,
):
yield mock_pb
@ -96,10 +101,16 @@ async def mock_modbus(
}
mock_pb = mock.MagicMock()
with mock.patch(
"homeassistant.components.modbus.modbus.ModbusTcpClient", return_value=mock_pb
"homeassistant.components.modbus.modbus.ModbusTcpClient",
return_value=mock_pb,
autospec=True,
):
now = dt_util.utcnow()
with mock.patch("homeassistant.helpers.event.dt_util.utcnow", return_value=now):
with mock.patch(
"homeassistant.helpers.event.dt_util.utcnow",
return_value=now,
autospec=True,
):
result = await async_setup_component(hass, DOMAIN, config)
assert result or not check_config_loaded
await hass.async_block_till_done()
@ -131,7 +142,9 @@ async def mock_pymodbus_return(hass, register_words, mock_modbus):
async def mock_do_cycle(hass, mock_pymodbus_exception, mock_pymodbus_return):
"""Trigger update call with time_changed event."""
now = dt_util.utcnow() + timedelta(seconds=90)
with mock.patch("homeassistant.helpers.event.dt_util.utcnow", return_value=now):
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()

View File

@ -602,7 +602,7 @@ async def test_pymodbus_constructor_fail(hass, caplog):
]
}
with mock.patch(
"homeassistant.components.modbus.modbus.ModbusTcpClient"
"homeassistant.components.modbus.modbus.ModbusTcpClient", autospec=True
) as mock_pb:
caplog.set_level(logging.ERROR)
mock_pb.side_effect = ModbusException("test no class")
@ -669,7 +669,9 @@ async def test_delay(hass, mock_pymodbus):
# pass first scan_interval
start_time = now
now = now + timedelta(seconds=(test_scan_interval + 1))
with mock.patch("homeassistant.helpers.event.dt_util.utcnow", return_value=now):
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()
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE