Harden modbus against lib errors (#100469)
This commit is contained in:
parent
a111988232
commit
d25f45a957
5 changed files with 30 additions and 4 deletions
|
@ -419,9 +419,15 @@ class ModbusHub:
|
|||
except ModbusException as exception_error:
|
||||
self._log_error(str(exception_error))
|
||||
return None
|
||||
if not result:
|
||||
self._log_error("Error: pymodbus returned None")
|
||||
return None
|
||||
if not hasattr(result, entry.attr):
|
||||
self._log_error(str(result))
|
||||
return None
|
||||
if result.isError(): # type: ignore[no-untyped-call]
|
||||
self._log_error("Error: pymodbus returned isError True")
|
||||
return None
|
||||
self._in_error = False
|
||||
return result
|
||||
|
||||
|
|
|
@ -32,6 +32,11 @@ class ReadResult:
|
|||
"""Init."""
|
||||
self.registers = register_words
|
||||
self.bits = register_words
|
||||
self.value = register_words
|
||||
|
||||
def isError(self):
|
||||
"""Set error state."""
|
||||
return False
|
||||
|
||||
|
||||
@pytest.fixture(name="mock_pymodbus")
|
||||
|
@ -136,6 +141,10 @@ async def mock_pymodbus_return_fixture(hass, register_words, mock_modbus):
|
|||
mock_modbus.read_discrete_inputs.return_value = read_result
|
||||
mock_modbus.read_input_registers.return_value = read_result
|
||||
mock_modbus.read_holding_registers.return_value = read_result
|
||||
mock_modbus.write_register.return_value = read_result
|
||||
mock_modbus.write_registers.return_value = read_result
|
||||
mock_modbus.write_coil.return_value = read_result
|
||||
mock_modbus.write_coils.return_value = read_result
|
||||
|
||||
|
||||
@pytest.fixture(name="mock_do_cycle")
|
||||
|
|
|
@ -261,7 +261,10 @@ async def test_restore_state_fan(
|
|||
],
|
||||
)
|
||||
async def test_fan_service_turn(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_modbus
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
mock_modbus,
|
||||
mock_pymodbus_return,
|
||||
) -> None:
|
||||
"""Run test for service turn_on/turn_off."""
|
||||
|
||||
|
|
|
@ -260,7 +260,10 @@ async def test_restore_state_light(
|
|||
],
|
||||
)
|
||||
async def test_light_service_turn(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_modbus
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
mock_modbus,
|
||||
mock_pymodbus_return,
|
||||
) -> None:
|
||||
"""Run test for service turn_on/turn_off."""
|
||||
|
||||
|
|
|
@ -316,7 +316,10 @@ async def test_restore_state_switch(
|
|||
],
|
||||
)
|
||||
async def test_switch_service_turn(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_modbus
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
mock_modbus,
|
||||
mock_pymodbus_return,
|
||||
) -> None:
|
||||
"""Run test for service turn_on/turn_off."""
|
||||
assert MODBUS_DOMAIN in hass.config.components
|
||||
|
@ -407,7 +410,9 @@ async def test_service_switch_update(hass: HomeAssistant, mock_modbus, mock_ha)
|
|||
},
|
||||
],
|
||||
)
|
||||
async def test_delay_switch(hass: HomeAssistant, mock_modbus) -> None:
|
||||
async def test_delay_switch(
|
||||
hass: HomeAssistant, mock_modbus, mock_pymodbus_return
|
||||
) -> None:
|
||||
"""Run test for switch verify delay."""
|
||||
mock_modbus.read_holding_registers.return_value = ReadResult([0x01])
|
||||
now = dt_util.utcnow()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue