Cancel entity timers. (#55141)

This commit is contained in:
jan iversen 2021-08-25 14:49:37 +02:00 committed by GitHub
parent 2271f3b5f9
commit d4064e7044
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View File

@ -87,9 +87,10 @@ class BasePlatform(Entity):
async def async_base_added_to_hass(self): async def async_base_added_to_hass(self):
"""Handle entity which will be added.""" """Handle entity which will be added."""
if self._scan_interval > 0: 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.hass, self.async_update, timedelta(seconds=self._scan_interval)
) )
self._hub.entity_timers.append(cancel_func)
class BaseStructPlatform(BasePlatform, RestoreEntity): class BaseStructPlatform(BasePlatform, RestoreEntity):

View File

@ -20,7 +20,7 @@ from homeassistant.const import (
CONF_TYPE, CONF_TYPE,
EVENT_HOMEASSISTANT_STOP, 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.discovery import async_load_platform
from homeassistant.helpers.event import async_call_later from homeassistant.helpers.event import async_call_later
@ -189,6 +189,8 @@ class ModbusHub:
name: str name: str
entity_timers: list[CALLBACK_TYPE] = []
def __init__(self, hass, client_config): def __init__(self, hass, client_config):
"""Initialize the Modbus hub.""" """Initialize the Modbus hub."""
@ -288,6 +290,9 @@ class ModbusHub:
if self._async_cancel_listener: if self._async_cancel_listener:
self._async_cancel_listener() self._async_cancel_listener()
self._async_cancel_listener = None self._async_cancel_listener = None
for call in self.entity_timers:
call()
self.entity_timers = []
if self._client: if self._client:
try: try:
self._client.close() self._client.close()