From 2d5176eee9b2a6eb370a5797500639d9cfa7c044 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Thu, 26 Aug 2021 15:23:00 +0200 Subject: [PATCH] Change entity_timers to be a local variable. (#55258) Ensure outstanding pymodbus calls are handled before closing. --- homeassistant/components/modbus/modbus.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/modbus/modbus.py b/homeassistant/components/modbus/modbus.py index c2cae9f4ec3..92609f5e891 100644 --- a/homeassistant/components/modbus/modbus.py +++ b/homeassistant/components/modbus/modbus.py @@ -189,13 +189,12 @@ class ModbusHub: name: str - entity_timers: list[CALLBACK_TYPE] = [] - def __init__(self, hass, client_config): """Initialize the Modbus hub.""" # generic configuration self._client = None + self.entity_timers: list[CALLBACK_TYPE] = [] self._async_cancel_listener = None self._in_error = False self._lock = asyncio.Lock() @@ -294,11 +293,12 @@ class ModbusHub: call() self.entity_timers = [] if self._client: - try: - self._client.close() - except ModbusException as exception_error: - self._log_error(str(exception_error)) - self._client = None + async with self._lock: + try: + self._client.close() + except ModbusException as exception_error: + self._log_error(str(exception_error)) + self._client = None def _pymodbus_connect(self): """Connect client."""