Skip to content

Commit

Permalink
Merge pull request #631 from canton7/bugfix/blocking-import-module
Browse files Browse the repository at this point in the history
Move call to import_module off event loop
  • Loading branch information
canton7 authored Jun 12, 2024
2 parents 9ade875 + a0ca72b commit ef33121
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions custom_components/foxess_modbus/client/modbus_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,20 @@ async def write_registers(self, register_address: int, register_values: list[int

async def _async_pymodbus_call(self, call: Callable[..., T], *args: Any, auto_connect: bool = True) -> T:
"""Convert async to sync pymodbus call."""
async with self._lock:

def _call() -> T:
# pymodbus 3.4.1 removes automatic reconnections for the sync modbus client.
# However, in versions prior to 4.3.0, the ModbusUdpClient didn't have a connected property.
# When using pollserial://, connected calls into serial.serial_for_url, which calls importlib.import_module,
# which HA doesn't like (see https://github.com/nathanmarlor/foxess_modbus/issues/618).
# Therefore we need to do this check inside the executor job
if auto_connect and hasattr(self._client, "connected") and not self._client.connected:
await self._hass.async_add_executor_job(self._client.connect)
self._client.connect()
# If the connection failed, this call will throw an appropriate error
result = await self._hass.async_add_executor_job(call, *args)
return call(*args)

async with self._lock:
result = await self._hass.async_add_executor_job(_call)
# This seems to be required for serial devices, otherwise subsequent reads fail
# The HA modbus integration does the same
if self._poll_delay > 0:
Expand Down

0 comments on commit ef33121

Please sign in to comment.