Move connect method into a task (#88452)

Move connect() into a task.
pull/88501/head
jan iversen 2023-02-20 08:32:47 +01:00 committed by GitHub
parent 488d78571e
commit b11539fb04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View File

@ -310,6 +310,13 @@ class ModbusHub:
_LOGGER.error(log_text)
self._in_error = error_state
async def async_pymodbus_connect(self) -> None:
"""Connect to device, async."""
async with self._lock:
if not await self.hass.async_add_executor_job(self._pymodbus_connect):
err = f"{self.name} connect failed, retry in pymodbus"
self._log_error(err, error_state=False)
async def async_setup(self) -> bool:
"""Set up pymodbus client."""
try:
@ -322,11 +329,9 @@ class ModbusHub:
func = getattr(self._client, entry.func_name)
self._pb_call[entry.call_type] = RunEntry(entry.attr, func)
async with self._lock:
if not await self.hass.async_add_executor_job(self._pymodbus_connect):
err = f"{self.name} connect failed, retry in pymodbus"
self._log_error(err, error_state=False)
return False
self.hass.async_create_background_task(
self.async_pymodbus_connect(), "modbus-connect"
)
# Start counting down to allow modbus requests.
if self._config_delay:

View File

@ -704,7 +704,7 @@ async def test_pymodbus_connect_fail(
caplog.set_level(logging.WARNING)
ExceptionMessage = "test connect exception"
mock_pymodbus.connect.side_effect = ModbusException(ExceptionMessage)
assert await async_setup_component(hass, DOMAIN, config) is False
assert await async_setup_component(hass, DOMAIN, config) is True
assert ExceptionMessage in caplog.text