Streamline modbus before 100% coverage. (#57478)

This commit is contained in:
jan iversen 2021-10-11 20:26:36 +02:00 committed by GitHub
parent 48c2cfa6f8
commit 6a39119ccc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -293,22 +293,18 @@ class ModbusHub:
func = getattr(self._client, entry.func_name)
self._pb_call[entry.call_type] = RunEntry(entry.attr, func)
await self.async_connect_task()
return True
async def async_connect_task(self) -> None:
"""Try to connect, and retry if needed."""
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
return False
# Start counting down to allow modbus requests.
if self._config_delay:
self._async_cancel_listener = async_call_later(
self.hass, self._config_delay, self.async_end_delay
)
return True
@callback
def async_end_delay(self, args: Any) -> None:
@ -341,10 +337,8 @@ class ModbusHub:
def _pymodbus_connect(self) -> bool:
"""Connect client."""
if not self._client:
return False
try:
self._client.connect()
self._client.connect() # type: ignore[union-attr]
except ModbusException as exception_error:
self._log_error(str(exception_error), error_state=False)
return False