diff --git a/homeassistant/components/modbus/base_platform.py b/homeassistant/components/modbus/base_platform.py index 1e2f1056db2..d9ee58f3c38 100644 --- a/homeassistant/components/modbus/base_platform.py +++ b/homeassistant/components/modbus/base_platform.py @@ -87,9 +87,10 @@ class BasePlatform(Entity): async def async_base_added_to_hass(self): """Handle entity which will be added.""" if self._scan_interval > 0: - async_track_time_interval( + cancel_func = async_track_time_interval( self.hass, self.async_update, timedelta(seconds=self._scan_interval) ) + self._hub.entity_timers.append(cancel_func) class BaseStructPlatform(BasePlatform, RestoreEntity): diff --git a/homeassistant/components/modbus/modbus.py b/homeassistant/components/modbus/modbus.py index 7cab51f7fe6..c2cae9f4ec3 100644 --- a/homeassistant/components/modbus/modbus.py +++ b/homeassistant/components/modbus/modbus.py @@ -20,7 +20,7 @@ from homeassistant.const import ( CONF_TYPE, EVENT_HOMEASSISTANT_STOP, ) -from homeassistant.core import callback +from homeassistant.core import CALLBACK_TYPE, callback from homeassistant.helpers.discovery import async_load_platform from homeassistant.helpers.event import async_call_later @@ -189,6 +189,8 @@ class ModbusHub: name: str + entity_timers: list[CALLBACK_TYPE] = [] + def __init__(self, hass, client_config): """Initialize the Modbus hub.""" @@ -288,6 +290,9 @@ class ModbusHub: if self._async_cancel_listener: self._async_cancel_listener() self._async_cancel_listener = None + for call in self.entity_timers: + call() + self.entity_timers = [] if self._client: try: self._client.close()