Complete modbus device response tests (#49633)
* Prepare test harness for new pymodbus return types. Use pytest.fixture to mock pymodbus. Use pytest.fixture to load modbus using mocked pymodbus Add test of Exception/IllegalResponse/ExceptionResponse from pymodbus. * Modbus.py is back at 100% test coverage. * Added assert mock.called. * add mock reset.
This commit is contained in:
parent
0379dee47e
commit
760caeed85
3 changed files with 285 additions and 117 deletions
|
@ -20,9 +20,43 @@ import homeassistant.util.dt as dt_util
|
|||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
TEST_MODBUS_NAME = "modbusTest"
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_pymodbus():
|
||||
"""Mock pymodbus."""
|
||||
mock_pb = mock.MagicMock()
|
||||
with mock.patch(
|
||||
"homeassistant.components.modbus.modbus.ModbusTcpClient", return_value=mock_pb
|
||||
), mock.patch(
|
||||
"homeassistant.components.modbus.modbus.ModbusSerialClient",
|
||||
return_value=mock_pb,
|
||||
), mock.patch(
|
||||
"homeassistant.components.modbus.modbus.ModbusUdpClient", return_value=mock_pb
|
||||
):
|
||||
yield mock_pb
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def mock_modbus(hass, mock_pymodbus):
|
||||
"""Load integration modbus using mocked pymodbus."""
|
||||
config = {
|
||||
DOMAIN: [
|
||||
{
|
||||
CONF_TYPE: "tcp",
|
||||
CONF_HOST: "modbusTestHost",
|
||||
CONF_PORT: 5501,
|
||||
CONF_NAME: TEST_MODBUS_NAME,
|
||||
}
|
||||
]
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config) is True
|
||||
await hass.async_block_till_done()
|
||||
yield mock_pymodbus
|
||||
|
||||
|
||||
class ReadResult:
|
||||
"""Storage class for register read results."""
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue